Databricks Certified Associate Developer For Apache Spark 30 · Free Practice Question Easy

Question 11

If we want to create a constant integer 1 as a new column ‘new_column’ in a dataframe df, which code block we should select ?

  • A

    df.withColumn”'new_column”, lit(“1”))

  • B

    df.withColumn(“new_column”, 1)

  • C

    df.withColumnRenamed('new_column', lit(1))

Reveal correct answer

Correct answer:

Explanation

The second argument for DataFrame.withColumn should be a Column so you have to use a literal to add constant value 1:

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need