C Control Statements Quiz- Free Online C Programming Course

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

Control Statements | Free C Programming Course

C Control Statements Quiz

Fourteen MCQs, five coding challenges and fourteen interview questions, all answers verified with gcc.

This C control statements quiz closes our set of lectures on loops, switch and number programs. It contains 14 multiple choice questions with explained answers, 5 hands-on programming challenges with expected output, and 14 interview questions with model answers that are regularly asked in embedded C programming interviews. Every output-prediction question was compiled and run with gcc, so the answers are checked, not guessed. This is the final lecture of this block in our free C programming course: take your time, write answers on paper first, and only then open the answer boxes.

What You Will Learn

  • How well you can trace loops, continue, break and switch without running code
  • Which control-statement rules you still confuse, so you know what to revise
  • How to write short programs from a specification and check them against sample output
  • How to answer the loop and switch questions that interviewers ask freshers and embedded engineers

Prerequisites

Work through the lectures on the continue statement, variable scope in loops, common loop mistakes, switch rules, digit programs and number logic programs first. You will also need gcc installed so you can verify your own solutions to the challenges. This free C tutorial assumes you can compile with gcc -Wall -Wextra.

How to Use This C Control Statements Quiz

Study loop for the quiz

Read question→Answer on paper→Open the answer→Note the rule you missed→Re-run the code yourself
Score out of 14What it meansWhat to do next
13 to 14Strong control-flow skillsMove on to the challenges and interview section
10 to 12Good, with a few gapsRevisit the lecture for each question you missed
6 to 9Basics are present, details are shakyRedo the trace tables in the loop and switch lectures
0 to 5Control flow needs more practiceRestart the chapter and type every example yourself

Tip: for output questions, write the value of every variable after each iteration. Guessing the final line without a trace is how most marks are lost.

C Control Statements Quiz: Multiple Choice Questions

Question 1. In a for loop, where does continue transfer control?

  1. To the first statement of the loop body
  2. To the update expression of the for header
  3. Out of the loop
  4. To the end of the function
Show answer

Answer: b. It jumps to the update expression, then the test is evaluated. That is why a for loop with continue still advances its counter.

Question 2. What happens here when n reaches 4?

int n = 0;
while (n < 8) {
    if (n == 4)
        continue;
    n++;
}
  1. The loop skips 4 and finishes normally
  2. The loop becomes infinite
  3. Compile error
  4. The loop ends immediately
Show answer

Answer: b. In a while loop, continue goes straight back to the test. Because n++ sits after the continue, n stays 4 and the same branch is taken for ever.

Question 3. What does this program print?

int n;
for (n = 0; n < 6; n++) {
    if (n % 2)
        continue;
    printf("%d ", n);
}
  1. 0 1 2 3 4 5
  2. 0 2 4
  3. 1 3 5
  4. 2 4
Show answer

Answer: b. When n is odd, n % 2 is nonzero and continue skips the printf. Only 0, 2 and 4 are printed.

Question 4. What is printed?

int x = 1;
{
    int x = 2;
    x++;
}
printf("%d", x);
  1. 1
  2. 2
  3. 3
  4. Compile error
Show answer

Answer: a. The inner x shadows the outer one. The increment changes only the inner variable, which disappears at the closing brace, so the outer x is still 1.

Question 5. Which kind of local variable keeps its value between iterations of a loop body?

  1. An automatic variable
  2. A register variable
  3. A static variable
  4. A shadowed variable
Show answer

Answer: c. A static local is created once and lives for the whole program. Automatic locals are recreated each time the block is entered.

Question 6. What is the value of k after this loop?

int k = 0;
while (k++ < 3) {
}
printf("%d", k);
  1. 3
  2. 4
  3. 2
  4. Undefined
Show answer

Answer: b. The test k++ < 3 increments after each comparison, including the last failing one (3 < 3), so k ends at 4.

Question 7. What does this code print?

int a = 0, b = 5;
if (a = b)
    printf("yes");
else
    printf("no");
  1. no
  2. yes
  3. Nothing, it fails to compile
  4. It depends on the compiler
Show answer

Answer: b. The condition is an assignment. It stores 5 in a and the value 5 is true, so the program prints yes. gcc warns with -Wall that you may have meant ==.

Question 8. Which of these is a valid case label in standard C?

  1. case n: where n is an int variable
  2. case 2.5:
  3. case 3 + 4:
  4. case "A":
Show answer

Answer: c. A case label must be an integer constant expression, and 3 + 4 is evaluated at compile time. A variable, a floating constant and a string are all invalid.

Question 9. What is printed?

int v = 2;
switch (v) {
case 1: printf("A");
case 2: printf("B");
case 3: printf("C"); break;
case 4: printf("D");
}
  1. B
  2. BC
  3. ABC
  4. BCD
Show answer

