What does this code segment output?
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) continue;
printf("%d ", i);
}
Answer: A
continue skips even numbers. Loop prints only odd values: 1 and 3.
Q.102Medium
Which of the following is NOT a valid use of the goto statement in C?
Answer: C
goto cannot jump across function boundaries. It can only jump within the same function to a labeled statement either forward or backward.
Q.103Medium
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.104Medium
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.105Medium
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.
Advertisement
Q.106Medium
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.107Medium
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.108Medium
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.109Medium
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.110Medium
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.111Medium
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.112Medium
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.113Medium
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.114Medium
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.115Medium
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.116Medium
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.117Medium
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.118Medium
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.119Medium
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.120Medium
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.