Databricks Certified Associate Developer for Apache Spark — Question 92
Which of the following code blocks returns a new DataFrame where column division is the first two characters of column division in DataFrame storesDF?
Answer options
- A. storesDF.withColumn(“division”, substr(col(“division”), 0, 2))
- B. storesDF.withColumn(“division”, susbtr(col(“division”), 1, 2))
- C. storesDF,withColumn(“division”, col(“division”).substr(0, 3))
- D. storesDF.withColumn(“division”, col(“division”).substr(0, 2))
- E. storesDF.withColumn(“division”, col(“division”).substr(l, 2))
Correct answer: E
Explanation
The correct answer is E because it correctly uses the `substr` function to extract the first two characters from the 'division' column, starting from index 1. Option A incorrectly starts from index 0, option B has a typo in 'susbtr', option C extracts three characters instead of two, and option D correctly extracts two characters but does not use the correct starting index.