C Loops Quiz and Interview- Learn C Programming Online Free

🚀 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 →

Free C Programming Course • Chapter 5 • Lecture 18

C Loops Quiz and Interview

Test yourself on for loops, nested loops, the comma operator, digit programs, and infinite loops, with embedded C interview questions and answers.

This final lecture of the loops chapter in our free C programming course is a quiz lab. You will answer multiple choice questions, trace outputs by hand, study common embedded C programming interview questions, and try three coding challenges. Everything here is covered by lectures 13 to 17 of this free embedded C course, so use it as a self-test before moving on. Open each answer only after you have committed to your own.

What You Will Learn

  • How to check your understanding of optional for expressions, the comma operator, and nesting.
  • How to trace loop output on paper, a skill interviewers test constantly.
  • How to explain loop concepts in your own words during a technical interview.
  • Three small programs to practise on your own machine.

How to Use This Quiz

  1. Cover the answer, and write your choice on paper.
  2. Click the answer line and compare.
  3. If you were wrong, return to the lecture named in the explanation.
  4. Score yourself at the end using the table below.
Quiz Lab Plan
Part A: 10 MCQs Part B: 4 output traces Part C: 10 interview questions Part D: 3 coding challenges

Part A: Multiple Choice Questions

Q1. Which statement is valid C?

  1. for (;)
  2. for (;;)
  3. for (i = 0 i < 5 i++)
  4. for ()
Show answer

(b). Both semicolons are mandatory but all three expressions may be empty. See lecture 13.

Q2. What is the value of x after int x = (2, 5, 7);?

  1. 2
  2. 5
  3. 7
  4. 14
Show answer

(c). The comma operator returns the right-most value. See lecture 14.

Q3. How many times does the first printf run? for (i = 0; i < 3; i++); printf("hi ");

  1. 0
  2. 1
  3. 3
  4. Infinite
Show answer

(b). The semicolon after the parentheses is an empty loop body, so the loop runs three empty iterations and then printf runs once. Afterwards i is 3.

Q4. What does while (n = 2) do?

  1. Runs while n equals 2 and then stops
  2. Never runs
  3. Runs forever and overwrites n
  4. Compile error
Show answer

(c). The assignment evaluates to 2, which is always true. GCC only gives a warning. See lecture 17.

Q5. An outer loop runs 5 times and an inner loop with a fixed limit runs 6 times for each. How many times does the inner body execute?

  1. 11
  2. 30
  3. 36
  4. 56
Show answer

(b). 5 multiplied by 6 equals 30. See lecture 15.

Q6. A break inside the inner loop of two nested loops does what?

  1. Exits both loops
  2. Exits only the inner loop
  3. Exits the program
  4. Skips one iteration
Show answer

(b). It leaves only the innermost enclosing loop. continue is the one that skips an iteration.

Q7. What is wrong with for (unsigned i = 3; i >= 0; i--)?

  1. Nothing
  2. It runs 3 times
  3. It never ends, because an unsigned value is never below zero
  4. Compile error
Show answer

(c). After 0 the value wraps to a very large number, so the condition stays true.

Q8. Which part of a for loop executes exactly once?

  1. The condition
  2. The update expression
  3. The initialisation
  4. The body
Show answer

(c). The initialisation runs once before the first test.

Q9. What is the repeated digit sum (digital root) of 99?

  1. 18
  2. 9
  3. 1
  4. 0
Show answer

(b). 9 + 9 = 18, and 1 + 8 = 9. The formula gives 1 + (99 – 1) % 9 = 9. See lecture 16.

Q10. Is 407 an Armstrong number?

  1. Yes, 64 + 0 + 343 = 407
  2. No
  3. Yes, 16 + 0 + 49 = 65
  4. Only in binary
Show answer

(a). 4³ is 64, 0³ is 0, and 7³ is 343, which add up to 407.

Part B: Trace the Output

Work out the output by hand before opening the answer. Every answer below was confirmed by compiling and running the code.

T1.

for (int i = 0, j = 4; i < j; i++, j--)
    printf("%d%d ", i, j);
Show output
04 13 

When i is 2 and j is 2 the test i < j fails, so only two pairs print.

T2.

for (int i = 1; i <= 3; i++)
{
    for (int j = i; j <= 3; j++)
        printf("%d", j);
    printf("-");
}
Show output
123-23-3-

The inner loop starts at the current outer value, so each row is shorter.

T3. What is the final value of s?

int s = 0;
for (int i = 1; i <= 4; i++)
    for (int j = 1; j <= i; j++)
        s++;
Show answer

10. The inner loop runs 1, 2, 3, and 4 times, and 1 + 2 + 3 + 4 equals 10.

T4. What does this print?

int c = 0;
for (;;)
{
    if (++c == 3)
        break;
}
printf("%d\n", c);
Show output
3

The pre-increment makes c equal to 3 on the third pass, which triggers the break.