Answer: b. Execution jumps to case 2, prints B, falls through to case 3, prints C and meets break.

Question 10. What does this program print?

int i;
for (i = 0; i < 4; i++) {
    switch (i) {
    case 1: continue;
    case 2: break;
    default: printf("x");
    }
    printf("%d", i);
}
  1. x0x2x3
  2. x02x3
  3. x0123
  4. x03
Show answer

Answer: b. i = 0 prints x then 0. i = 1 hits continue, skipping the printf. i = 2 breaks out of the switch and prints 2. i = 3 prints x then 3. Result: x02x3.

Question 11. What does this digit program print?

int m = 1250, r = 0;
while (m) {
    r = r * 10 + m % 10;
    m /= 10;
}
printf("%d", r);
  1. 0521
  2. 521
  3. 1250
  4. 5210
Show answer

Answer: b. The loop builds 0, 5, 52, 521 from the digits 0, 5, 2, 1 of 1250. The leading zero of 0521 is not stored in an integer, so the output is 521.

Question 12. How many times does the body of a do-while loop run when the condition is false from the very start?

  1. Zero times
  2. Once
  3. Twice
  4. It depends on the compiler
Show answer

Answer: b. A do-while checks its condition at the end, so the body runs at least once. That is why it is used for digit counting where the input can be 0.

Question 13. Which test tells you, before multiplying, that r * x would overflow an unsigned long long (x is not zero)?

  1. r * x < r
  2. r > ULLONG_MAX / x
  3. x > r
  4. r % x == 0
Show answer

Answer: b. Dividing the maximum by x gives the largest r that is still safe, and the check happens without performing the overflowing multiplication.

Question 14. Which gcc option warns when an inner variable hides an outer variable of the same name?

  1. -Wall
  2. -Wextra
  3. -Wshadow
  4. -Wswitch
Show answer

Answer: c. -Wshadow is a separate option. It is not included in -Wall, so add it explicitly in your embedded build flags.

C Control Statements Quiz: Programming Challenges

Write each program yourself, compile it with gcc -std=c11 -Wall -Wextra and compare your output with the sample. We tested every sample with our own solutions, but we do not print the solutions here, because writing them is the point. Use the hints only if you are stuck.

#ChallengeSample inputExpected outputHint
1Print the sum of the digits of a number947525Peel digits with % 10 and / 10
2Report whether a number is a palindrome12321 then 123012321 palindrome, 1230 not palindromeReverse it and compare with the original
3Count the numbers from 1 to 100 that are divisible by 7, using continue for the othersnone14Skip when i % 7 is nonzero
4Compute n factorial in unsigned long long and report overflow instead of a wrong value20 then 2120! = 2432902008176640000, 21! overflowsCheck against ULLONG_MAX divided by the multiplier first
5Mini calculator: read lines with an operator and two numbers until q, using a switch inside a loop+ 8 5, * 6 7, / 9 0, ^ 2 3, q13, 42, error: divide by zero, unknown operatorUse default for unknown operators; check b before dividing

Stretch goal

Turn each challenge into a function with a clear return value, and write a small main that tests it with edge cases such as 0, 1 and the largest value your type can hold.

C Control Statements Interview Questions and Answers

These are the control-flow questions that come up again and again in embedded C programming interviews. Try to answer aloud before opening each one.

Q1. What is the difference between break and continue?

Show model answer

break terminates the innermost loop or switch completely. continue abandons only the current iteration and moves to the next one. Inside a switch, break leaves only the switch, while continue is legal only if a loop encloses the switch.

Q2. Where does continue jump in a for loop and in a while loop?

Show model answer

In a for loop it jumps to the update expression and then the test. In a while or do-while loop it jumps directly to the test, so any update statement must be placed before the continue to avoid an infinite loop.

Q3. Can you use continue inside a switch statement?

Show model answer

Yes, but only when the switch is inside a loop, because continue belongs to the loop. Used without a surrounding loop, gcc reports that the continue statement is not within a loop.

Q4. Why must case labels be constants?

Show model answer

The compiler builds a jump table or comparison tree at compile time, so every label must be an integer constant expression. Variables, even const-qualified ones in C, are not accepted. Use enum constants or macros instead.

Q5. What is fall-through and when is it useful?

Show model answer

Fall-through means execution continues into the next case label because no break was written. It is useful when cases should accumulate behaviour, such as log levels that include all lower levels. Mark it with a comment and enable -Wextra so unintentional cases are flagged.

Q6. What is variable shadowing and how do you catch it?

Show model answer

Shadowing is declaring a variable in an inner block with the same name as one in an outer block. The inner one hides the outer one. Compile with -Wshadow to get a warning, and avoid reusing names such as i in nested loops.

Q7. What is the difference between a static local variable and an automatic one?

Show model answer

