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.
Which statement is true about the goto statement in modern C programming?
AIt is completely removed from C standards
BIt is legal but generally discouraged for code clarity
CIt is mandatory for error handling
DIt automatically optimizes loop performance
Correct Answer:
B. It is legal but generally discouraged for code clarity
EXPLANATION
goto is valid in C but discouraged in modern programming as it reduces code readability. However, it has legitimate uses in cleanup code and error handling.