Spark Associate Sample Questions

About these questions

These five questions are original. We wrote them in the style of the Databricks Spark Associate Developer exam objectives, using PySpark as the exam does. They are not real exam questions, and the real exam will word things differently. Each question has one correct answer. They cover five of the seven domains, with the biggest domain, the DataFrame API, first.

How to use these

Try each question without notes, and choose an answer before you open the explanation. Then read the explanation even if you were right, because the reason the closest wrong option fails is where you learn the most. If you slip on a domain, revisit it on the domains explained page and try the code in a notebook.

Question 1 (Developing Apache Spark DataFrame/DataSet API Applications)

A DataFrame named `readings` has the columns `sensor_id`, `value`, and `recorded_at`. A sensor reports many times, and the values differ each time. A developer needs exactly one row per `sensor_id` while keeping all three columns. Which call does this?

  • A. `readings.select(“sensor_id”).distinct()`
  • B. `readings.distinct()`
  • C. `readings.dropDuplicates([“sensor_id”])`
  • D. `readings.dropDuplicates()`
Show the answer and explanation

Correct answer: C. `dropDuplicates` accepts an optional list of columns and compares only those, so each `sensor_id` collapses to one row and the other columns come along. `distinct()`, the closest wrong option, takes no arguments and compares entire rows, so two readings that differ in `value` or `recorded_at` are not duplicates and all of them stay. Calling `dropDuplicates()` without a subset behaves the same way. Option A keeps one row per sensor, but it drops the other two columns. Note that Spark does not guarantee which of the competing rows survives, so if you need the newest one, use a window function instead.

Question 2 (Using Spark SQL)

A data engineer has a DataFrame named `events` and wants to aggregate it with a SQL `GROUP BY`, then add a computed column to the result using the Python DataFrame API. Which approach works?

  • A. Write `events` to Parquet first, because SQL can only read data from storage
  • B. Run `events.createOrReplaceTempView(“events_v”)`, call `spark.sql` on that view, and continue with the DataFrame it returns
  • C. Call `spark.sql` on the DataFrame object directly, which returns nothing, so the work must end there
  • D. Use SQL for the aggregation on a separate engine, and accept that Spark copies the result back each time
Show the answer and explanation

Correct answer: B. A temporary view gives a DataFrame a name that SQL can resolve, without writing any data. `spark.sql` returns a new DataFrame, and it stays lazy, so you can call `withColumn` on it straight away. The closest wrong option is D. SQL and the DataFrame API are two front ends to the same engine and produce the same kind of plan, so there is no separate engine and no copy between them. Option A is wrong because SQL can read a DataFrame through a view without any storage step, and option C is wrong because `spark.sql` returns a DataFrame.

Question 3 (Apache Spark Architecture and Components)

A team reuses a large lookup DataFrame in several jobs. It does not fit comfortably in memory, so the team wants Spark to keep its partitions on executor disk only. Which call achieves this?

  • A. `lookup.cache()`
  • B. `lookup.persist()`
  • C. `lookup.persist(StorageLevel.DISK_ONLY)`
  • D. `lookup.unpersist()`
Show the answer and explanation

Correct answer: C. `persist` takes a `StorageLevel`, and `DISK_ONLY` tells Spark to store the partitions on disk and not in memory. `cache()` and `persist()` without an argument both use the default storage level, so neither lets you ask for disk only. The closest wrong option is B, because persist is the right method, but without the argument you get the default level. Option D does the opposite of what the team wants: it removes the data from storage. Both `persist` and `cache` are lazy, so the data is stored when the first action runs.

Question 4 (Structured Streaming)

A streaming DataFrame has the columns `order_time` (a timestamp) and `category`. A developer needs the number of orders per category in each non-overlapping 15-minute window of event time. Which grouping expresses this?

  • A. `groupBy(col(“order_time”), col(“category”)).count()`
  • B. `groupBy(window(col(“order_time”), “15 minutes”, “5 minutes”), col(“category”)).count()`
  • C. `groupBy(window(col(“order_time”), “15 minutes”), col(“category”)).count()`
  • D. `groupBy(col(“category”)).count().withWatermark(“order_time”, “15 minutes”)`
Show the answer and explanation

Correct answer: C. Putting `window` inside `groupBy` creates event-time windows. With only a duration, the windows are tumbling: each event falls in exactly one window, and windows neither overlap nor leave gaps. Option B is the closest wrong answer. A second duration makes the windows slide, so with 15 minutes sliding every 5, each order is counted in three windows. Option A groups by the exact timestamp, which makes almost every group a single order. Option D misuses the watermark: a watermark limits how long Spark keeps state for late data and does not define windows.

Question 5 (Troubleshooting and Tuning Apache Spark DataFrame API Applications)

A job writes a DataFrame with `.partitionBy(“region”)`. Every region folder ends up with many tiny files, because each of the 200 task partitions holds rows from many regions. Which change before the write puts each region’s rows into a single file while still using many tasks?

  • A. `df.coalesce(1)`
  • B. `df.sortWithinPartitions(“region”)`
  • C. `df.repartition(200)`
  • D. `df.repartition(“region”)`
Show the answer and explanation

Correct answer: D. A partitioned write creates a file for each combination of task partition and partition value. If you repartition by the `region` column, all rows for one region land in the same task partition, so each region folder gets one file, and different regions are still written in parallel. Option A, the closest wrong answer, also gives one file per folder, but it forces the whole write through a single task. Option C repartitions into 200 round-robin partitions, which ignores the column and keeps the many-small-files problem. Option B only orders rows inside each existing partition.

Next step

If these felt comfortable, read the exam format page and think about booking. If a domain felt shaky, try more questions in our Databricks Spark Associate practice tests.