What are C Control Statements Quiz- Embedded C Programming Training In Hyderabad

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

PREV_LEC | NEXT_LEC

Free C Programming Course · Control Statements

C Control Statements Quiz

Output-prediction quiz, coding challenges and interview questions – free C tutorial practice for embedded C programming

C Control Statements Quiz and Interview Prep

This C control statements quiz closes the pattern and loop-tracing section of our free C programming course. You will predict outputs, solve short coding challenges and rehearse the interview questions that hiring managers ask for embedded C programming roles. Every answer is hidden behind a click, so try on paper first. If you are working through this free embedded C course for a placement or a firmware job, this page is your self-test.

What You Will Learn

  • How to test your understanding of if, loops and nested loops
  • How to count stars in a diamond using a formula
  • How to answer common embedded C interview questions on control flow
  • How to avoid the traps covered in the last three lectures

Prerequisites

  • Lectures on star patterns, number diamonds, debugging if statements and tracing loops
  • A pencil, a notebook and no compiler until you have written your answer

Part 1: Predict the Output Quiz

Assume #include <stdio.h> and that each fragment sits inside main. Write your answer before opening the answer box.

Q1.

int a = 4;
if (a = 0) printf("X"); else printf("Y");
Show answer

Y. The condition assigns 0 to a and the value 0 is false.

Q2.

int i = 2;
if (i > 1);
printf("Z");
Show answer

Z. The semicolon is the whole body of the if; the printf always runs.

Q3.

int a = 5, b = 0;
if (a || b && 0) printf("1"); else printf("0");
Show answer

1. AND binds tighter, giving a || (b && 0). Since a is non-zero, the OR is true (and b && 0 is not even evaluated).

Q4.

int n = 0;
do { n += 2; } while (n < 2);
printf("%d", n);
Show answer

2. The body runs once, n becomes 2, and 2 < 2 is false.

Q5.

int c = 0;
for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= i; j++)
        c++;
printf("%d", c);
Show answer

6. The inner loop runs 1, 2 and 3 times: 1 + 2 + 3.

Q6.

int x = 3;
while (x--) printf("%d ", x);
Show answer

2 1 0 – the post-decrement tests the old value, but the body sees the new one. When x is 0 the test fails and x becomes -1 afterwards.

Q7.

int s = 0;
for (int i = 0; i < 5; i++) {
    if (i == 2) continue;
    s += i;
}
printf("%d", s);
Show answer

8. 0 + 1 + 3 + 4; the pass with i = 2 is skipped.

Q8.

int p = 2, q = 0;
if (p == 2)
    if (q == 1) printf("A");
else
    printf("B");
Show answer

B. The else pairs with the nearest if, which is q == 1.

Part 2: Diamond Pattern Questions

QuestionThink about
Q9. How many rows does a star diamond of size n have?Upper half plus lower half share the middle row
Q10. How many stars are printed in total for n = 4?Sum 2w – 1 over all widths
Q11. What is the widest row’s star count for n = 5?2w – 1 with w = n
Show answers

Q9: 2n – 1 rows.

Q10: 1 + 3 + 5 + 7 + 5 + 3 + 1 = 25 stars. The general formula is n² + (n – 1)².

Q11: 2 x 5 – 1 = 9 stars.

Part 3: Coding Challenges

  1. Print an upright star pyramid for any n entered by the user.
  2. Print a hollow diamond using only the first and last star of each row.
  3. Print a number diamond where each row starts with the row’s width and counts down to 1, then back up.
  4. Write int count_stars(int n) that returns the number of stars in a size-n diamond without printing anything.
Hints
  • Challenge 1: blanks are n - r, stars are 2r - 1.
  • Challenge 2: print a star only when the column is the first or last of that row; blanks otherwise.
  • Challenge 3: reuse the width index w and loop d from w down to 1.
  • Challenge 4: loop over rows and add 2w - 1 for each width, then compare with n * n + (n - 1) * (n - 1).