An automatic local is created each time its block runs and has no defined value unless initialised. A static local is created once, zero-initialised by default and keeps its value for the lifetime of the program. Static locals make a function non-reentrant, which matters when interrupt handlers share the code.

Q8. Why is while (x = y) dangerous?

Show model answer

It assigns y to x and tests the assigned value, which is almost never what the author meant. gcc warns about it. If the assignment is intended, write it with explicit parentheses and a comparison, as in while ((x = y) != 0).

Q9. How do you reverse the digits of a number, and what about trailing zeros?

Show model answer

Repeatedly take the last digit with n % 10, append it with result = result * 10 + digit, and remove it with n / 10. Trailing zeros of the input become leading zeros of the result and are lost, because integers do not store leading zeros.

Q10. Why would you use do-while to count digits?

Show model answer

A do-while runs the body once before testing, so the input 0 is counted as one digit. A while loop with a nonzero test would skip the body entirely for 0.

Q11. How do you detect unsigned multiplication overflow before it happens?

Show model answer

For nonzero x, check r > MAX / x before computing r * x. If the check is true, the product would overflow. Signed overflow is undefined behaviour, so always check first.

Q12. Why do you test divisors only up to the square root of n when checking for primes?

Show model answer

Divisors come in pairs whose product is n. If one is larger than the square root, the other must be smaller, so a composite number always has a divisor at or below its square root.

Q13. How do you prevent endless loops in firmware?

Show model answer

Give every polling loop a timeout counter, read hardware registers through volatile pointers, validate loop bounds, and enable a hardware watchdog as a last line of defence so a stuck loop causes a controlled reset.

Q14. What is the difference between i++ < 3 and ++i < 3 in a loop condition?

Show model answer

With i++ < 3 the old value is compared and then i is incremented. With ++i < 3 i is incremented first and the new value is compared. Starting from 0, the first form runs the body three times, the second only twice.

Common Mistakes Seen in Quizzes and Interviews

MistakeWhy it happensHow to avoid it
Predicting the final loop value wronglyForgetting that the failing test still incrementsTrace the last iteration in a table
Saying a const int works in a case labelHabit from C++Remember that C needs a true constant expression
Forgetting fall-throughReading cases as isolated blocksLook for the break on every path
Ignoring edge inputs such as 0Testing only typical valuesTest 0, 1, maximum and just above maximum
Mixing up break and continue inside a switch in a loopBoth change flow, but differentlyAsk which statement owns the keyword

Best Practices to Remember

  • Initialise variables, bound every loop, and compile with -Wall -Wextra -Wshadow.
  • Use enums for state machines and always include a default in a switch.
  • Check for overflow before arithmetic, not after, and validate input before using it.
  • Practise tracing by hand; it is the single skill that makes interviews easier.

Summary and Key Takeaways

  • continue lands on the update in for loops and on the test in while loops.
  • Case labels are integer constants; switch fall-through is legal and needs a comment.
  • Shadowing and assignment in conditions compile silently but are caught by warnings.
  • Digit and number programs rely on % and /, safe types and edge-case checks.
  • Interview answers are strongest when you name the rule and give a tiny example.

Conclusion

A C control statements quiz is only useful if you act on the result. Note every question you missed, find the matching lecture, and re-run the example until you can predict its output without help. Then write the five challenges from scratch and compare them with the expected output. With that done, you have covered loops, continue, scope, switch and number programs in this free embedded C course, and you are ready for the chapter on functions. Keep this C control statements quiz bookmarked and retry it in a week to see how much you retained.

Frequently Asked Questions

What does this C control statements quiz cover?

It covers for, while and do-while loops, continue and break, variable scope in loops, switch rules and fall-through, digit programs, number logic programs and common loop mistakes.

Are the answers in this quiz verified?

Yes. Every output-prediction question was compiled with gcc and the printed result matches the answer given.

How should I prepare for a C interview on control statements?

Practise tracing loops by hand, know the break and continue rules, understand case label constraints and be able to write the digit and prime programs without help.

Why are the programming challenge solutions not printed?

Writing the code yourself is the learning step. The sample input and expected output let you check your own solution.

Is this quiz useful for embedded C programming roles?

Yes. Embedded interviews focus on control flow, overflow safety, switch-based state machines and loop termination, all of which appear here.

What compiler flags should I use when checking my answers?

Use gcc -std=c11 -Wall -Wextra, and add -Wshadow when working with nested scopes.

What if I score low on the quiz?

Revisit the lecture for each missed question, redo its trace table on paper and run the example again. Then retake the quiz a few days later.

Where can I continue after this quiz?

Continue with the next chapter of this free C programming course, which introduces functions and program structure.

Finish Strong With the Free C Course

This free C tutorial is designed for students who want strong embedded C programming skills. Move on to the next chapter and keep practising.

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →