SwitchCase in C++ MCQs Exercise I

Ques 1 SwitchCase


What is the output of the following Code?

int main()
{
  int a = 1;
  switch(a)
  {
    case 1: cout<<"1";
    case 2: cout<<"2";
    case 3: cout<<"3";
    default: cout<<"Default";
  }
  return 0;
}

A 1
B 12
C 123
D 123Default

The switch statement checks the value of a, which is 1.
The case 1: matches, so the code inside it, cout<< "1", is executed.
Since there's no break statement after case 1:, the execution continues to the next case, case 2:.
The code inside case 2:, cout<< "2", is also executed.
Similarly, the execution continues to case 3: and the code inside it, cout<< "3", is executed.
Finally, the default: case is reached, but it's not executed because a matching case was found earlier.