Showing 21–30 of 100 questions
in Control Flow
In which scenario would a do-while loop be preferred over a while loop?
A
When the loop must execute at least once
B
When the condition is checked before execution
C
When nested loops are involved
D
When break statement is used
Correct Answer:
A. When the loop must execute at least once
EXPLANATION
do-while executes the body first, then checks the condition, ensuring at least one execution.
Which looping construct in C allows initialization, condition, and increment to be placed in a single line?
A
while loop
B
do-while loop
C
for loop
D
None of the above
Correct Answer:
C. for loop
EXPLANATION
The for loop syntax is for(init; condition; increment) allowing all three components in one statement.
What is the output of the following code?
int x = 5;
if(x > 3)
if(x < 10)
printf("Pass");
else
printf("Fail");
A
Pass
B
Fail
C
No output
D
Compilation error
EXPLANATION
The inner if condition (x < 10) is true, so 'Pass' is printed. The else is associated with the inner if, not the outer one.
In a nested loop structure, which statement is used to skip the current iteration of the innermost loop only?
A
break
B
continue
C
return
D
exit()
Correct Answer:
B. continue
EXPLANATION
continue skips the current iteration of the innermost loop and moves to the next iteration, while break exits the loop entirely.
What does continue do in a for loop with multiple statements in body?
A
Skips remaining body statements and goes to next iteration
B
Exits the loop completely
C
Skips only the next statement
D
Causes compilation error
Correct Answer:
A. Skips remaining body statements and goes to next iteration
EXPLANATION
continue skips all remaining statements in the current loop iteration and jumps to the increment/condition check for the next iteration.
Which statement best describes the 'dangling else' problem in C?
A
else cannot be used with nested if statements
B
Else binds to the nearest preceding if without an else
C
Multiple else statements cause compilation error
D
Else keyword must be on same line as if
Correct Answer:
B. Else binds to the nearest preceding if without an else
EXPLANATION
Dangling else refers to ambiguity when else could bind to multiple if statements. C resolves this by binding else to the nearest if.
What will this code do?
int i = 0;
while(i < 5) {
printf("%d ", i++);
if(i == 3) break;
}
A
Prints: 0 1 2
B
Prints: 0 1 2 3
C
Prints: 1 2 3
D
Infinite loop
Correct Answer:
A. Prints: 0 1 2
EXPLANATION
Post-increment: prints 0 (i becomes 1), prints 1 (i becomes 2), prints 2 (i becomes 3). When i==3, break executes. Output: 0 1 2
Consider:
switch(x) {
case 1:
case 2:
case 3: printf("A"); break;
default: printf("B");
}
What advantage does stacking cases provide?
A
Saves memory
B
Allows same code for multiple case values without repetition
C
Improves execution speed
D
Makes code unreadable intentionally
Correct Answer:
B. Allows same code for multiple case values without repetition
EXPLANATION
Stacking cases (without break between them) allows multiple values to execute the same code block, reducing redundancy.
What is the output of:
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
if(i == j) continue;
printf("Done");
A
Done
B
Nothing printed
C
DoneDoneDone (three times)
D
Compilation error
EXPLANATION
continue skips current iteration but doesn't affect outer loop. After nested loops complete, 'Done' prints once.
In a for loop for(int i = 0; i < 5; i++), at what point does the increment happen?
A
Before checking the condition
B
After executing the loop body
C
At the beginning before first iteration
D
During condition evaluation
Correct Answer:
B. After executing the loop body
EXPLANATION
For loop order: initialization → check condition → execute body → increment → repeat from condition check.