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
- 2 — Correct
- 0
- 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?
- df.write.bucketBy('customer_id', 16).orderBy('customer_id').save('sales_b', format='parquet')
- df.write.sortBy('customer_id').bucketBy(16).parquet('sales_b')
- df.write.bucketBy(16, 'customer_id').sortBy('customer_id').saveAsTable('sales_b') — Correct
- 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?
- df.count(col('email').isNull())
- df.where('email = NULL').count()
- df.filter(col('email').isNull()).count() — Correct
- 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?
- orders.innerJoin(customers, 'customer_id')
- orders.join(customers, 'customer_id', 'inner') — Correct
- orders.merge(customers, on='customer_id')
- 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?
- 2 — Correct
- 4
- 3
- 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
- 3 — Correct
- 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?
- spark.read.parquet('/data/sales/year=*/month=*/', partitions=False)
- spark.read.format('partitioned').load('/data/sales/')
- spark.read.parquet('/data/sales/') — Correct
- 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?
- df.show(truncate=False) — Correct
- df.show(n=20, full=True, vertical=False)
- df.display(truncate=0)
- 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?
- df.cache(StorageLevel.MEMORY_ONLY).count()
- df.persist(StorageLevel.MEMORY_ONLY) — Correct
- df.persist('memory')
- 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?
- months_between(col('start'))
- date_sub(col('end'), col('start'))
- datediff(col('end'), col('start')) — Correct
- col('end') - col('start').days
Correct answer
datediff(col('end'), col('start'))
Explanation
datediff(end, start) returns the day difference as an integer.