MCQs on Control Structures | Scala

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

  1. 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
  2. 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
  3. 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)
  4. 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
  5. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. Which keyword is used in Scala to yield values from a for loop?
    • A) return
    • B) collect
    • C) yield
    • D) provide
  6. 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
  7. 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
  8. 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
  9. Which loop structure is preferred for transforming collections in Scala?
    • A) while loop
    • B) do-while loop
    • C) for comprehension
    • D) foreach loop
  10. 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

  1. What is pattern matching in Scala primarily used for?
    • A) Looping through collections
    • B) Defining constants
    • C) Matching values against patterns
    • D) Logging data
  2. Which keyword is used to define pattern matching in Scala?
    • A) match
    • B) case
    • C) when
    • D) pattern
  3. What is the default case in pattern matching?
    • A) else
    • B) case _
    • C) case *
    • D) default
  4. 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
  5. Can pattern matching be used as an expression in Scala?
    • A) Yes
    • B) No
    • C) Only in match blocks
    • D) Only with case classes
  6. Which of the following supports pattern matching in Scala?
    • A) Case classes
    • B) Tuples
    • C) Sealed traits
    • D) All of the above
  7. What does the underscore _ represent in pattern matching?
    • A) A wildcard pattern
    • B) An error condition
    • C) A default value
    • D) A null value
  8. Can guards be used in pattern matching cases?
    • A) Yes
    • B) No
    • C) Only with tuples
    • D) Only with collections
  9. 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"
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. Can pattern matching be nested in Scala?
    • A) Yes
    • B) No
    • C) Only for case classes
    • D) Only for sealed traits

Answers

QnoAnswer
1A) if (condition) { block }
2C) Depends on the block’s result type
3D) It returns () (Unit)
4D) All of the above
5A) Yes
6B) do-while checks the condition after execution
7A) It runs indefinitely (infinite loop)
8B) After the loop executes once
9D) All of the above
10C) yield
11A) Yes
12B) A condition that filters iterations
13A) for (x <- list if x > 0) yield x
14C) for comprehension
15A) Yes
16C) Matching values against patterns
17A) match
18B) case _
19A) A runtime exception is thrown
20A) Yes
21D) All of the above
22A) A wildcard pattern
23A) Yes
24A) x match { case 1 => "one" case _ => "other" }
25B) They automatically support deconstruction in matching
26A) All cases must be defined
27A) Yes, using specific case patterns
28B) Matches the absence of a value
29A) case (x, y) => x + y
30A) Yes

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