Practice mode C++

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.