Spark SQL & DataFrames Questions with Answers

12 practice questions on Spark SQL & DataFrames (Big Data (Hadoop & Spark)) with answers and step-by-step solutions. Try 10 samples below, then practise all 12 free in the AptiNest app.

Sample questions

Question 1Easy

Spark DataFrames differ from RDDs mainly because they:

  1. Have a named-column schema and use the Catalyst optimizer
  2. Cannot be distributed
  3. Are always stored on disk by default
  4. Support only numeric data for every job

Answer: A. Have a named-column schema and use the Catalyst optimizer

Explanation

Schema information lets Spark optimise queries; RDDs are opaque objects.

Step-by-step solution

  1. A DataFrame has named columns and a schema of types.
  2. Because Spark knows the schema, the Catalyst optimizer can rewrite the query plan.
  3. An RDD has no schema, so Spark cannot look inside its functions to optimise them.
  4. So the main difference is the named-column schema with Catalyst (option A).

Why the other options are wrong

  • B. DataFrames can be distributed across the cluster, just like RDDs.
  • C. DataFrames are not stored on disk by default; storage depends on caching and use.
  • D. DataFrames support many data types, not only numeric ones.

Tip: Schema plus Catalyst gives optimised queries; prefer DataFrames for structured work.

Question 2Easy

A Spark DataFrame is best described as

  1. A distributed collection of rows organised into named columns
  2. A single-node spreadsheet that has to fit in the memory of a single machine
  3. A stream of bytes
  4. A graph of vertices only

Answer: A. A distributed collection of rows organised into named columns

Explanation

A DataFrame adds schema and lets the Catalyst optimiser plan queries.

Step-by-step solution

  1. Rows plus named columns.
  2. Has a schema.
  3. Optimised by Catalyst.

Why the other options are wrong

  • B. It is distributed.
  • C. It has a schema.
  • D. That is a graph abstraction.

Question 3Medium

Which format is columnar and commonly used with Spark for efficient analytics?

  1. CSV
  2. Plain text log
  3. Parquet
  4. XML

Answer: C. Parquet

Explanation

Columnar layout allows column pruning and better compression; CSV is row-oriented text.

Step-by-step solution

  1. Parquet stores data column by column, not row by row.
  2. Column layout lets Spark read only the columns a query needs.
  3. Values of one column are similar, so they compress well.
  4. So the columnar format is Parquet (option C).

Why the other options are wrong

  • A. CSV is row-oriented text.
  • B. A plain text log is row-oriented text with no column layout.
  • D. XML is a row-oriented, nested text format.

Tip: Row formats such as CSV suit raw data; columnar formats such as Parquet suit analytics.

Question 4Medium

What does the Catalyst optimiser do in Spark SQL?

  1. Stores data in HDFS and writes the result tables to the metastore
  2. Rewrites logical plans (for example pushing filters down) into efficient physical plans
  3. Replaces the cluster manager
  4. Encrypts queries

Answer: B. Rewrites logical plans (for example pushing filters down) into efficient physical plans

Explanation

It applies rule-based and cost-based optimisations such as predicate pushdown and column pruning.

Step-by-step solution

  1. Parse the query.
  2. Optimise the logical plan.
  3. Pick a physical plan.

Why the other options are wrong

  • A. Storage is separate.
  • C. Not its role.
  • D. Not its function.

Question 5Medium

Why do DataFrames usually perform better than raw RDDs for structured data?

  1. They avoid using memory because they hold fewer rows in memory than an RDD does
  2. They do not need executors
  3. They carry a schema, enabling optimisation and compact binary storage
  4. They are not lazy

Answer: C. They carry a schema, enabling optimisation and compact binary storage

Explanation

Knowing the schema lets Spark optimise the plan and manage memory efficiently.

Step-by-step solution

  1. Schema known upfront.
  2. Catalyst optimises.
  3. Tungsten stores compactly.

Why the other options are wrong

  • A. They use memory efficiently, not none.
  • B. They run on executors.
  • D. They are lazy as well.

