Databricks-Spark-Assoc Developing DataFrame/DataSet API Applications Practice Question
A developer needs to add a column `full_name` to DataFrame `people` by concatenating `first_name` and `last_name` with a single space, and must handle rows where `last_name` is null by producing just the first name. Which expression is correct?
⚠ Common exam trap
The trap here is assuming `concat` and `concat_ws` behave identically with nulls, when only `concat_ws` skips them.
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
✓
people.withColumn("full_name", concat_ws(" ", col("first_name"), col("last_name")))
`concat_ws` inserts the separator between non-null arguments and treats nulls as absent, so a null last name produces a clean first-name-only value with no dangling space. That matches the requirement for both the populated and the null case in one expression, without needing any explicit conditional branching.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
people.withColumn("full_name", col("first_name") + lit(" ") + col("last_name"))
Why it's wrong here
The `+` operator on string columns is not string concatenation in Spark SQL; it attempts numeric addition and raises an analysis error on string types. Even in dialects that allow it, a null `last_name` would propagate null through the entire expression, producing a null full_name instead of falling back to the first name.
- ✗
people.withColumn("full_name", when(col("last_name").isNull(), col("first_name")).otherwise(col("first_name")))
Why it's wrong here
Both branches of the `when`/`otherwise` return only `first_name`, so `last_name` is never appended in any case. The expression compiles and runs but silently drops the last name for every row, producing incorrect data rather than an error, which makes it a subtle but definite failure here.
- ✓
people.withColumn("full_name", concat_ws(" ", col("first_name"), col("last_name")))
Why this is correct
`concat_ws` joins its arguments with the given separator and, importantly, skips null arguments rather than returning null. So a null `last_name` yields just the first name with no trailing space, which is exactly the null-handling behavior required. It also avoids the awkward empty-string artifacts that string addition can introduce.
- ✗
people.withColumn("full_name", concat(col("first_name"), lit(" "), col("last_name")))
Why it's wrong here
`concat` returns null if any input is null, so a missing `last_name` makes the whole result null rather than just the first name, violating the stated requirement. It also always inserts the separator, so even a non-null path is fine but the null path is wrong, making this unsuitable for the scenario.
About these practice questions
Courseiva writes every Databricks-Spark-Assoc question from scratch — 295 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
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.