Bird
Raised Fist0
Apache Sparkdata~20 mins

Transformations vs actions in Apache Spark - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spark Transformations & Actions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Understanding lazy evaluation in Spark
What will be the output of this Spark code snippet?
Apache Spark
val rdd = sc.parallelize(Seq(1, 2, 3, 4))
val mapped = rdd.map(x => x * 2)
// No action called yet
mapped.count()
A4
B8
CNo output, code runs lazily without action
DThrows an error because no action is called
Attempts:
2 left
💡 Hint

Remember that transformations are lazy and actions trigger computation.

❓ data_output
intermediate
2:00remaining
Result of chained transformations and action
Given this Spark code, what is the output of the collect() action?
Apache Spark
val rdd = sc.parallelize(Seq(1, 2, 3, 4, 5))
val filtered = rdd.filter(_ % 2 == 0)
val mapped = filtered.map(_ * 10)
mapped.collect()
A[10, 20]
B[2, 4]
C[1, 3, 5]
D[20, 40]
Attempts:
2 left
💡 Hint

Filter keeps even numbers, then map multiplies by 10.

🔧 Debug
advanced
2:00remaining
Why does this Spark job not run?
Consider this Spark code snippet. Why does it not produce any output or run any job?
Apache Spark
val rdd = sc.parallelize(Seq(1, 2, 3))
rdd.map(_ * 2)
ABecause <code>map</code> is a transformation and no action is called to trigger execution
BBecause <code>map</code> is an action and should run immediately
CBecause the RDD is empty
DBecause SparkContext is not initialized
Attempts:
2 left
💡 Hint

Think about what triggers Spark jobs to run.

🧠 Conceptual
advanced
2:00remaining
Difference between transformations and actions
Which statement correctly describes the difference between transformations and actions in Spark?
ATransformations immediately compute results; actions only define computations
BActions are lazy and transformations trigger execution
CTransformations define a new RDD without executing; actions trigger execution and return results
DBoth transformations and actions trigger execution immediately
Attempts:
2 left
💡 Hint

Recall which operations cause Spark to run jobs.

🚀 Application
expert
3:00remaining
Optimizing Spark job with transformations and actions
You have this Spark code. Which option will minimize the number of Spark jobs executed?
Apache Spark
val rdd = sc.parallelize(Seq(1, 2, 3, 4, 5))
val mapped = rdd.map(_ * 2)
val filtered = mapped.filter(_ > 5)
val countResult = filtered.count()
val collectResult = filtered.collect()
ACall count and collect without caching; Spark will optimize automatically
BCache the filtered RDD before calling count and collect
CCall collect first, then count; order does not matter
DUse two separate RDDs for count and collect to avoid recomputation
Attempts:
2 left
💡 Hint

Think about how caching affects repeated actions on the same RDD.

Practice

(1/5)
1. Which of the following best describes a transformation in Apache Spark?
easy
A. It creates a new dataset from an existing one without running the job immediately.
B. It runs the job and returns the final result immediately.
C. It saves the dataset to an external storage system.
D. It deletes data from the dataset permanently.

Solution

  1. Step 1: Understand what transformations do

    Transformations in Spark create a new dataset from an existing one but do not execute the job immediately.
  2. Step 2: Differentiate from actions

    Actions trigger the execution and return results or save data, unlike transformations.
  3. Final Answer:

    It creates a new dataset from an existing one without running the job immediately. -> Option A
  4. Quick Check:

    Transformation = Lazy evaluation [OK]
Hint: Transformations are lazy; they don't run until an action triggers them [OK]
Common Mistakes:
  • Confusing transformations with actions
  • Thinking transformations execute immediately
  • Believing transformations save or delete data
2. Which of the following is the correct syntax to perform an action that counts the number of elements in an RDD named data?
easy
A. data.map()
B. data.count()
C. data.filter()
D. data.transform()

Solution

  1. Step 1: Identify the action to count elements

    The count() method is an action that returns the number of elements in an RDD.
  2. Step 2: Check syntax correctness

    Calling data.count() correctly triggers the action and returns the count.
  3. Final Answer:

    data.count() -> Option B
  4. Quick Check:

    Action to count = data.count() [OK]
Hint: Actions like count() end with parentheses and return results [OK]
Common Mistakes:
  • Using transformations like map() as actions
  • Calling non-existent methods like transform()
  • Forgetting parentheses on actions
3. Given the following Spark code, what will be the output?
rdd = sc.parallelize([1, 2, 3, 4])
mapped = rdd.map(lambda x: x * 2)
result = mapped.collect()
medium
A. An error because collect() is not an action
B. [1, 2, 3, 4]
C. [2, 4, 6, 8]
D. An empty list [] because map is lazy

Solution

  1. Step 1: Understand the transformation

    The map transformation multiplies each element by 2 but does not run immediately.
  2. Step 2: Understand the action

    The collect() action triggers execution and returns all elements as a list.
  3. Final Answer:

    [2, 4, 6, 8] -> Option C
  4. Quick Check:

    Transformation + collect() = output list [OK]
Hint: collect() runs the job and returns all data as a list [OK]
Common Mistakes:
  • Thinking map() runs immediately
  • Believing collect() returns nothing
  • Confusing transformations with actions
4. Identify the error in the following Spark code snippet:
rdd = sc.parallelize([10, 20, 30])
filtered = rdd.filter(lambda x: x > 15)
filtered.count
medium
A. filter is not a valid transformation
B. Lambda function syntax is incorrect
C. parallelize is not defined
D. Missing parentheses after count action

Solution

  1. Step 1: Check the use of count

    The count method is an action and requires parentheses to execute.
  2. Step 2: Identify the error

    Missing parentheses means filtered.count is a method reference, not a call, so no action runs.
  3. Final Answer:

    Missing parentheses after count action -> Option D
  4. Quick Check:

    Actions need () to run [OK]
Hint: Always add () to call actions like count() [OK]
Common Mistakes:
  • Forgetting parentheses on actions
  • Thinking filter is an action
  • Assuming parallelize is undefined without context
5. You want to find the sum of all even numbers in an RDD numbers. Which sequence of operations correctly uses transformations and actions?
hard
A. Use filter to select even numbers, then reduce to sum them.
B. Use reduce first, then filter to select even numbers.
C. Use map to sum numbers, then collect to filter evens.
D. Use collect first, then filter and reduce on the driver.

Solution

  1. Step 1: Apply transformation to filter even numbers

    Use filter transformation to keep only even numbers in the RDD.
  2. Step 2: Use action to sum filtered numbers

    Use reduce action to sum all filtered even numbers efficiently on the cluster.
  3. Final Answer:

    Use filter to select even numbers, then reduce to sum them. -> Option A
  4. Quick Check:

    Transformations then actions = correct Spark pattern [OK]
Hint: Filter first, then reduce to sum -- transformations before actions [OK]
Common Mistakes:
  • Trying to reduce before filtering
  • Using collect() too early causing driver overload
  • Misusing map() for summing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard