ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
int main(void) {
    int x = 2;
    switch (x) {
        case 1:
        case 2:
        case 3:
            printf("low ");
            break;
        case 4:
        case 5:
            printf("high ");
            break;
        default:
            printf("other ");
    }
}
  1. A. low
  2. B. high
  3. C. other
  4. D. low high

Correct answer: A. low

Grouped cases 1, 2, 3 (stacked with no code between them, just falling through to the shared body) all lead to the same "low " print; x=2 matches case 2, falling into "low ", then break exits the switch.

More C Programming questions

Exams that ask this topic