What will be printed?
int x = 0;
if (x = 5)
printf("True");
else
printf("False");
Answer: A
x = 5 assigns 5 to x and evaluates to 5 (non-zero), which is true. So 'True' is printed.
Q.2Medium
What is the output?
for (int i = 1; i <= 5; i++) {
if (i == 3) break;
printf("%d ", i);
}
Answer: A
When i becomes 3, break is executed, exiting the loop. So only 1 and 2 are printed.
Q.3Medium
What does the following nested if-else produce?
int a = 10, b = 20;
if (a > b)
if (b > 0)
printf("X");
else
printf("Y");
Answer: B
The else clause pairs with the inner if. Since a > b is false, the else block executes printing 'Y'.
Q.4Medium
What is the output of this switch statement?
int x = 2;
switch(x) {
case 1: printf("One"); break;
case 2: printf("Two");
case 3: printf("Three"); break;
default: printf("Other");
}
Answer: B
Case 2 matches. Since there's no break after case 2, fall-through occurs to case 3, printing 'TwoThree'.
Q.5Medium
How many times will this loop execute?
for (int i = 0; i < 5; ++i) {
if (i == 2) continue;
if (i == 4) break;
}
Answer: C
Loop runs for i=0,1,2,3. When i=4, break is executed. The body executes 4 times (iterations for i=0,1,2,3).
Advertisement
Q.6Medium
What is printed by this code?
int x = 5;
while (x-- > 0)
printf("%d ", x);
Answer: B
x-- returns 5 first, then decrements to 4. Loop runs while x > 0, printing 4, 3, 2, 1, 0.
Q.7Medium
What is the output?
int n = 1;
do {
printf("%d ", n);
n++;
} while (n > 5);
Answer: A
The do-while executes once (prints 1), then n becomes 2. Since 2 > 5 is false, the loop exits.
Q.8Medium
Determine the output:
int i = 0;
while (i < 3) {
printf("%d ", i++);
}
Answer: A
i starts at 0. Loop prints i then increments: prints 0, then 1, then 2. When i becomes 3, condition fails.
Q.9Medium
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.10Medium
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.11Medium
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.12Medium
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.13Medium
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.
Q.14Medium
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.15Medium
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.16Medium
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.17Medium
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.18Medium
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.19Medium
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.20Medium
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