Databricks Certified Associate Developer for Apache Spark — Question 89
The code block shown below contains an error. The code block is intended to return a new DataFrame from DataFrame storesDF where column storeId is of the type string. Identify the error.
Code block:
storesDF.withColumn(“storeId”, cast(col(“storeId”), StringType()))
Answer options
- A. Calls to withColumn() cannot create a new column of the same name on which it is operating.
- B. DataFrame columns cannot be converted to a new type inside of a call to withColumn().
- C. The call to StringType should not be followed by parentheses.
- D. The column name storeId inside the col() operation should not be quoted.
- E. The cast() operation is a method in the Column class rather than a standalone function.
Correct answer: E
Explanation
The correct answer is E because the cast() operation is indeed a method associated with the Column class and cannot be used as a standalone function. The other options are incorrect because withColumn() can create new columns with the same name, DataFrame columns can be cast to new types, StringType requires parentheses, and col() requires the column name to be in quotes.