Reviewed by Aditya Kumar · Last reviewed 2026-03-24
**Why It Matters for Data Engineering**: Immutability reduces bugs in distributed pipelines; def enables lazy/computed values. **var**: Mutable, reassignable. Use sparingly—loops, accumulators. Risk: shared state in concurrent code. **val**: Immutable binding; computed once at...
Why It Matters for Data Engineering: Immutability reduces bugs in distributed pipelines; def enables lazy/computed values.
var: Mutable, reassignable. Use sparingly—loops, accumulators. Risk: shared state in concurrent code.
val: Immutable binding; computed once at definition. Preferred for RDDs/DataFrames—avoids accidental reassignment.
def: Method; re-evaluated each call. Use for parameterized factories; avoid for expensive ops without caching.
Trade-off: val + immutable collections = predictable; var in closures can cause serialization issues in Spark.
Red Flag: Overusing var. Pro-Move: 'In Spark I use val for DataFrames and def for factory methods with parameters.'
According to DataEngPrep.tech, this is one of the most frequently asked General/Other interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.