What is the primary difference between while and do-while loops?
Answer: B
do-while loop checks the condition after executing the loop body, ensuring at least one execution. while loop checks before executing.
Q.242Easy
In a switch statement, if a case label is missing the break statement, what occurs?
Answer: C
Missing break causes fall-through behavior where execution continues to the next case label until a break or end of switch is encountered.
Q.243Easy
Which control flow statement allows jumping to a labeled location in C?
Answer: B
goto statement allows unconditional jump to a labeled location. Though generally discouraged, it's valid in C and sometimes used for error handling.
Q.244Medium
Consider the code:
int x = 5;
if(x > 3)
if(x > 10)
printf("A");
else
printf("B");
Which statement best describes the output?
Answer: B
The else binds to the nearest if statement (inner if). Since x=5 is not > 10, the inner condition fails and else executes, printing 'B'.
Q.245Medium
What does the following code segment accomplish?
int i = 0;
while(i < 5) {
if(i == 2) {
i++;
continue;
}
printf("%d ", i);
i++;
}
Answer: B
When i==2, continue skips printf but still increments i. So 2 is not printed. Output: 0 1 3 4
Advertisement
Q.246Easy
In the ternary operator expression (condition ? expr1 : expr2), if condition is true, which expression evaluates?
Answer: B
Ternary operator is short-circuit. If condition is true, expr1 evaluates and expr2 is skipped. If false, expr2 evaluates and expr1 is skipped.
Q.247Medium
Which of the following is a valid use of the switch statement in C?
Answer: C
Switch works with integral types only. Multiple case labels before statements (fall-through) is valid. Strings and floats are not allowed.
Q.248Medium
Analyze: What is printed by this code?
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= i; j++) {
printf("%d", i);
}
printf(" ");
}
Answer: D
Outer loop: i=1,2,3. Inner loop repeats i times. When i=1: print '1' once. i=2: print '2' twice. i=3: print '3' thrice. Each iteration separated by space.
Q.249Medium
What happens when you use break in a nested loop structure?
Answer: B
break statement exits only the innermost loop it's in. To break outer loop, you need additional logic or goto statement.
Q.250Easy
Which statement is true about the goto statement in modern C programming?
Answer: B
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.
Q.251Medium
Consider the code:
int x = 0;
while(1) {
if(++x > 3) break;
printf("%d ", x);
}
What is the output?
Answer: B
Pre-increment ++x happens first. When x becomes 4, condition is true and loop breaks. Values printed: x=1,2,3 (before x reaches 4).
Q.252Medium
What is the key distinction between if-else and switch statements in terms of evaluation?
Answer: B
switch typically uses jump tables for efficient multi-way branching with integral values, while if-else evaluates boolean expressions sequentially.
Q.253Medium
What does the following code segment do?
int i = 0;
do {
printf("%d ", i);
} while(++i < 3);
Answer: A
do-while executes body first (prints 0), then checks ++i < 3. Loop continues for i=1,2. When i=3, condition fails. Output: 0 1 2
Q.254Easy
In a for loop for(int i = 0; i < 5; i++), at what point does the increment happen?
Answer: B
For loop order: initialization → check condition → execute body → increment → repeat from condition check.
Q.255Hard
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");
Answer: A
continue skips current iteration but doesn't affect outer loop. After nested loops complete, 'Done' prints once.
Q.256Medium
Consider:
switch(x) {
case 1:
case 2:
case 3: printf("A"); break;
default: printf("B");
}
What advantage does stacking cases provide?
Answer: B
Stacking cases (without break between them) allows multiple values to execute the same code block, reducing redundancy.
Q.257Hard
What will this code do?
int i = 0;
while(i < 5) {
printf("%d ", i++);
if(i == 3) break;
}
Answer: A
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
Q.258Hard
Which statement best describes the 'dangling else' problem in C?
Answer: B
Dangling else refers to ambiguity when else could bind to multiple if statements. C resolves this by binding else to the nearest if.
Q.259Medium
What does continue do in a for loop with multiple statements in body?
Answer: A
continue skips all remaining statements in the current loop iteration and jumps to the increment/condition check for the next iteration.
Q.260Easy
In a nested loop structure, which statement is used to skip the current iteration of the innermost loop only?
Answer: B
continue skips the current iteration of the innermost loop and moves to the next iteration, while break exits the loop entirely.