Explore key Scala concepts like if/else statements, loops (while, do-while, for-comprehensions), and pattern matching basics. These 30 MCQs will strengthen your understanding of Scala control structures.
Control Structures in Scala
1. If/Else Statements
How do you structure a simple if statement in Scala?
A) if (condition) { block }
B) if: condition -> block
C) if condition then block
D) if(condition); block
What is the return type of an if statement in Scala?
A) Always Boolean
B) Always Unit
C) Depends on the block’s result type
D) None
What happens if the else block is missing in a Scala if/else statement?
A) It throws an error
B) It assumes a default value of 0
C) It returns null
D) It returns () (Unit)
Which of the following is valid syntax for an if/else statement in Scala?
A) if (condition) block else block
B) if condition { block } else { block }
C) if (condition) { block } else { block }
D) All of the above
Can an if/else statement be used as an expression in Scala?
A) Yes
B) No
C) Only if it has an else block
D) Only in case classes
2. Loops: while, do-while, for-comprehensions
What is the primary difference between a while loop and a do-while loop?
A) while checks the condition after execution, do-while checks it before
B) do-while checks the condition after execution, while checks it before
C) while loops are faster
D) do-while loops do not require curly braces
What happens if the condition in a while loop is always true?
A) It runs indefinitely (infinite loop)
B) It throws a runtime error
C) It automatically exits after one iteration
D) It depends on the compiler
In a do-while loop, when is the condition checked?
A) Before the loop executes
B) After the loop executes once
C) Only if the loop executes more than once
D) It depends on the code
What is a key characteristic of a Scala for comprehension?
A) It can iterate over collections
B) It returns a new collection or value
C) It supports filtering with if clauses
D) All of the above
Which keyword is used in Scala to yield values from a for loop?
A) return
B) collect
C) yield
D) provide
Can for comprehensions in Scala include multiple if clauses?
A) Yes
B) No
C) Only one if clause is allowed
D) Only with nested loops
Which of the following best describes a guard in a for comprehension?
A) A condition to exit the loop early
B) A condition that filters iterations
C) A condition to initialize variables
D) A loop terminator
What is a valid example of a for comprehension with a guard in Scala?
A) for (x <- list if x > 0) yield x
B) for x in list where x > 0 yield x
C) for each x in list { if x > 0 yield x }
D) for x from list filter x > 0 yield x
Which loop structure is preferred for transforming collections in Scala?
A) while loop
B) do-while loop
C) for comprehension
D) foreach loop
Can a for comprehension in Scala use multiple generators?
A) Yes
B) No
C) Only with nested loops
D) Only when yielding lists
3. Pattern Matching Basics
What is pattern matching in Scala primarily used for?
A) Looping through collections
B) Defining constants
C) Matching values against patterns
D) Logging data
Which keyword is used to define pattern matching in Scala?
A) match
B) case
C) when
D) pattern
What is the default case in pattern matching?
A) else
B) case _
C) case *
D) default
What happens if no case matches in a pattern matching block?
A) A runtime exception is thrown
B) The program exits
C) The default case is executed automatically
D) It returns null
Can pattern matching be used as an expression in Scala?
A) Yes
B) No
C) Only in match blocks
D) Only with case classes
Which of the following supports pattern matching in Scala?
A) Case classes
B) Tuples
C) Sealed traits
D) All of the above
What does the underscore _ represent in pattern matching?
A) A wildcard pattern
B) An error condition
C) A default value
D) A null value
Can guards be used in pattern matching cases?
A) Yes
B) No
C) Only with tuples
D) Only with collections
Which of the following is a valid syntax for a match expression in Scala?
A) x match { case 1 => "one" case _ => "other" }
B) x match case 1 => "one" else "other"
C) match x: case 1 => "one"; _ => "other"
D) case x with 1 -> "one" default -> "other"
How are case classes useful in pattern matching?
A) They define unique object types
B) They automatically support deconstruction in matching
C) They can hold null values
D) They bypass default cases
What is required when using pattern matching with a sealed trait?
A) All cases must be defined
B) Only partial cases are allowed
C) It depends on the compiler
D) A wildcard case is mandatory
Can pattern matching be applied to collections in Scala?
A) Yes, using specific case patterns
B) No, it only works with primitive types
C) Yes, but only with immutable collections
D) Only for Lists
What is the purpose of case None in pattern matching?
A) Handles null values
B) Matches the absence of a value
C) Handles default cases
D) Matches all remaining cases
Which of the following is a valid example of tuple matching in Scala?
A) case (x, y) => x + y
B) match (x, y): x -> y
C) case x, y -> x + y
D) match case x + y
Can pattern matching be nested in Scala?
A) Yes
B) No
C) Only for case classes
D) Only for sealed traits
Answers
Qno
Answer
1
A) if (condition) { block }
2
C) Depends on the block’s result type
3
D) It returns () (Unit)
4
D) All of the above
5
A) Yes
6
B) do-while checks the condition after execution
7
A) It runs indefinitely (infinite loop)
8
B) After the loop executes once
9
D) All of the above
10
C) yield
11
A) Yes
12
B) A condition that filters iterations
13
A) for (x <- list if x > 0) yield x
14
C) for comprehension
15
A) Yes
16
C) Matching values against patterns
17
A) match
18
B) case _
19
A) A runtime exception is thrown
20
A) Yes
21
D) All of the above
22
A) A wildcard pattern
23
A) Yes
24
A) x match { case 1 => "one" case _ => "other" }
25
B) They automatically support deconstruction in matching