DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #81
Which of the following code blocks returns a DataFrame containing a column month, an integer representation of the day of the year from column openDate from DataFrame storesDF. Note that column…
The correct answer is D. 1. "Timestamp". Option D is correct because converting a UNIX epoch integer requires casting to "timestamp" (not "date"), since Spark's timestamp type correctly interprets the value as seconds since the Unix epoch - enabling subsequent date functions to work properly. After creating…
Question
Which of the following code blocks returns a DataFrame containing a column month, an integer representation of the day of the year from column openDate from DataFrame storesDF. Note that column openDate is of type integer and represents a date in the UNIX epoch format - the number of seconds since midnight on January 1st, 1970. A sample of storesDF is displayed below:
Code block:
stored.withColumn("openTimestamp”, col("openDate").cast(1)) .withColumn(2, 3(4))
Options
- A
- "Data"
- B
- "Timestamp"
- C
- "Timestamp"
- D
- "Timestamp"
How the community answered
(46 responses)- A9% (4)
- B13% (6)
- C4% (2)
- D74% (34)
Explanation
Option D is correct because converting a UNIX epoch integer requires casting to "timestamp" (not "date"), since Spark's timestamp type correctly interprets the value as seconds since the Unix epoch - enabling subsequent date functions to work properly. After creating openTimestamp, the correct function to extract the day of the year as an integer is dayofyear(), applied to col("openTimestamp"), not to the original openDate column.
Why the distractors fail:
- Option A uses
"Data"as the cast type, which is not a valid Spark DataType - this would throw a runtime error. - Option B likely uses the correct cast but applies a wrong function such as
month()or references the wrong column (col("openDate")), which hasn't been converted yet. - Option C likely uses
dayofmonth()instead ofdayofyear()- these sound similar but return different things (day 1–31 vs. day 1–366).
Memory tip: Think of it as a two-step pipeline - cast first, extract second. The cast must be to "timestamp" (epoch-aware), and the extraction function must match exactly what's asked: dayofyear = day of the year (1–366), not day of the month or month number.
Topics
Community Discussion
No community discussion yet for this question.