Which of the following statements about the switch-case construct is INCORRECT?
AThe default case is mandatory in every switch statement
BEach case must end with a break statement to prevent fall-through
CMultiple cases can have the same code block if break is omitted
DThe switch expression is evaluated only once
Correct Answer:
A. The default case is mandatory in every switch statement
EXPLANATION
The default case is optional in a switch statement. It executes when no case matches, but it is not mandatory. All other statements are correct properties of switch-case.
In nested loops, if an inner loop contains a break statement, what happens?
AOnly the inner loop terminates; the outer loop continues
BBoth inner and outer loops terminate
CThe entire program terminates
DThe break statement is ignored in nested loops
Correct Answer:
A. Only the inner loop terminates; the outer loop continues
EXPLANATION
The break statement only exits the innermost loop that contains it. It does not affect the outer loop's execution. The outer loop will continue its iterations normally.
Consider the following code snippet:
int x = 5;
do {
printf("%d ", x);
x--;
} while(x > 0);
What will be the output?
A5 4 3 2 1
B4 3 2 1
C5 4 3 2 1 0
DNo output
Correct Answer:
A. 5 4 3 2 1
EXPLANATION
The do-while loop executes at least once. x starts at 5, prints 5, decrements to 4, and continues while x > 0. Loop executes for x = 5,4,3,2,1 and exits when x becomes 0.
What is the primary difference between 'break' and 'continue' statements in C loops?
Abreak exits the loop entirely, while continue skips the current iteration and proceeds to the next
Bcontinue exits the loop entirely, while break skips the current iteration
CBoth statements perform identical functions
Dbreak skips iterations, continue terminates the program
Correct Answer:
A. break exits the loop entirely, while continue skips the current iteration and proceeds to the next
EXPLANATION
break statement terminates the loop completely, whereas continue statement skips the remaining statements in the current iteration and moves to the next iteration of the loop.
What will be printed?
int i = 0, j = 0;
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
if(i + j == 2) break;
printf("%d%d ", i, j);
}
}
A00 01 10
B00 01 10 11 20 21
C00 01 11 20 21
D00 10 20
Correct Answer:
A. 00 01 10
EXPLANATION
Inner break only exits the inner loop. i=0,j=0: prints 00; i=0,j=1: prints 01, breaks when j=2; i=1,j=0: prints 10, breaks when j=1; i=2,j=0: breaks immediately.