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.62Medium
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
Q.63Easy
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.64Medium
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.65Medium
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.
Advertisement
Q.66Medium
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.67Easy
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.68Medium
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.69Medium
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.70Medium
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.71Easy
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.72Hard
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.73Medium
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.74Hard
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.75Hard
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.76Medium
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.77Easy
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.
Q.78Medium
What is the output of the following code?
int x = 5;
if(x > 3)
if(x < 10)
printf("Pass");
else
printf("Fail");
Answer: A
The inner if condition (x < 10) is true, so 'Pass' is printed. The else is associated with the inner if, not the outer one.
Q.79Easy
Which looping construct in C allows initialization, condition, and increment to be placed in a single line?
Answer: C
The for loop syntax is for(init; condition; increment) allowing all three components in one statement.
Q.80Easy
In which scenario would a do-while loop be preferred over a while loop?
Answer: A
do-while executes the body first, then checks the condition, ensuring at least one execution.