CPA-21-02 practice question 28 of 35

What happens when you attempt to compile and run the following code? #include using namespace std; int main() { int i=5; switch(i) { case 1: cout

Full set: 257 questions
Answer options
A: It prints: Hello
B: It prints: world
C: It prints: End Correct answer
D: It prints: Helloworld
Correct Answer:
C. It prints: End
Explanation

In this program, the integer variable i is initialized to 5. The switch statement evaluates the expression i. It then checks the provided case labels sequentially. Since there are only labels for case 1, case 2, and case 3, and none of them match the value 5, the control flow jumps directly to the default label. The statement under the default label is cout<<"End";, which prints "End" to the console. The program then exits normally.

Why Incorrect

A. Incorrect because i evaluates to 5, so the switch statement bypasses case 1 entirely.
B. Incorrect because i evaluates to 5, so the switch statement bypasses case 2 entirely.
D. Incorrect because i evaluates to 5, meaning the execution jumps directly to the default label, skipping the "Hello" and "world" outputs.

References

1. ISO/IEC 14882:2017, Section 9.4.2 [stmt.switch], Paragraph 4-5 (Switch statement execution and default label behavior).

2. MIT OpenCourseWare, 6.096 Introduction to C++, Lecture 2: Control Structures (Switch statements).

Scroll to Top