Question 2Easy
What is the output?
#include <stdio.h>
int main(void) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0)
continue;
sum += i;
}
printf("%d\n", sum);
return 0;
}
- 25
- 30
- 55
- 20
Answer: A. 25
Explanation
continue skips the even numbers, so only 1 + 3 + 5 + 7 + 9 = 25 is added.
Step-by-step solution
- The loop runs from i = 1 to i = 10.
- If i is even, continue skips the rest of the loop.
- Only odd numbers are added to sum: 1 + 3 + 5 + 7 + 9 = 25.
- Final value of sum is 25.
- Printf prints 25.
Why the other options are wrong
- B. Thirty is the sum of the even numbers 2 to 10; continue skips even i, so they are never added.
- C. Fifty-five adds all numbers 1 to 10, but continue skips the even ones, so only odd values count.
- D. Twenty is wrong; the odd values 1, 3, 5, 7 and 9 add up to 25, not 20.
Tip: Remember that continue skips the current iteration and moves to the next.
Question 3Easy
What is the output?
#include <stdio.h>
int main(void) {
int x = 2;
switch (x) {
case 1: printf("A");
case 2: printf("B");
case 3: printf("C"); break;
default: printf("D");
}
printf("\n");
return 0;
}
- BCD
- BC
- ABC
- B
Answer: B. BC
Explanation
Execution jumps to case 2 and, with no break, falls through into case 3; the break there stops it before default.
Step-by-step solution
- X is 2, so execution jumps to case 2.
- Case 2 prints B and has no break, so it falls through.
- Case 3 prints C, and its break stops the switch before default.
- The output is BC, which is option B.
Why the other options are wrong
- A. Default is skipped because a case matched and break stopped the switch first.
- C. Case 1 is not run, so A is not printed.
- D. C is printed too, because the fall-through runs case 3.
Tip: Without a break, a switch falls through into the next case label.
Question 4Easy
What is the key difference between a while loop and a do-while loop?
- while always executes its body at least once
- do-while cannot use break
- while cannot have a compound body
- do-while always executes its body at least once
Answer: D. do-while always executes its body at least once
Explanation
do-while checks its condition after each iteration, so the first iteration is unconditional; while checks before each iteration and may run zero times.
Step-by-step solution
- Do-while runs the loop body first, then checks the condition.
- While checks the condition before running the loop body.
- So do-while executes at least once regardless of the condition.
- This makes do-while different from while in execution order.
- The correct answer is do-while always executes its body at least once.
- Final Answer: D (do-while always executes its body at least once).
Why the other options are wrong
- A. A while loop checks the condition first, so its body may run zero times; it does not always run once.
- B. Break works inside a do-while loop, so a do-while loop can use break just like a while loop.
- C. A while loop can have a compound body in braces; braces work for any loop body in C.
Tip: Remember do-while runs first, then checks; while checks first, then runs.
Question 5Medium
What is the output?
#include <stdio.h>
int main(void) {
int n = 1200, r = 0;
while (n > 0) {
r = r * 10 + n % 10;
n /= 10;
}
printf("%d\n", r);
return 0;
}
- 12
- 2100
- 0021
- 21
Answer: D. 21
Explanation
Reversing 1200 produces digits 0, 0, 2, 1, but leading zeros vanish in an int: r goes 0, 0, 2, 21.
Step-by-step solution
- n = 1200, r = 0.
- First loop: n % 10 = 0, r = 0 * 10 + 0
= 0.
- Second loop: n = 120, n % 10 = 0, r = 0 * 10 + 0
= 0.
- Third loop: n = 12, n % 10 = 2, r = 0 * 10 + 2
= 2.
- Fourth loop: n = 1, n % 10 = 1, r = 2 * 10 + 1
= 21.
Why the other options are wrong
- A. Twelve drops digits; reversing 1200 builds the digits 0, 0, 2, 1, which is the number 21.
- B. Two thousand one hundred would need the zeros kept at the end, but the reversal gives 21.
- C. Leading zeros are never printed for an int value, so the reversed result prints as 21, not 0021.
Tip: Remember that integers automatically drop leading zeros when reversed.
Question 7Medium
What is the output?
#include <stdio.h>
int main(void) {
int x = 7;
switch (x % 4) {
case 1: printf("one");
case 2: printf("two");
}
printf("end\n");
return 0;
}
- onetwoend
- end
- oneend
- twoend
Answer: B. end
Explanation
7 % 4 is 3, which matches no case, and there is no default, so the switch body is skipped entirely.
Step-by-step solution
- X % 4 is 7 % 4 = 3.
- No case label is 3, and there is no default.
- So the switch body is skipped and nothing in it prints.
- The final printf prints end, which is option B.
Why the other options are wrong
- A. One and two need case 1 and case 2 to match, but the value is 3.
- C. One needs case 1 to match, but the value is 3.
- D. Two needs case 2 to match, but the value is 3.
Tip: A switch with no matching case and no default does nothing.
Question 8Medium
What happens when this code runs?
int i = 0;
while (i < 5) {
if (i == 2)
continue;
printf("%d", i);
i++;
}
- It prints 0134
- It prints 01 and stops
- It prints 01234
- It prints 01 and then loops forever
Answer: D. It prints 01 and then loops forever
Explanation
When i reaches 2, continue jumps back to the condition without executing i++, so i stays 2 forever. In a for loop, continue would still run the increment expression.
Step-by-step solution
- i = 0 is not 2, so 0 is printed and i becomes 1.
- i = 1 is printed and i becomes 2.
- When i = 2, continue jumps back to the test without running i++.
- I stays 2 forever, so the loop never ends, which is option D.
Why the other options are wrong
- A. 2 is never printed, because continue skips the print and the loop never reaches 3 or 4.
- B. The loop does not stop, because i stays 2 and the test stays true.
- C. Values 2, 3 and 4 are never printed, because the loop is stuck at i = 2.
Tip: In a while loop, continue jumps to the test and skips any update placed after it; in a for loop the update still runs.
Question 9Hard
What is the output?
#include <stdio.h>
int main(void) {
int s = 0;
for (int i = 0; i < 5; i++) {
switch (i % 3) {
case 0: s += 1;
case 1: s += 10; break;
default: continue;
}
s += 100;
}
printf("%d\n", s);
return 0;
}
- 542
- 222
- 442
- 440
Answer: C. 442
Explanation
i = 0 and 3 (case 0): +1, fall through +10, then +100 = 111 each.
i = 1 and 4 (case 1): +10 +100 = 110 each.
i = 2 hits default, and continue skips the +100.
Total 222 + 220 = 442.
Step-by-step solution
- i=0: case 0 falls into case 1: 11, then +100 = 111.
- i=1: +10 = 121,
+100 = 221.
- i=2 continue.
i=3: 332.
i=4: 442.
Why the other options are wrong
- A. 542 over-counts.
- B. 222 is for fewer iterations.
- D. 440 ignores last additions.
Tip: Trace each i.
Question 10Hard
What is the output?
#include <stdio.h>
int main(void) {
int i;
for (i = 0; i++ < 3; )
printf("%d", i);
printf(" %d\n", i);
return 0;
}
- 0123 4
- 123 3
- 012 3
- 123 4
Answer: D. 123 4
Explanation
The condition compares the old value and then increments: 0<3 (i=1, print 1), 1<3 (print 2), 2<3 (print 3), then 3<3 is false but i still becomes 4.
Step-by-step solution
- i++ < 3 compares then increments.
- Prints 1, 2, 3.
- At the last check, i becomes 4.
Why the other options are wrong
- A. Does not print 0.
- B. i ends at 4.
- C. Does not start from 0.
Tip: Final i is 4.