nerdexam
Databricks

DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #86

Which of the following code blocks reads a CSV at the file path filePath into a Data Frame with the specified schema schema?

The correct answer is C. spark.read.schema(schema).csv(filePath). Option C is correct because in PySpark, spark.read is a property (a DataFrameReader object), not a method - so it takes no parentheses. The .schema() method accepts a StructType variable (here schema), not a string, followed by .csv(filePath) to specify the source format and…

Data Loading and I/O Operations with Apache Spark

Question

Which of the following code blocks reads a CSV at the file path filePath into a Data Frame with the specified schema schema?

Options

  • Aspark.read().csv(filePath)
  • Bspark.read().schema("schema").csv(filePath)
  • Cspark.read.schema(schema).csv(filePath)
  • Dspark.read.schema("schema").csv(filePath)
  • Espark.read().schema(schema).csv(filePath)

How the community answered

(47 responses)
  • B
    2% (1)
  • C
    94% (44)
  • E
    4% (2)

Explanation

Option C is correct because in PySpark, spark.read is a property (a DataFrameReader object), not a method - so it takes no parentheses. The .schema() method accepts a StructType variable (here schema), not a string, followed by .csv(filePath) to specify the source format and path.

Why the distractors fail:

  • A & B & E: spark.read() with parentheses is a TypeError - read is a property, not callable.
  • B & D: Passing "schema" (a string literal) to .schema() instead of the actual StructType variable schema will either fail or be misinterpreted.
  • A: Also missing .schema() entirely, so no schema is applied at all.

Memory tip: Think "read is free, schema needs the variable" - spark.read has no (), and .schema() wants the real object, not its name in quotes.

Topics

#Spark DataFrameReader#CSV I/O#Schema Definition#Spark API Syntax

Community Discussion

No community discussion yet for this question.

Full DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK Practice