Part C: Embedded C Interview Questions

Basic Level

1. What are the three parts of a for loop, and are they all required?

Initialisation, condition, and update. None of them is required, but the two semicolons are. An empty condition counts as true.

2. What is the difference between while and do while?

A while loop tests before the body, so it may run zero times. A do while loop tests after the body, so it always runs at least once.

3. What is the comma operator and where do you use it?

It evaluates its operands left to right and returns the right-most value. Its most accepted use is initialising and updating several loop variables in a for header.

4. How do you leave two nested loops at once?

A break only leaves the inner loop. Use a flag checked by the outer loop, move the loops into a function and return, or use a carefully placed goto.

Intermediate Level

5. Why does an embedded program usually end with an infinite loop?

There is no operating system to return to. The firmware must keep reading inputs and driving outputs while the device is powered, so main ends with for (;;).

6. for (;;) or while (1): which do you prefer and why?

They are equivalent. Many embedded standards prefer for (;;) because some static analysers warn about a constant controlling expression in while (1). The key is to be consistent and to comment the exit conditions.

7. Why can for (unsigned i = n; i >= 0; i–) hang?

An unsigned variable is never negative, so i >= 0 is always true. When i is 0 and decrements, it wraps to the maximum value. Write for (unsigned i = n; i-- > 0; ) or restructure the loop instead.

8. How do you wait for a hardware flag safely?

Declare the register access volatile so the compiler re-reads it, and add a timeout counter so a failed peripheral cannot freeze the system. Refresh the watchdog if the wait is long.

Advanced Level

9. What is the time cost of two nested loops over n items each?

The inner body executes n × n times, so the cost grows with the square of n. On a microcontroller with a tight real-time deadline, keep such loops out of interrupt handlers and consider a better algorithm.

10. How would you convert an unsigned integer to decimal text without printf?

Use the digit loop from lecture 16: take n % 10, convert to a character by adding '0', store it in a buffer from the end backward, divide n by 10, and repeat until n is zero. Handle the value 0 as a special case.

Part D: Coding Challenges

#TaskHintExpected result for the sample
1Print a reversed right triangle of height 4Make the inner limit shrink as the outer variable growsRows of 4, 3, 2, and 1 stars
2Count the digits and reverse the digits of 12345Use % 10 and / 10, and build the reverse with rev = rev * 10 + d5 digits, reversed 54321
3Find all two-digit numbers whose digits add to 9One loop plus a digit sum, or a nested loop over tens and units18, 27, 36, 45, 54, 63, 72, 81, 90

Compile each solution with gcc -std=c17 -Wall -Wextra, and compare against the expected result in the table.

Score Yourself

Part A scoreWhat to do next
9 to 10Excellent. Move to the next chapter.
6 to 8Review the lectures named in your wrong answers.
Below 6Re-read lectures 13 to 17 and run every program yourself.

Common Mistakes Seen in Interviews

  • Saying that break leaves all nested loops.
  • Forgetting that the comma operator in a condition ignores every expression but the last.
  • Counting down with an unsigned variable and a condition of >= 0.
  • Putting a semicolon after the for or while header.
  • Not mentioning the watchdog and volatile when asked about embedded loops.

Best Practices for Interviews

  • Trace loops on paper with a small table of variable values.
  • Say your assumptions aloud, such as the integer size and signedness.
  • Mention compiler warnings and testing: it shows professional habits.
  • Connect each answer to a firmware example, like a super loop or a keypad scan.

Summary and Key Takeaways

  • All for-loop expressions are optional but both semicolons are mandatory.
  • The comma operator returns its right-most value and is best kept in for headers.
  • Nested loops multiply iteration counts, and break leaves only the inner loop.
  • Digit loops use % 10 and / 10.
  • Accidental infinite loops come from wrong direction, missing update, or assignment in the condition; intentional ones power firmware.

You have completed the loop concepts of this free embedded C course chapter. Keep practising, and continue with the next chapter of our free C programming course on EmbeddedPathashala.

Frequently Asked Questions

Are loop questions common in embedded C interviews?

Yes. Interviewers use loops to test trace skills, understanding of unsigned arithmetic, volatile flags, and the firmware main loop.

How should I practise tracing loops?

Draw a table with one column per variable and one row per iteration, and update it exactly as the compiler would.

Do I need hardware to follow this free embedded C course?

No. A computer with GCC is enough for all examples. Hardware examples are written so you can simulate them first.

Which loop should a beginner prefer?

Use for when the number of iterations is known, while when it depends on a condition, and do while when the body must run at least once.

Where can I find more free C programming practice?

Continue through the chapters of this free C programming course, and rewrite each program with your own variations to build confidence.

Continue the Free C Programming Course

Great work finishing the loops chapter. Explore the full course index for the next chapter, and share this free embedded C course with a friend.

💬 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 →