C#.NET — Evaluate bitwise OR of character literals and switch grouping result. char ch = Convert.ToChar('a' | 'b' | 'c'); switch (ch) { case 'A': case 'a': Console.WriteLine("case A | case a"); break; case 'B': case 'b': Console.WriteLine("case B | case b"); break; case 'C': case 'c': case 'D': case 'd': Console.WriteLine("case D | case d"); break; }

Difficulty: Medium

Correct Answer: case D | case d

Explanation:


Introduction / Context:
This tests understanding of bitwise OR on character codes and how a switch with grouped cases routes execution to a specific write line.


Given Data / Assumptions:

  • Character literals: 'a' = 97, 'b' = 98, 'c' = 99 (ASCII/Unicode code points in this range).
  • Expression: Convert.ToChar('a' | 'b' | 'c').
  • Switch groups map to different Console.WriteLine strings.


Concept / Approach:
Bitwise OR combines set bits. Since 97|98 == 99, and 99|99 == 99, the result is 'c'. The switch has a group that includes 'C'/'c' and also routes to a print line labeled "case D | case d".


Step-by-Step Solution:

Compute 97 | 98 = 99. Then 99 | 99 = 99. Convert.ToChar(99) yields 'c'. Switch on 'c': control flows into the case block that handles 'C', 'c', 'D', and 'd' and prints "case D | case d".


Verification / Alternative check:
Write hex: 0x61 | 0x62 = 0x63; 0x63 | 0x63 = 0x63; 0x63 corresponds to 'c'.


Why Other Options Are Wrong:

  • "case A | case a" and "case B | case b": not selected for 'c'.
  • Compile Error: valid code.
  • No output: one of the cases matches; printing occurs.


Common Pitfalls:
Assuming grouped cases must print their own letter; in this example, the author intentionally prints the string mentioning D for multiple labels.


Final Answer:
case D | case d

More Questions from Control Instructions

Discussion & Comments

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