What will be the output of the following C code?
int x = 5;
if (x > 3)
printf("A");
else
printf("B");
Answer: A
Since x = 5 and 5 > 3 is true, the if block executes and prints 'A'.
Q.2Easy
Which of the following is NOT a valid control flow statement in C?
Answer: B
foreach is not a standard C control flow statement. C has if-else, switch, for, while, do-while, and goto.
Q.3Easy
What is the output of this code?
for (int i = 0; i < 3; i++)
printf("%d ", i);
Answer: A
The loop runs from i=0 to i=2 (i < 3), printing '0 1 2 '.
Q.4Easy
What is the difference between 'break' and 'continue' in loops?
Answer: A
break terminates the loop entirely, while continue jumps to the next iteration without executing remaining statements.
Q.5Easy
Analyze the ternary operator: int x = (5 > 3) ? 10 : 20; What is the value of x?
Answer: A
Since 5 > 3 is true, the ternary operator returns 10, so x = 10.
Advertisement
Q.6Easy
What is the behavior of goto in C?
Answer: A
goto transfers program control to a labeled location, though it's discouraged for code readability.
Q.7Easy
Which statement is used to skip remaining iterations and move to next iteration?
Answer: B
continue is the C keyword that jumps to the next iteration of the loop.
Q.8Easy
In C, which control flow statement is used to terminate a loop prematurely?
Answer: A
The 'break' statement is used to exit or terminate a loop immediately. 'continue' skips the current iteration, 'exit' terminates the entire program.
Q.9Easy
What is the primary difference between a while loop and a do-while loop?
Answer: A
A do-while loop executes the body at least once before checking the condition. A while loop checks the condition first, so it may not execute at all.
Q.10Easy
In a nested loop structure, which statement will skip only the inner loop's current iteration?
Answer: B
The 'continue' statement skips the remaining statements of the current iteration and moves to the next iteration. In nested loops, it affects only the innermost loop.
Q.11Easy
In the expression (x > 5) ? (y = 10) : (y = 20), what does y equal if x is 3?
Answer: B
The ternary operator evaluates the condition (x > 5). Since 3 is not greater than 5, the expression evaluates to false, and the false part executes, setting y to 20.
Q.12Easy
Which control flow structure allows execution of code only if a certain condition is met?
Answer: B
The if statement executes a block of code conditionally. Loops repeat code, and switch cases are for multi-way branching based on a single expression.
Q.13Easy
In a switch statement, which keyword provides a default case if no cases match?
Answer: B
The 'default' keyword in a switch statement provides code to execute when no case values match the switch expression.
Q.14Easy
What is the correct syntax for a for loop that iterates from 0 to 9?
Answer: B
for(int i=0; i<10; i++) correctly iterates from 0 to 9 (10 iterations total). Using i<=10 would iterate 11 times, and starting from 1 would skip 0.
Q.15Easy
Which control structure allows checking multiple conditions in sequence using else-if?
Answer: C
The if-else-if chain allows testing multiple different conditions sequentially. switch is used for a single expression with multiple values.
Q.16Easy
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.17Easy
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.18Easy
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.19Easy
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.20Easy
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.