Identify the correct statements about C# control statements: 1) A switch statement can act on numerical as well as Boolean types. 2) A switch statement can act on characters, strings, and enumeration types. 3) We cannot declare variables within a case unless the statements are enclosed by { }. 4) foreach should be used to change a collection to avoid unpredictable side effects. 5) All expressions of the for statement are not optional.

Difficulty: Medium

Correct Answer: 2, 3

Explanation:


Introduction / Context:
This question checks nuanced C# rules for switch, foreach, and for statements.



Given Data / Assumptions:

  • Five statements cover allowed switch types, scoping in case labels, foreach behavior, and for clause optionality.


Concept / Approach:
Classic C# switch does not use bool. It does support char, string, and enum. Declaring variables directly under a case usually requires braces to create scope. foreach is read-only in intent; modifying the collection during enumeration is discouraged. In C#, for(;;) is allowed, so all three expressions are optional.



Step-by-Step Solution:

1) False — bool is not a classic switch operand.2) True — char, string, enum are valid.3) True — braces ensure proper scope under case labels.4) False — modifying a collection during foreach is unsafe.5) False — all for clauses are optional.


Verification / Alternative check:
Language specifications and compiler behavior confirm items 2 and 3 only.



Why Other Options Are Wrong:

  • 1, 2: Includes 1 (false).
  • 3, 5: Includes 5 (false).
  • 4, 5: Both are false.
  • None of these: Incorrect because 2 and 3 are true.


Common Pitfalls:
Forgetting to use braces when declaring variables within case sections.



Final Answer:
2, 3

More Questions from Control Instructions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion