Govt Exams
int x = 5;
if(x > 3)
if(x > 10)
printf("A");
else
printf("B");
Which statement best describes the output?
The else binds to the nearest if statement (inner if). Since x=5 is not > 10, the inner condition fails and else executes, printing 'B'.
int i = 0;
while(1) {
i++;
if(i > 5) break;
}
while(1) creates an infinite loop. i increments until i > 5 (i becomes 6), then break exits. Loop runs 6 iterations.
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
if(i + j == 1) continue;
printf("Done");
continue only affects the inner loop, not the outer. The loops complete normally and 'Done' is printed once after both loops finish.
if(1) if(0) printf("A"); else printf("B");
The else binds to the nearest if (inner if). Outer if(1) is true, inner if(0) is false, so else executes printing 'B'.
Case labels must be constant integral expressions (int, char, etc.). Floating-point constants are not allowed as case labels.
int x = 0;
for(int i = 1; i
continue skips even values. x accumulates: 1(i=1) + 3(i=3) + 5(i=5) = 9. Values i=2,4 are skipped.
for(int i = 0; i < 3; i++) {
if(i == 1) goto skip;
printf("%d ", i);
}
skip: printf("End");
When i=1, goto skip jumps to the skip label, skipping the printf. i continues to 2 in the loop. Output: 0 2 End
int x = 5;
switch(x) {
case 5: printf("Five");
case 6: printf("Six");
default: printf("Default");
}
Without a break statement after case 5, control falls through to case 6 and default. This is called fall-through behavior in switch statements.
'goto' transfers control to the label specified, which can be anywhere in the program. It can jump out of nested structures to the labeled location.
for(int i = 1; i
The break statement only exits the inner loop. When i=2 and j=1, inner loop breaks. i=3 executes normally. Output: 11 31 32