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 questionsIn 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.
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.
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).
Keep practicing
Take the free CPA-21-02 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other CPA-21-02 practice questions
- Question 1What happens when you attempt to compile and run the following code? #include using names…
- Question 2What happens when you attempt to compile and run the following code? #include #include us…
- Question 3Which of the following statements are correct about an array? int tab[10];
- Question 4What happens when you attempt to compile and run the following code? #include #include us…
- Question 5What is the output of the program if characters 'h', 'e', 'l', 'l' , 'o' and enter are su…
- Question 6What happens when you attempt to compile and run the following code? #include using names…
