Scala Interview Questions
Answers written to be said out loud in an interview — each with a common follow-up question and a link to the tutorial that explains it in depth.
- Beginner
- 3
- Intermediate
- 4
- Experienced
- 3
01What is the difference between val and var?Beginner
val declares an immutable reference that cannot be reassigned; var declares a mutable one. Idiomatic Scala prefers val and immutable collections.
02What is a case class?Beginner
A class for immutable data. The compiler generates a constructor without new, field accessors, equals/hashCode based on values, toString, copy and pattern-matching support.
case class User(name: String, age: Int)
val a = User("Asha", 25)
val b = a.copy(age = 26) // User(Asha,26)
a == User("Asha", 25) // true
03What is Option?Beginner
A type that is either Some(value) or None, used instead of null to represent a value that may be missing. Use map, getOrElse, fold or pattern matching to handle both cases.
04What is pattern matching?Intermediate
A powerful match expression that checks a value against patterns — constants, types, case class shapes, tuples, lists — and can extract parts of the value. With sealed types the compiler warns about missing cases.
05What is a trait?Intermediate
A reusable unit of fields and methods that classes can mix in; a class can extend several traits. Traits replace Java interfaces and also hold implemented methods.
06What is a for comprehension?Intermediate
Syntax sugar over map, flatMap and withFilter that makes chaining Options, Lists, Futures or Eithers readable, stopping early on None or Left.
07What are higher-order functions?Intermediate
Functions that take other functions as parameters or return them, such as map, filter and foldLeft. They enable concise, reusable data transformations.
08What are given and using in Scala 3?Experienced
Contextual abstractions: a given defines a canonical instance of a type (for example an Ordering), and a using clause asks the compiler to supply it automatically. They replace Scala 2 implicits for type classes and context passing.
09How do Futures work?Experienced
A Future represents a value computed asynchronously on an ExecutionContext. Compose them with map, flatMap and for comprehensions; handle failures with recover. Avoid Await in production code.
10Why is Scala popular with Apache Spark?Experienced
Spark itself is written in Scala, its DataFrame/Dataset APIs feel natural with Scala’s functional collections and case classes, and the typed Dataset API uses case classes for compile-time schema checks.