Question 56
What happens in a C 'switch' statement if a matching 'case' is found but lacks a 'break' statement?
Detailed Solution
Without a break statement, execution continues into following case labels (fall-through behavior).
What happens in a C 'switch' statement if a matching 'case' is found but lacks a 'break' statement?
Without a break statement, execution continues into following case labels (fall-through behavior).
What is the minimum number of times a 'do-while' loop body executes?
Because do-while checks its exit condition at the bottom, its body always executes at least once.
What is the output of this code?
#include <stdio.h>
int sum = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) continue;
sum += i;
}
printf("%d", sum);The continue statement skips even numbers (2 and 4). Sum = 1 + 3 + 5 = 9.
Which of the following expression types is permitted inside a C 'switch(expr)' statement?
In C, switch expression must evaluate to an integer or enumeration type.
What is the output of the code snippet?
#include <stdio.h>
int main(void) {
int a = 1;
switch(a) {
case 1: printf("One ");
case 2: printf("Two ");
default: printf("Def");
}
return 0;
}Due to missing break statements, execution falls through case 1, case 2, and default, printing 'One Two Def'.