Courseiva

Databricks-Spark-Assoc Developing DataFrame/DataSet API Applications Practice Question

You need to add a new calculated column 'discounted_price' to an existing DataFrame 'df' by multiplying 'price' by 0.9. Which DataFrame transformation accomplishes this correctly?

⚠ Common exam trap

Candidates often try to modify the DataFrame in place, forgetting that Spark DataFrames are immutable; they fail to assign the result of 'withColumn' back to a variable.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

df.withColumn('discounted_price', col('price') * 0.9)

Adding columns immutably is a core pattern in Spark DataFrame operations. Using the withColumn method creates a new DataFrame reference with the added transformation while leaving the original dataset unchanged, adhering to functional programming principles required for distributed data processing.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    df.addColumn('discounted_price', col('price') * 0.9)

    Why it's wrong here

    PySpark DataFrames expose withColumn, not addColumn; no such method exists on DataFrame, so the call fails at runtime. An addColumn-style API belongs to other frameworks, and would be correct only where that library's DataFrame interface is genuinely in use.

  • ✗

    df.update('discounted_price', df.price * 0.9)

    Why it's wrong here

    Spark DataFrames are immutable structures, meaning they do not support an update method to modify existing datasets in place. Transformations must return a brand new DataFrame instance rather than mutating existing data representations.

  • ✓

    df.withColumn('discounted_price', col('price') * 0.9)

    Why this is correct

    withColumn returns a new DataFrame with the added or replaced column, and multiplying the price column by 0.9 computes the discounted value per row. This is the standard immutable transformation, matching the requirement to add discounted_price without mutating the original DataFrame.

  • ✗

    df.select(col('*'), col('price') * 0.9 as 'discounted_price')

    Why it's wrong here

    Python syntax does not support the SQL 'as' keyword directly inside method arguments like this. While select with alias can achieve a similar result, this specific syntax raises a syntax error in standard Python code execution.

About these practice questions

This Databricks-Spark-Assoc question is part of Courseiva's 295-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Databricks exam blueprint

This Databricks-Spark-Assoc practice question is part of Courseiva's free Databricks certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the Databricks-Spark-Assoc exam.