What is the output of the following code?
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 2; j++) {
if(i == 2 && j == 1) break;
printf("%d%d ", i, j);
}
}
Answer: C
The break statement only exits the inner loop. When i=2 and j=1, inner loop breaks. i=3 executes normally. Output: 11 31 32
Q.22Medium
In a nested loop, if we use 'goto' statement, where does the control transfer?
Answer: A
'goto' transfers control to the label specified, which can be anywhere in the program. It can jump out of nested structures to the labeled location.
Q.23Medium
What is the output?
int x = 5;
switch(x) {
case 5: printf("Five");
case 6: printf("Six");
default: printf("Default");
}
Answer: B
Without a break statement after case 5, control falls through to case 6 and default. This is called fall-through behavior in switch statements.
Q.24Medium
What will be printed?
for(int i = 0; i < 3; i++) {
if(i == 1) goto skip;
printf("%d ", i);
}
skip: printf("End");
Answer: B
When i=1, goto skip jumps to the skip label, skipping the printf. i continues to 2 in the loop. Output: 0 2 End
Q.25Medium
What is the final value of x?
int x = 0;
for(int i = 1; i <= 5; i++) {
if(i % 2 == 0) continue;
x += i;
}
Answer: A
continue skips even values. x accumulates: 1(i=1) + 3(i=3) + 5(i=5) = 9. Values i=2,4 are skipped.
Advertisement
Q.26Medium
In a switch statement with integer cases, can a case have a floating-point constant?
Answer: B
Case labels must be constant integral expressions (int, char, etc.). Floating-point constants are not allowed as case labels.
Q.27Medium
What happens with this nested if?
if(1) if(0) printf("A"); else printf("B");
Answer: B
The else binds to the nearest if (inner if). Outer if(1) is true, inner if(0) is false, so else executes printing 'B'.
Q.28Medium
Predict the output:
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
if(i + j == 1) continue;
printf("Done");
Answer: A
continue only affects the inner loop, not the outer. The loops complete normally and 'Done' is printed once after both loops finish.
Q.29Medium
What is the behavior of this code?
int i = 0;
while(1) {
i++;
if(i > 5) break;
}
Answer: B
while(1) creates an infinite loop. i increments until i > 5 (i becomes 6), then break exits. Loop runs 6 iterations.
Q.30Medium
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.31Medium
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.32Medium
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.33Medium
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.34Medium
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.35Medium
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.36Medium
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.37Medium
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.38Medium
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.39Medium
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.40Medium
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.