Master the concepts of case classes, pattern matching, sealed traits, and extractors in Scala. These 30 MCQs will help you strengthen your knowledge of immutability, guards, and pattern matching.
Case Classes and Pattern Matching in Scala
1. Case Classes and Immutability
What is the primary feature of a case class in Scala?
A) They are mutable by default
B) They automatically generate methods for equality, hashCode, and toString
C) They cannot extend other classes
D) They are designed only for inheritance
Which of the following is true about case classes in Scala?
A) Case classes are always mutable
B) Case classes have automatically generated companion objects
C) Case classes cannot be used in pattern matching
D) Case classes do not support immutability
When using case classes, what is the default behavior for the fields?
A) Fields are mutable by default
B) Fields are private by default
C) Fields are immutable by default
D) Fields are public by default
What does the copy method in case classes do?
A) It clones the case class object
B) It creates a modified copy with some fields changed
C) It converts the case class into a tuple
D) It changes the class to a mutable one
How do you define a case class in Scala?
A) class ClassName(val x: Int)
B) case class ClassName(val x: Int)
C) object ClassName(val x: Int)
D) case object ClassName(val x: Int)
Which of the following features do case classes automatically provide?
A) HashCode and equals methods
B) Serializable methods
C) Constructor overloading
D) Polymorphism
What is the advantage of using case classes for data structures in Scala?
A) They allow mutable fields
B) They provide immutability and are easier to use in pattern matching
C) They are used for inheritance purposes
D) They are faster than regular classes
Can you pattern match against case classes in Scala?
A) No, case classes cannot be used in pattern matching
B) Yes, case classes can be used in pattern matching
C) Only abstract case classes can be used in pattern matching
D) Only case classes with mutable fields can be used in pattern matching
How are case classes different from regular classes in Scala?
A) Case classes are mutable
B) Case classes generate additional methods like toString, equals, and hashCode automatically
C) Case classes cannot extend other classes
D) Case classes are used for functional programming only
In Scala, what is the default access modifier for fields in case classes?
A) Private
B) Public
C) Protected
D) Default to no modifier
2. Sealed Traits for Pattern Matching
What does a sealed trait in Scala allow you to do?
A) It allows creating an infinite hierarchy of classes
B) It restricts the trait’s extensions to the same file
C) It enforces that only one class can extend the trait
D) It allows any class to extend the trait
Why are sealed traits commonly used in pattern matching?
A) They restrict the number of classes that can extend the trait
B) They provide methods for pattern matching
C) They enforce immutability
D) They are more flexible than normal traits
What is a key advantage of using sealed traits in pattern matching?
A) They reduce the number of pattern matching cases required
B) They enforce exhaustive checks for pattern matching
C) They make pattern matching faster
D) They simplify trait inheritance
Which of the following is true for sealed traits?
A) They can be extended by any class or trait within the same file
B) They can be extended only by one class
C) They are immutable by default
D) They cannot be used with case classes
What is a sealed trait commonly used for in Scala?
A) To allow for multiple inheritance
B) To define the base for an exhaustive pattern matching hierarchy
C) To implement mutable state
D) To define abstract methods only
Can a sealed trait be extended outside of its defining file?
A) Yes, sealed traits can be extended globally
B) No, they can only be extended within the same file
C) Sealed traits cannot be extended
D) Sealed traits can only be extended by classes
How does the match keyword work with sealed traits?
A) It forces you to cover all possible cases within the trait hierarchy
B) It allows pattern matching with only one case
C) It does not work with sealed traits
D) It results in an error if all cases aren’t explicitly matched
What is the recommended use case for sealed traits in Scala?
A) Defining complex mutable state
B) Representing a closed set of types in pattern matching
C) Implementing inheritance hierarchies
D) Creating highly flexible trait structures
Which of the following Scala features is used to represent a closed set of types?
A) Abstract classes
B) Case classes
C) Sealed traits
D) Interfaces
What does pattern matching on a sealed trait ensure?
A) It ensures that the matching will always be exhaustive
B) It ensures that no class can extend the trait
C) It ensures that the trait is mutable
D) It ensures better performance
3. Extractors and Guards in Patterns
What is an extractor in Scala pattern matching?
A) A function that converts an object into a case class
B) A method to match against an object and extract values
C) A type of pattern that only works with classes
D) A trait used to create pattern matching
How do you define an extractor in Scala?
A) Using the unapply method in a companion object
B) Using the apply method in a companion object
C) Using the match method
D) Using the extract method in the trait
What does the unapply method do in Scala extractors?
A) It transforms a value into a case class
B) It extracts values from a matched object
C) It applies a condition for pattern matching
D) It returns the type of the object
What is the purpose of guards in Scala pattern matching?
A) To filter out objects during the matching process
B) To provide default behavior when no case matches
C) To enforce immutability in the matched value
D) To throw an exception when no match is found
How do you use a guard in Scala pattern matching?
A) By using an if condition after the pattern
B) By declaring a variable inside the pattern
C) By using a match clause without a condition
D) By defining the guard inside the unapply method
Which of the following is true about guards in Scala?
A) Guards are always used to validate values after matching
B) Guards are optional and can be used for additional conditions
C) Guards replace patterns in match expressions
D) Guards work only with case classes
How would you write a pattern matching guard that only matches if a number is greater than 10?
A) case x if x > 10 =>
B) case x when x > 10 =>
C) case x where x > 10 =>
D) case x x > 10 =>
Can extractors be used with case classes in Scala?
A) No, extractors can only be used with normal classes
B) Yes, case classes can be used with extractors
C) Yes, but only if the case class is sealed
D) No, case classes don’t support extractors
In Scala, what is a typical use case for extractors in pattern matching?
A) To extract values from matched objects
B) To override the default matching behavior
C) To create new case classes dynamically
D) To store variables for pattern matching
What happens if a guard fails in Scala pattern matching?
A) The match expression is skipped
B) The next case is attempted
C) The pattern matching fails and throws an error
D) The pattern matching proceeds without any change
Answer Key
Qno
Answer (Option with the text)
1
B) They automatically generate methods for equality, hashCode, and toString
2
B) Case classes have automatically generated companion objects
3
C) Fields are immutable by default
4
B) It creates a modified copy with some fields changed
5
B) case class ClassName(val x: Int)
6
A) HashCode and equals methods
7
B) They provide immutability and are easier to use in pattern matching
8
B) Yes, case classes can be used in pattern matching
9
B) Case classes generate additional methods like toString, equals, and hashCode automatically
10
B) Public
11
B) It restricts the trait’s extensions to the same file
12
B) They provide exhaustive checks for pattern matching
13
B) They enforce exhaustive checks for pattern matching
14
A) They can be extended by any class or trait within the same file
15
B) To define the base for an exhaustive pattern matching hierarchy
16
B) No, they can only be extended within the same file
17
A) It forces you to cover all possible cases within the trait hierarchy
18
B) Representing a closed set of types in pattern matching
19
C) Sealed traits
20
A) It ensures that the matching will always be exhaustive
21
B) A method to match against an object and extract values
22
A) Using the unapply method in a companion object
23
B) It extracts values from a matched object
24
A) To filter out objects during the matching process
25
A) By using an if condition after the pattern
26
B) Guards are optional and can be used for additional conditions