MCQs on Case Classes and Pattern Matching | Scala

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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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)
  6. Which of the following features do case classes automatically provide?
    • A) HashCode and equals methods
    • B) Serializable methods
    • C) Constructor overloading
    • D) Polymorphism
  7. 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
  8. 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
  9. 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
  10. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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 =>
  8. 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
  9. 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
  10. 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

QnoAnswer (Option with the text)
1B) They automatically generate methods for equality, hashCode, and toString
2B) Case classes have automatically generated companion objects
3C) Fields are immutable by default
4B) It creates a modified copy with some fields changed
5B) case class ClassName(val x: Int)
6A) HashCode and equals methods
7B) They provide immutability and are easier to use in pattern matching
8B) Yes, case classes can be used in pattern matching
9B) Case classes generate additional methods like toString, equals, and hashCode automatically
10B) Public
11B) It restricts the trait’s extensions to the same file
12B) They provide exhaustive checks for pattern matching
13B) They enforce exhaustive checks for pattern matching
14A) They can be extended by any class or trait within the same file
15B) To define the base for an exhaustive pattern matching hierarchy
16B) No, they can only be extended within the same file
17A) It forces you to cover all possible cases within the trait hierarchy
18B) Representing a closed set of types in pattern matching
19C) Sealed traits
20A) It ensures that the matching will always be exhaustive
21B) A method to match against an object and extract values
22A) Using the unapply method in a companion object
23B) It extracts values from a matched object
24A) To filter out objects during the matching process
25A) By using an if condition after the pattern
26B) Guards are optional and can be used for additional conditions
27A) case x if x > 10 =>
28B) Yes, case classes can be used with extractors
29A) To extract values from matched objects
30B) The next case is attempted

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top