PySpark Online Test: 30 Spark Quiz Questions with Answers (MCQ)
Want to check how well you really know PySpark? This PySpark online test has 30 multiple-choice questions covering the basics, Spark architecture and internals, and "what does this code print" questions that catch common mistakes. Every question has a hidden answer with a short explanation.
It is free, there is no sign-up, and it takes about 15 minutes. If you want to check an answer yourself, paste any code question into the free PySpark online compiler and run it.
How to Take This Spark Quiz
- Read each question and pick A, B, C or D before opening the answer.
- Click Show answer to see the correct option and why it is correct.
- Keep a tally of your correct answers and check your score at the end.
- Sections get harder as you go: Basics (Q1 to Q10), Architecture and Internals (Q11 to Q20), Code Output and Performance (Q21 to Q30).
Section 1: PySpark Basics (Q1 to Q10)
Q1. Which of the following is an action in PySpark?
- A.
count() - B.
filter() - C.
select() - D.
withColumn()
Show answer
Answer: A. count()
count() is an action: it triggers a Spark job and returns a result to the driver. filter, select and withColumn are lazy transformations that only build the plan. Read more in transformations and actions.
Q2. What does lazy evaluation mean in Spark?
- A. Spark processes data one row at a time
- B. Spark caches every DataFrame automatically
- C. Spark runs jobs only when the cluster is idle
- D. Transformations are only executed when an action is called
Show answer
Answer: D. Transformations are only executed when an action is called
Spark records transformations in a plan (a DAG) and only runs them when an action such as count, show or write is called. This lets the optimizer see the whole query before running it. See lazy evaluation in Spark.
Q3. Which method brings all rows of a DataFrame back to the driver?
- A.
show() - B.
take(10) - C.
collect() - D.
limit(10)
Show answer
Answer: C. collect()
collect() returns every row to the driver as a Python list, which can crash the driver with an OutOfMemoryError on large data. show, take and limit only return a small number of rows.
Q4. What is the default value of spark.sql.shuffle.partitions?
- A. 8
- B. 64
- C. 200
- D. Equal to the number of executor cores
Show answer
Answer: C. 200
The default is 200 partitions after every shuffle (joins, groupBy, distinct). With Adaptive Query Execution enabled, Spark can automatically merge small post-shuffle partitions.
Q5. Which join type returns only the rows from the left DataFrame that have NO match in the right DataFrame?
- A.
left_anti - B.
left - C.
left_semi - D.
outer
Show answer
Answer: A. left_anti
left_anti keeps left rows without a match (like NOT EXISTS in SQL). left_semi does the opposite and keeps left rows that do have a match. See joining data in PySpark.
Q6. Which DataFrame method replaces null values with a constant you choose?
- A.
dropna() - B.
distinct() - C.
isNull() - D.
fillna()
Show answer
Answer: D. fillna()
fillna() (also available as df.na.fill()) replaces nulls, for example df.fillna({"city": "Unknown"}). dropna() removes rows with nulls instead. More in handling nulls.
Q7. Which of these file formats is columnar?
- A. CSV
- B. JSON
- C. Parquet
- D. Avro
Show answer
Answer: C. Parquet
Parquet stores data column by column, so Spark reads only the columns a query needs and compresses data much better. CSV, JSON and Avro are row-based. See common file formats.
Q8. What does inferSchema=True do when reading a CSV file?
- A. Scans the data to detect column types, which costs an extra pass over the file
- B. Reads every column as a string
- C. Uses the schema stored inside the CSV header
- D. Validates the data against an existing table schema
Show answer
Answer: A. Scans the data to detect column types, which costs an extra pass over the file
With inferSchema=True Spark reads the data to guess types such as int, double or timestamp. This is convenient but slower on big files, so production jobs usually pass an explicit schema. See inferSchema in PySpark.
Q9. What is the main entry point for a PySpark application since Spark 2.0?
- A.
SparkContext - B.
SQLContext - C.
HiveContext - D.
SparkSession
Show answer
Answer: D. SparkSession
SparkSession combines the older SQLContext and HiveContext and gives access to the SparkContext. You create it with SparkSession.builder.getOrCreate(). See setting up a Spark session.
Q10. What is the difference between cache() and persist()?
- A. cache() stores data on disk, persist() stores it in memory
- B. cache() works only on RDDs, persist() only on DataFrames
- C. persist() lets you choose the storage level, cache() uses the default level
- D. There is no difference; persist() is deprecated
Show answer
Answer: C. persist() lets you choose the storage level, cache() uses the default level
Both keep a computed DataFrame for reuse. cache() uses the default storage level, while persist() accepts a level such as MEMORY_ONLY, MEMORY_AND_DISK or DISK_ONLY. Both are lazy and take effect on the next action.
Section 2: Spark Architecture and Internals (Q11 to Q20)
Q11. Which component turns your code into jobs, stages and tasks and schedules them?
- A. Executor
- B. Cluster manager
- C. Worker node
- D. Driver
Show answer
Answer: D. Driver
The driver builds the logical and physical plans, splits work into jobs, stages and tasks, and sends tasks to executors. The cluster manager only allocates resources. See Spark architecture.
Q12. When does Spark start a new stage within a job?
- A. At every shuffle boundary (wide transformation)
- B. After every transformation
- C. Whenever cache() is called
- D. After every 200 tasks
Show answer
Answer: A. At every shuffle boundary (wide transformation)
Stages are split at shuffle boundaries. All narrow transformations between two shuffles run together in one stage. See jobs, stages and tasks.
Q13. How many tasks does Spark run in a stage?
- A. One per executor
- B. One per CPU core in the cluster
- C. One per partition
- D. One per column
Show answer
Answer: C. One per partition
Each task processes exactly one partition, so a stage reading 10 partitions runs 10 tasks. Executor cores decide how many of those tasks run at the same time.
Q14. Which of these is a wide transformation that causes a shuffle?
- A.
groupBy().agg() - B.
filter() - C.
select() - D.
withColumn()
Show answer
Answer: A. groupBy().agg()
groupBy needs all rows with the same key in the same partition, so data is shuffled across the network. filter, select and withColumn are narrow: each output partition depends on one input partition.
Q15. Which feature helps Spark recover lost RDD partitions?
- A. Replication
- B. Compression
- C. Scheduling
- D. Lineage
Show answer
Answer: D. Lineage
Spark keeps the lineage (the chain of transformations) of every RDD and DataFrame. If a partition is lost, Spark recomputes only that partition from its lineage instead of relying on replicated copies. See RDDs in Spark.
Q16. What is the correct order of the Catalyst optimizer phases?
- A. Physical planning, Analysis, Code generation, Logical optimization
- B. Logical optimization, Analysis, Code generation, Physical planning
- C. Analysis, Logical optimization, Physical planning, Code generation
- D. Code generation, Physical planning, Logical optimization, Analysis
Show answer
Answer: C. Analysis, Logical optimization, Physical planning, Code generation
Catalyst first resolves names (analysis), applies rule-based rewrites such as predicate pushdown (logical optimization), picks a physical plan using a cost model (physical planning), and finally generates JVM bytecode (code generation). See Catalyst optimizer.
Q17. Which of the following can Adaptive Query Execution (AQE) do at runtime?
- A. Coalesce small shuffle partitions
- B. Switch a sort-merge join to a broadcast join
- C. Split skewed partitions in a join
- D. All of the above
Show answer
Answer: D. All of the above
AQE uses real statistics collected after each shuffle stage to merge small partitions, change join strategies and handle skewed joins. It is enabled by default since Spark 3.2. See Adaptive Query Execution.
Q18. What is the default value of spark.sql.autoBroadcastJoinThreshold?
- A. 1 MB
- B. 10 MB
- C. 100 MB
- D. 1 GB
Show answer
Answer: B. 10 MB
Tables smaller than 10 MB (by Spark's size estimate) are broadcast automatically. You can force it with F.broadcast(df) or disable automatic broadcasting by setting the threshold to -1. See join strategies.
Q19. By default, what fraction of executor heap (after reserved memory) is unified memory shared by execution and storage?
- A. 0.4
- B. 0.5
- C. 0.6
- D. 0.8
Show answer
Answer: C. 0.6
spark.memory.fraction defaults to 0.6. Execution memory (shuffles, joins, sorts) and storage memory (cache, broadcast) share this region, and the remaining 40% is user memory. See executor memory.
Q20. Which cluster manager is NOT supported in Spark 4.0?
- A. Apache Mesos
- B. YARN
- C. Kubernetes
- D. Spark Standalone
Show answer
Answer: A. Apache Mesos
Mesos support was deprecated in Spark 3.2 and removed in Spark 4.0. YARN, Kubernetes and Spark's built-in standalone manager are supported.
Section 3: Code Output and Performance (Q21 to Q30)
These questions show a short snippet. Work out the output in your head first, then run it in the online compiler if you want to check. All snippets assume from pyspark.sql import functions as F, Window and an existing spark session.
Q21. What does this code print?
1df = spark.createDataFrame(
2 [(1, "IT", 100), (2, "HR", 80), (3, "IT", 120), (4, "Ops", 90), (5, "IT", 70)],
3 ["id", "dept", "salary"])
4
5print(df.filter((F.col("dept") == "IT") & (F.col("salary") >= 100)).count())- A. 1
- B. 2
- C. 3
- D. 5
Show answer
Answer: B. 2
Only rows 1 (IT, 100) and 3 (IT, 120) match both conditions. Row 5 is in IT but its salary is 70. Remember to wrap each condition in brackets when combining them with & or |.
Q22. What rank does employee "d" (salary 80) get?
1s = spark.createDataFrame([("a", 100), ("b", 90), ("c", 90), ("d", 80)], ["name", "salary"])
2w = Window.orderBy(F.desc("salary"))
3s.withColumn("r", F.rank().over(w)).show()- A. 2
- B. 3
- C. 4
- D. 5
Show answer
Answer: C. 4
rank() gives ties the same rank and then leaves a gap: 100 gets 1, both 90s get 2, and 80 gets 4. dense_rank() would give 3, and row_number() would also give 4 but never repeats a value.
Q23. What does this code print?
1print(spark.range(100).repartition(4).coalesce(10).rdd.getNumPartitions())- A. 4
- B. 10
- C. 40
- D. It raises an error
Show answer
Answer: A. 4
coalesce() can only reduce the number of partitions because it merges existing ones without a full shuffle. Asking for more partitions than exist leaves the count unchanged. Use repartition(10) to increase it. See repartition and coalesce.
Q24. How many rows does this left join return?
1left = spark.createDataFrame([(1, "a"), (2, "b"), (3, "c")], ["id", "l"])
2right = spark.createDataFrame([(1, "x"), (1, "y"), (4, "z")], ["id", "r"])
3
4print(left.join(right, "id", "left").count())- A. 2
- B. 3
- C. 4
- D. 5
Show answer
Answer: C. 4
A left join keeps every left row, but a left row is repeated once for every match. id 1 matches two rows on the right, so it appears twice; ids 2 and 3 appear once with nulls. That is 4 rows. Duplicate keys on the right are a common cause of row explosions after joins.
Q25. What does this code print?
1n = spark.createDataFrame([(1,), (2,), (None,)], "x int")
2print(n.filter(F.col("x") != 1).count())- A. 0
- B. 1
- C. 2
- D. 3
Show answer
Answer: B. 1
Comparing null with anything returns null, not true, so the null row is silently dropped by the filter. Only the row with 2 is kept. To keep nulls, use (F.col("x") != 1) | F.col("x").isNull(), or a null-safe comparison.
Q26. How many rows does the grouped result have?
1g = spark.createDataFrame([("a",), ("b",), (None,), (None,), ("a",)], "k string")
2print(g.groupBy("k").count().count())- A. 2
- B. 3
- C. 4
- D. 5
Show answer
Answer: B. 3
groupBy treats null as its own group, so the result has three groups: "a", "b" and null. This often surprises people who expect nulls to be ignored.
Q27. What do the two print statements output?
1a = spark.createDataFrame([(1,), (2,)], ["v"])
2b = spark.createDataFrame([(2,), (3,), (3,)], ["v"])
3
4print(a.union(b).count())
5print(a.union(b).distinct().count())- A. 3 and 3
- B. 5 and 3
- C. 5 and 5
- D. 3 and 5
Show answer
Answer: B. 5 and 3
union() behaves like SQL UNION ALL and keeps duplicates, so 2 + 3 = 5 rows. Adding distinct() removes the duplicates, leaving 1, 2 and 3. Also note that union matches columns by position; use unionByName to match by name.
Q28. Which operation reduces the number of partitions WITHOUT a full shuffle?
- A.
repartition(10) - B.
repartition(10, "key") - C.
sortWithinPartitions("key") - D.
coalesce(10)
Show answer
Answer: D. coalesce(10)
coalesce() merges existing partitions on the same executors, so it avoids a full shuffle. It is the usual way to reduce the number of output files before a write. repartition() always shuffles.
Q29. In the Spark UI, 199 of 200 tasks finish in seconds but one task runs for 30 minutes. What is the most likely cause?
- A. Too much executor memory
- B. Too many executors
- C. Data skew
- D. A missing cache()
Show answer
Answer: C. Data skew
One task processing far more data than the others is the classic sign of data skew: one key has many more rows than the rest. Fixes include AQE skew join handling, broadcasting the smaller table, or salting the hot key. See data skew and salting.
Q30. Which option is usually the SLOWEST way to add a computed column?
- A. A regular Python UDF
- B. A built-in function from
pyspark.sql.functions - C. A pandas UDF
- D. A Spark SQL expression with
expr()
Show answer
Answer: A. A regular Python UDF
A regular Python UDF is a black box to the Catalyst optimizer and sends every row between the JVM and a Python worker. Built-in functions and SQL expressions run inside the JVM, and pandas UDFs process data in Arrow batches, so both are much faster.
Check Your Score
- 26 to 30 correct: Excellent. You are ready for most PySpark interview rounds. Sharpen your speed with the PySpark coding interview questions.
- 18 to 25 correct: Good foundation. Review the topics you missed in the Spark theory tutorials and work through the 67 PySpark interview questions.
- 10 to 17 correct: You know the basics. Go through the PySpark tutorials and try a few PySpark projects for practice.
- Below 10: Start with the introduction to PySpark and keep the PySpark cheat sheet open while you practise.
FAQ
Is this PySpark online test free?
Yes. The test is completely free with no sign-up. You can also run every code question in the free PySpark online compiler.
What topics does this Spark quiz cover?
PySpark DataFrame basics, joins and nulls, Spark architecture (driver, executors, jobs, stages, tasks), lazy evaluation, Catalyst and AQE, memory, partitioning, data skew, and common code-output gotchas that come up in interviews.
How can I prepare for a PySpark online assessment?
- Most company assessments mix multiple-choice theory questions like the ones above with a live coding task. Practise both: revise the interview questions for theory and solve problems in the coding question bank to build speed.