databricks Associate Developer for Apache Spark · Practice questions

Section 3: Developing Apache Spark DataFrame/DataSet API Applications Practice Questions

Review 10 sample questions with their correct answers and explanations. Sign up free to continue with the full topic quiz.

Sign up free for the full quiz

Question 1

Given: df = spark.createDataFrame([(1, ['x', 'y']), (2, [])], 'id INT, tags ARRAY<STRING>') How many rows does df.select('id', explode('tags')).count() return?

  1. 1
  2. 2 — Correct
  3. 0
  4. 3

Correct answer

2

Explanation

explode produces one row per element and drops rows with empty arrays: id 1 yields 2 rows, id 2 yields none.

Question 2

Which code saves a DataFrame as a persistent table bucketed by customer_id into 16 buckets and sorted by customer_id within buckets?

  1. df.write.bucketBy('customer_id', 16).orderBy('customer_id').save('sales_b', format='parquet')
  2. df.write.sortBy('customer_id').bucketBy(16).parquet('sales_b')
  3. df.write.bucketBy(16, 'customer_id').sortBy('customer_id').saveAsTable('sales_b') — Correct
  4. df.write.partitionBy('customer_id').sortBy(16).saveAsTable('sales_b')

Correct answer

df.write.bucketBy(16, 'customer_id').sortBy('customer_id').saveAsTable('sales_b')

Explanation

bucketBy(numBuckets, cols) and sortBy(cols) work with saveAsTable, which records bucketing metadata in the catalog.

Question 3

Which code counts rows where the column email is NULL?

  1. df.count(col('email').isNull())
  2. df.where('email = NULL').count()
  3. df.filter(col('email').isNull()).count() — Correct
  4. df.filter(col('email') == None).select('email').count()

Correct answer

df.filter(col('email').isNull()).count()

Explanation

isNull() tests for NULL. Comparing with None using == produces NULL, which filters out all rows.

Question 4

Which code performs an inner join of orders and customers on customer_id?

  1. orders.innerJoin(customers, 'customer_id')
  2. orders.join(customers, 'customer_id', 'inner') — Correct
  3. orders.merge(customers, on='customer_id')
  4. orders.merge(customers, on='customer_id', how='inner')

Correct answer

orders.join(customers, 'customer_id', 'inner')

Explanation

join(other, on, how) with a column name joins on equal values and keeps a single customer_id column. merge is a pandas method.

Question 5

Given: df = spark.createDataFrame([(1,), (1,), (2,)], ['v']) What does df.union(df).distinct().count() return?

  1. 2 — Correct
  2. 4
  3. 3
  4. 6

Correct answer

2

Explanation

union keeps duplicates (6 rows), and distinct reduces them to the unique values 1 and 2.

Question 6

Given the same DataFrame with tags [ 'x', 'y' ] for id 1 and [] for id 2, how many rows does df.select('id', explode_outer('tags')).count() return?

  1. 1
  2. 3 — Correct
  3. 4
  4. 2

Correct answer

3

Explanation

explode_outer keeps rows with empty or NULL arrays, producing a NULL element: 2 rows for id 1 plus 1 row for id 2.

Question 7

Which code reads all Parquet files under /data/sales/ including subdirectories partitioned as year=2026/month=11?

  1. spark.read.parquet('/data/sales/year=*/month=*/', partitions=False)
  2. spark.read.format('partitioned').load('/data/sales/')
  3. spark.read.parquet('/data/sales/') — Correct
  4. spark.read.text('/data/sales/')

Correct answer

spark.read.parquet('/data/sales/')

Explanation

Reading the base path discovers partition directories and adds year and month as columns.

Question 8

Which code shows the DataFrame contents without truncating long strings?

  1. df.show(truncate=False) — Correct
  2. df.show(n=20, full=True, vertical=False)
  3. df.display(truncate=0)
  4. df.print(False)

Correct answer

df.show(truncate=False)

Explanation

show truncates strings at 20 characters by default; truncate=False shows full values.

Question 9

Which code caches a DataFrame using memory only, serialized off-heap is not required?

  1. df.cache(StorageLevel.MEMORY_ONLY).count()
  2. df.persist(StorageLevel.MEMORY_ONLY) — Correct
  3. df.persist('memory')
  4. df.store(MEMORY_ONLY)

Correct answer

df.persist(StorageLevel.MEMORY_ONLY)

Explanation

persist accepts a StorageLevel. cache() takes no arguments.

Question 10

Which code computes the number of days between columns start and end?

  1. months_between(col('start'))
  2. date_sub(col('end'), col('start'))
  3. datediff(col('end'), col('start')) — Correct
  4. col('end') - col('start').days

Correct answer

datediff(col('end'), col('start'))

Explanation

datediff(end, start) returns the day difference as an integer.

Ready for the full quiz?

Create a free QuizAnalyst account to practice the complete question set and save your progress.

Sign up free for the full quiz