C#.NET — Identify invalid switch-case labels and default usage. switch (id) { case 6: grp = "Grp B"; break; case 13: grp = "Grp D"; break; case 1: grp = "Grp A"; break; case ls > 20: grp = "Grp E"; break; case Else: grp = "Grp F"; break; }
-
ACompiler will report an error in case ls > 20 as well as in case Else.
-
BThere is no error in this switch case statement.
-
CCompiler will report an error only in case Else.
-
DCompiler will report an error as there is no default case.
-
EThe order of the first three cases should be case 1, case 6, case 13 (ascending).
Answer
Correct Answer: Compiler will report an error in case ls > 20 as well as in case Else.
Explanation
Introduction / Context:This tests which constructs are valid inside a C# switch statement and how to specify a default branch.
Given Data / Assumptions:
- Valid numeric cases exist: 6, 13, 1.
- Two suspicious labels: case ls > 20 and case Else.
Concept / Approach:Case labels must be constant expressions of the same type as the switch expression. Relational expressions (like ls > 20) are not allowed. The default branch must be written as default:, not case Else: .
Step-by-Step Solution:
case ls > 20: invalid because case labels cannot be boolean relational expressions. case Else: invalid because the correct keyword is default:, not Else. Other numeric cases are fine regardless of ordering.Verification / Alternative check:Replace "case ls > 20:" with a proper if/else before the switch or compute a bucketed integer; replace "case Else:" with "default:".
Why Other Options Are Wrong:
- B: claims no error; wrong.
- C: only flags Else, ignoring ls > 20; wrong.
- D: default is optional in C#; lack of it is not an error.
- E: ordering is not enforced; any order is acceptable.
Common Pitfalls:Trying to embed boolean conditions in case labels and misusing Else instead of default.
Final Answer:Compiler will report an error in case ls > 20 as well as in case Else.