Assuming standard headers and using namespace std, what does this C++ code print? int n = 2; switch (n) { case 1: cout << “A”; case 2: cout << “B”; case 3: cout << “C”; break; default: cout << “D”; }
Correct answer A. BC
Explanation
Control jumps to case 2 and prints B. Because there is no break after case 2, execution falls through into case 3 and prints C, then the break ends the switch, so D is never printed. Case labels only mark entry points; without break, every following statement runs. C++17 offers [[fallthrough]] to document deliberate fall-through.
Answer checked How we write and check questions
Discussion