Question 6Medium

Which of the following will usually trigger a shuffle in Spark SQL?

  1. select('a'), a column projection
  2. filter(col > 5), a row filter
  3. withColumn('b', col+1), a per-row expression
  4. groupBy().count()

Answer: D. groupBy().count()

Explanation

Aggregations by key need rows with the same key to meet in one partition.

Step-by-step solution

  1. Narrow: row-local ops.
  2. groupBy needs same keys together.
  3. So a shuffle.

Why the other options are wrong

  • A. Projection is narrow and row-local.
  • B. Filtering is narrow and partition-local.
  • C. A per-row expression is narrow.

Question 7Medium

In the DataFrame API, df.filter(df.age > 30).select('name') will compute

  1. Immediately after filter is called because transformations in Spark always execute eagerly on the driver
  2. Only on the driver machine
  3. Only after an action triggers it, since both calls are lazy
  4. Never, since select is invalid

Answer: C. Only after an action triggers it, since both calls are lazy

Explanation

Both filter and select build the plan; show(), count() or write start execution.

Step-by-step solution

  1. filter: lazy.
  2. select: lazy.
  3. Action needed to run.

Why the other options are wrong

  • A. Filter is a lazy transformation.
  • B. It runs distributed on executors.
  • D. select is valid.

Question 8Medium

Which is the best way to join a very large table with a very small one in Spark SQL?

  1. Shuffle both tables with sort-merge always for every job
  2. Collect the large table to the driver in current releases
  3. Cache the large table only
  4. Broadcast the small table to every executor

Answer: D. Broadcast the small table to every executor

Explanation

A broadcast join avoids shuffling the large table.

Step-by-step solution

  1. A broadcast join copies the small table to every executor.
  2. Each executor joins its local part of the large table with the copy.
  3. The large table is not shuffled, so the network cost stays low.
  4. So the best choice is to broadcast the small table (option D).

Why the other options are wrong

  • A. A sort-merge join shuffles both tables, which is costly when one table is small.
  • B. Collecting the large table to the driver would overload the driver's memory.
  • C. Caching the large table does not avoid the shuffle needed for the join.

Tip: If the small table fits in executor memory, use a broadcast join.

Question 9Hard

In Spark, what does the Catalyst optimizer do for DataFrame queries?

  1. Encrypts data between nodes on each node
  2. Chooses the HDFS replication factor in current releases
  3. Rewrites the logical plan, e.g. predicate pushdown and column pruning
  4. Restarts failed executors

Answer: C. Rewrites the logical plan, e.g. predicate pushdown and column pruning

Explanation

Catalyst applies rule-based and cost-based optimisations before generating physical plans.

Step-by-step solution

  1. Catalyst is the optimizer behind Spark SQL and DataFrames.
  2. It works on the logical plan of a query.
  3. It applies rules such as predicate pushdown and column pruning.
  4. So Catalyst rewrites the logical plan (option C).

Why the other options are wrong

  • A. Encrypting data between nodes is not Catalyst's job.
  • B. The HDFS replication factor is a storage setting, not a Catalyst task.
  • D. Restarting failed executors is part of cluster and fault recovery, not Catalyst.

Tip: Catalyst is the query optimizer: filter early and read only the columns you need.

Question 10Hard

A small dimension table is joined with a huge fact table in Spark. Which join strategy usually works best?

  1. Sort-merge join with a full shuffle of both tables, because every key is sorted before the join starts
  2. Broadcast hash join, sending the small table to all executors
  3. Cartesian product
  4. Collect the big table to the driver

Answer: B. Broadcast hash join, sending the small table to all executors

Explanation

Broadcasting the small table avoids shuffling the large one.

Step-by-step solution

  1. One side is small.
  2. Broadcast it to executors.
  3. Join locally, no big shuffle.

Why the other options are wrong

  • A. More expensive when one side is small.
  • C. Explodes the row count.
  • D. Would exhaust driver memory.

Practise all 12 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Big Data (Hadoop & Spark) topics · All topics