Question discussion

What will be the output of the following code?

358842Medium DSSSB TGT Computer Science 0 replies 0 views
0
What will be the output of the following code?
#include <stdio.h>
int main() {
    int a = 2, b = 3;
    switch (a) {
        case 1:
            printf("Case 1");
            break;
        case 2:
            switch (b) {
                case 3:
                    printf("Case 3");
                    break;
                default:
                    printf("Inner Default");
            }
        default:
            printf("Outer Default");
    }
    return 0;
}
  • ACase 3Inner Default
  • BCase 3
  • CInner DefaultOuter Default
  • DCase 3Outer Default
Correct answer: D

Explanation

a = 2 matches case 2, which runs the inner switch. b = 3 prints "Case 3" and the inner break exits only the inner switch. There is no break after it in case 2, so control falls to the outer default and prints "Outer Default".

Discussion 0

Nobody has replied yet. Start the discussion!