Courseiva

Databricks-DE-Assoc Troubleshooting, Monitoring, and Optimization Practice Question

A data engineer is troubleshooting a Databricks job that intermittently fails with a `SparkException: Job aborted due to stage failure: Task not serializable`. The job reads from a Parquet file, performs a transformation using a custom function defined in a Python class, and writes to a Delta table. The engineer suspects that the custom function is causing the issue. Which action should the engineer take to resolve the serialization error?

⚠ Common exam trap

The trap here is assuming that changing the serializer or increasing memory will fix the issue, when the real problem is the capture of non-serializable objects in the closure.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

Refactor the custom function to avoid referencing any non-serializable objects from the enclosing class or module.

The `Task not serializable` error arises when Spark tries to serialize a closure for execution on executors but encounters objects that cannot be serialized. This often happens when a function references a non-serializable object from its enclosing scope, such as a database connection or a large data structure. Refactoring the function to avoid such references ensures that only serializable data is captured, resolving the error.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Mark the custom function with the `@staticmethod` decorator to avoid serializing the enclosing class.

    Why it's wrong here

    Using @staticmethod can help if the function does not need instance state, but the error indicates that the function captures non-serializable objects. Simply making it static may not resolve the issue if the function still references instance variables or other non-serializable entities. The root cause is the capture of non-serializable objects, not the method type.

  • ✗

    Increase the driver memory to ensure that the serialized function fits in memory.

    Why it's wrong here

    Increasing driver memory does not address serialization issues. The error is not about memory capacity but about the inability to serialize certain objects. More memory will not make non-serializable objects serializable. The engineer must fix the function to remove references to non-serializable entities.

  • ✓

    Refactor the custom function to avoid referencing any non-serializable objects from the enclosing class or module.

    Why this is correct

    The `Task not serializable` error occurs when a closure captures objects that cannot be serialized and sent to executors. By refactoring the function to avoid referencing non-serializable objects (e.g., database connections, file handles, or large objects), the function becomes serializable and the error is resolved. This directly addresses the root cause.

  • ✗

    Set the Spark configuration `spark.serializer` to `org.apache.spark.serializer.KryoSerializer` to enable Kryo serialization.

    Why it's wrong here

    Switching to Kryo serialization can improve performance and sometimes handle objects that Java serialization cannot, but it does not automatically make non-serializable objects serializable. If the function captures objects that are fundamentally not serializable (e.g., open sockets), Kryo will also fail. The correct approach is to eliminate the capture of such objects.

Visual reference

Client Recursive Resolver Root DNS (13 root servers) TLD DNS (.com, .org, …) Authoritative example.com query IP addr answer

About these practice questions

One of 276 original Databricks-DE-Assoc practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Databricks exam blueprint

This Databricks-DE-Assoc practice question is part of Courseiva's free Databricks certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the Databricks-DE-Assoc exam.