What is the primary difference between a while loop and a do-while loop?
Answer: A
A do-while loop executes the body at least once before checking the condition. A while loop checks the condition first, so it may not execute at all.
Q.22Easy
In a nested loop structure, which statement will skip only the inner loop's current iteration?
Answer: B
The 'continue' statement skips the remaining statements of the current iteration and moves to the next iteration. In nested loops, it affects only the innermost loop.
Q.23Medium
Consider a switch statement with multiple cases. If a case matches and break is omitted, what happens?
Answer: B
Without a break statement, execution continues to the next case (fall-through behavior). This is a common source of bugs if not intentional.
Q.24Medium
What will be the behavior of a for loop with an empty body and no statements inside braces?
Answer: C
A for loop with an empty body is valid syntax in C. The loop executes for the specified iterations but performs no operations in each iteration.
Q.25Easy
In the expression (x > 5) ? (y = 10) : (y = 20), what does y equal if x is 3?
Answer: B
The ternary operator evaluates the condition (x > 5). Since 3 is not greater than 5, the expression evaluates to false, and the false part executes, setting y to 20.
Advertisement
Q.26Easy
Which control flow structure allows execution of code only if a certain condition is met?
Answer: B
The if statement executes a block of code conditionally. Loops repeat code, and switch cases are for multi-way branching based on a single expression.
Q.27Medium
What is the output of executing break in a nested loop context?
Answer: B
The break statement exits only the innermost loop it is in. To exit multiple nested loops, you need multiple break statements or use goto.
Q.28Easy
In a switch statement, which keyword provides a default case if no cases match?
Answer: B
The 'default' keyword in a switch statement provides code to execute when no case values match the switch expression.
Q.29Easy
What is the correct syntax for a for loop that iterates from 0 to 9?
Answer: B
for(int i=0; i<10; i++) correctly iterates from 0 to 9 (10 iterations total). Using i<=10 would iterate 11 times, and starting from 1 would skip 0.
Q.30Medium
Which of the following correctly uses the conditional (ternary) operator?
Answer: B
The correct syntax is (condition) ? (value_if_true) : (value_if_false). The condition comes first, followed by ? and then the true and false values separated by :.
Q.31Medium
How many times will a do-while loop execute if the condition is initially false?
Answer: B
A do-while loop executes the body at least once before checking the condition. Even if the condition is false, the body executes one time.
Q.32Medium
In control flow, which statement is used to explicitly exit from a program?
Answer: B
The exit() function terminates the entire program and returns control to the operating system. break exits loops, and return exits functions.
Q.33Hard
What happens when continue is used in a for loop with a post-increment expression?
Answer: B
When continue is executed in a for loop, the post-increment (i++) still executes before the condition is checked again. Only the body statements after continue are skipped.
Q.34Easy
Which control structure allows checking multiple conditions in sequence using else-if?
Answer: C
The if-else-if chain allows testing multiple different conditions sequentially. switch is used for a single expression with multiple values.
Q.35Medium
In a switch statement, case values must be of which type?
Answer: B
Switch case values must be constant integral expressions (int, char, enum). Floating-point values and strings (in C89/C90) are not allowed.
Q.36Hard
What is the effect of using multiple break statements in nested loops?
Answer: C
Each break statement exits only its immediately enclosing loop. To exit multiple nested loops, you need one break for each level you want to exit.
Q.37Medium
What is the scope of a variable declared in the initialization section of a for loop?
Answer: C
Variables declared in a for loop's initialization (e.g., for(int i=0; ...)) have block scope limited to that loop. They are not accessible outside the loop.
Q.38Medium
Which statement is best suited for implementing a menu-driven program with exact matching?
Answer: C
A switch statement is ideal for menu-driven programs where you match user input against specific cases. It's more efficient and readable than multiple if-else statements for this purpose.
Q.39Medium
What will be the output of the following code?
int i = 0;
while(i < 3) {
printf("%d ", i++);
if(i == 2) continue;
}
Answer: A
The loop prints 0, then i becomes 1. When i becomes 2, continue skips the rest but the loop continues. i becomes 3 and loop exits. Output: 0 1 2
Q.40Easy
Which keyword is used to exit a loop prematurely in C?
Answer: B
The 'break' statement is used to exit or terminate a loop prematurely. 'exit' terminates the entire program, while 'return' exits functions.