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.222Medium
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.223Easy
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.
Q.224Medium
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.225Medium
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.
Advertisement
Q.226Medium
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.227Medium
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.228Easy
Analyze the code: What happens when condition in if statement is false?
if(0) { statements; } else { statements; }
Answer: B
When the if condition is false (0 is false in C), the else block executes. This is basic if-else control flow.
Q.229Medium
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.
Q.230Easy
In the ternary operator (? :), what happens if the condition is false?
Answer: B
The ternary operator syntax is: condition ? true_expr : false_expr. If condition is false, false_expr is evaluated.
Q.231Easy
What will be the result?
for(int i = 1; i <= 4; i++) {
if(i == 3) break;
printf("%d ", i);
}
Answer: B
When i=3, break executes and exits the loop immediately. Only i=1 and i=2 are printed. Output: 1 2
Q.232Medium
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.233Easy
What is printed by this code?
int x = 1, y = 2;
if(x > y) printf("A");
else if(x < y) printf("B");
else printf("C");
Answer: B
Since x=1 and y=2, x>y is false, but x<y is true, so 'B' is printed. The else if condition matches.
Q.234Medium
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.235Medium
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.236Medium
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.237Easy
Which statement best describes the difference between break and continue?
Answer: A
break completely exits/terminates the loop. continue skips the rest of current iteration and jumps to the next iteration.
Q.238Easy
What value does the ternary operator return?
int result = (5 > 3) ? 10 : 20;
Answer: C
The condition (5 > 3) is true, so the ternary operator evaluates and returns the true expression value: 10.
Q.239Hard
What is the output of this complex nested loop?
for(int i = 1; i <= 2; i++) {
for(int j = 1; j <= 2; j++) {
if(i == j) continue;
printf("%d%d ", i, j);
}
}
Answer: B
continue skips when i==j. i=1: prints 12 (skips 11); i=2: prints 21 (skips 22). Output: 12 21
Q.240Easy
Which of the following control flow statements will cause a compilation error in C?
Answer: A
break and continue statements can only be used within loops or switch statements. Using break outside these constructs results in a compilation error.