nerdexam
Microsoft

DP-700 · Question #45

You have the following code segment: def loading_pattern_sample(df_source): try: deltatable = DeltaTable.forName(spark, target_table) except Exception as e…

The correct answer is B. No. The provided Python code segment contains a fundamental error in its exception handling structure, making the second except block unreachable.

Design and implement data ingestion and transformation

Question

You have the following code segment: def loading_pattern_sample(df_source): try: deltatable = DeltaTable.forName(spark, target_table) except Exception as e: df_source.write.format('delta').mode('overwrite').saveAsTable(f'{target_table}') except Exception as e: print(f'load for table {target_table} failed with error: {str(e)}') return try: change_detection_columns = [col for col in df_source.columns if col not in candidate_key] match_condition = ' AND '.join([f'target.{col} = source.{col}' for col in candidate_key]) update_condition = ' OR '.join([f'target.{col} != source.{col}' for col in change_detection_columns]) update_expr = {col: f'source.{col}' for col in df_source.columns} merge_operation = deltatable.alias('target').merge( source=df_source.alias('source'), condition=match_condition ).whenMatchedUpdate( condition=update_condition, set=update_expr ).whenNotMatchedInsertAll() merge_operation.execute() except Exception as e: print(f'insert operation for table {target_table} failed with error: {str(e)}') return Based on the code, will the target table always be overwritten?

Options

  • AYes
  • BNo

How the community answered

(38 responses)
  • A
    37% (14)
  • B
    63% (24)

Why each option

The provided Python code segment contains a fundamental error in its exception handling structure, making the second `except` block unreachable.

AYes

The statement is false because the code contains a structural error where the second `except` block is unreachable, leading to incorrect error handling logic.

BNoCorrect

The code has a logical flaw in its exception handling structure: the second `except Exception as e:` block directly following the first `try-except` block will never be executed. If an exception occurs within the initial `try` block, it will be caught by the first `except Exception as e:`, making the subsequent `except` block unreachable according to Python's exception handling rules.

Concept tested: Python try-except block execution flow

Topics

#Delta Lake#Data Merging#Upsert#Conditional Writing

Community Discussion

No community discussion yet for this question.

Full DP-700 Practice