Self-check routine for every quiz question
1Predict on paper
2Open the answer
3Compile and run to confirm
4Note why you were wrong

Part 4: Embedded C Interview Questions on Control Flow

1. What is the difference between = and == in an if condition?

Show answer

The single equals assigns and yields the assigned value; the double equals compares. A mistaken assignment changes the variable and usually makes the condition always true or always false.

2. Why do coding standards such as MISRA C insist on braces?

Show answer

Without braces only one statement is controlled, which causes dangling else and misplaced-statement bugs when code is edited later.

3. When would you choose do-while over while?

Show answer

When the body must run at least once, for example reading a sensor before checking its validity.

4. How do you prevent a polling loop from hanging forever?

Show answer

Add a timeout counter or a watchdog service, and exit with an error code if the hardware never responds.

5. What is short-circuit evaluation and why does it matter?

Show answer

In && and || the right operand is skipped when the left operand already decides the result. It allows guards such as ptr && ptr->ready and means side effects on the right may not occur.

6. What does an else attach to?

Show answer

The nearest preceding if that has no else, regardless of indentation.

7. Is while (1) acceptable in firmware?

Show answer

Yes. The main super-loop of many bare-metal systems is an intentional infinite loop; the key is that every other exit path and watchdog servicing is deliberate.

8. What is the difference between break and continue?

Show answer

break leaves the innermost loop or switch; continue skips the rest of the current pass and goes to the next condition test (or the update part in a for loop).

9. How many times does for (int i = 0; i < n; i++) test its condition?

Show answer

n + 1 times: n true results and one final false result.

10. Why is goto sometimes still used in kernel and driver code?

Show answer

For a single cleanup path on error. It avoids repeating release code and nested ifs, while loops should still use structured statements.

11. What happens when a loop variable is an unsigned type and the loop counts down to below zero?

Show answer

An unsigned value wraps around to a very large number, so a condition like i >= 0 is always true and the loop never ends.

12. How would you print a diamond of size n with a single outer loop?

Show answer

Loop 2n – 1 rows, compute w = (r <= n) ? r : 2 * n - r, then print n – w blanks and 2w – 1 stars per row.

Common Mistakes Seen in Quizzes

MistakeWhy it happensCure
Trusting indentationReading code like PythonLook for braces and semicolons
Forgetting post-decrement timingTest and update happen in one expressionTrace test, then update, then body
Off-by-one in loopsConfusing less-than with less-or-equalTest with n = 1 and n = 2
Ignoring operator precedenceMixing && and || without parenthesesAdd parentheses

Summary and Key Takeaways

  • Predict first, run second – this builds real understanding.
  • Most output-prediction traps come from assignment in conditions, stray semicolons, missing braces and precedence.
  • Loops are traced with tables; patterns are designed with row formulas.
  • Interviewers use control-flow questions to check fundamentals for embedded C programming jobs.

Well done on finishing this part of the free C programming course. Re-do the quiz in a day or two without looking at the answers; spaced repetition makes these concepts stick.

Frequently Asked Questions

Where can I practise C control statements for free?

This free C tutorial and the other lectures in the EmbeddedPathashala free embedded C course include quizzes, coding challenges and interview questions.

How do I prepare for embedded C interview questions?

Practise output prediction, understand precedence and volatile-safe loops, and be ready to explain how you avoid infinite loops.

Should I run the code before answering?

No. Predict on paper first, then compile and compare; the difference shows what you still need to learn.

Are pattern programs asked in interviews?

Often as warm-up questions, since they test nested loops and formula thinking quickly.

What should I learn after control statements?

Move on to functions, arrays and pointers, which are the next chapters in the free C programming course.

Finish the Free Embedded C Course

Keep building your embedded C programming skills with the next chapter of the free C programming course.

PREV_LEC | NEXT_LEC

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