🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →C Quiz and Interview Questions
Practice nested if, the else if ladder and the while loop with 15 multiple choice questions, 7 output predictions, 5 challenges and 15 interview questions.
Reading a lecture is only half of learning. This practice lab for our free C programming course gives you C programming interview questions, quizzes and output puzzles that test your understanding of control statements. It belongs to a free C tutorial built for embedded C programming, and every program in it has been compiled and run so the answers are trustworthy.
What You Will Learn
- Multiple choice questions on nested if, else if and while
- How to predict the output of tricky control flow programs
- Five programming challenges to code yourself
- Fifteen interview questions with short model answers
- How to score yourself and decide what to revise
Prerequisites
- Lectures 4, 5 and 6 of Chapter 5
- gcc installed on Linux to verify your predictions
How to Use This Practice Lab
This lab revises Chapter 5 of the free C programming course: nested if, the else if ladder and the while loop. Try each question on paper first, then open the answer. The output prediction programs were compiled and run with gcc, so the answers are real. Score yourself honestly: 1 point per multiple choice question, 2 points per output prediction.
| Part | Questions | Points |
|---|---|---|
| A. Multiple choice | 15 | 15 |
| B. Output prediction | 7 | 14 |
| C. Programming challenges | 5 | Self-check |
| D. Interview questions | 15 | Self-check |
Part A: Multiple Choice Questions on C Control Statements
In C, an else is paired with:
- A. The first if in the function
- B. The nearest preceding if without an else
- C. The if with the same indentation
- D. The outermost if
Show answer
Answer: B
The compiler ignores indentation and pairs else with the closest unmatched if.
Which warning flag in gcc reports an ambiguous else?
- A. -Wdangling-else
- B. -Wunused
- C. -Wformat
- D. -Wshadow
Show answer
Answer: A
-Wdangling-else is also enabled by -Wall.
How many branches of an else if ladder run in one pass?
- A. All true branches
- B. Exactly one at most
- C. At least two
- D. Always the last one
Show answer
Answer: B
The first true condition runs and the rest are skipped.
What does the following condition do: if (x = 5)?
- A. Compares x with 5
- B. Assigns 5 to x and is always true
- C. Causes a compile error
- D. Assigns 5 and is always false
Show answer
Answer: B
The assignment expression has the value 5, which is non-zero and therefore true.
Which year is NOT a leap year?
- A. 2000
- B. 2024
- C. 2100
- D. 2400
Show answer
Answer: C
2100 is divisible by 100 but not by 400.
What value does scanf return when it reads two integers successfully?
- A. 0
- B. 1
- C. 2
- D. The first integer
Show answer
Answer: C
scanf returns the number of items converted.
In C, which value counts as false?
- A. Zero
- B. Any negative number
- C. The character ‘F’
- D. Any odd number
Show answer
Answer: A
Zero is false and every non-zero value is true.
A while loop whose condition is false on the first test runs the body:
- A. Once
- B. Zero times
- C. Infinitely
- D. Twice
Show answer
Answer: B
While is an entry-controlled loop.
What is wrong with: while (i < 3); { i++; }?
- A. Nothing
- B. Semicolon makes the body empty, causing an endless loop
- C. Missing int
- D. Braces are illegal here
Show answer
Answer: B
The semicolon is the whole loop body, so i++ is never reached.
Which statement is the idiomatic firmware super loop?
- A. for (;;;)
- B. while (1) { }
- C. if (1) { }
- D. do { } until (0)
Show answer
Answer: B
while (1) repeats forever, which is what a microcontroller main loop needs.
Without braces, how many statements after while (cond) belong to the loop?
- A. All indented ones
- B. Exactly one
- C. Two
- D. Until the next blank line
Show answer
Answer: B
Only the first statement is the loop body.
Why can a ladder be faster than separate if statements?
- A. The compiler cheats
- B. It stops evaluating after the first match
- C. It uses fewer variables
- D. It is not faster
Show answer
Answer: B
Remaining conditions are skipped after a match.
Which of these is a compound statement?
- A. x = 1;
- B. { x = 1; y = 2; }
- C. ;
- D. return;
Show answer
Answer: B
Braces group several statements into one block.
What does short-circuit evaluation of a && b mean?
- A. b is always evaluated first
- B. b is skipped when a is false
- C. a is skipped when b is true
- D. Both are always evaluated
Show answer
Answer: B
If a is false the result is already known.
A flag set by an interrupt and polled in a while loop should be declared:
- A. const
- B. volatile
- C. static only
- D. register
Show answer
Answer: B
Volatile prevents the compiler from caching the value in a register.
Part B: Predict the Output
For each program, write down the exact output before you reveal it.
Program 1:
#include <stdio.h> int main(void) { int x = 5; if (x > 3) if (x > 10) printf("A"); else printf("B"); printf("\n"); return 0; }Show output
Bx is 5, so the outer if is true; the inner x > 10 is false; the else belongs to the inner if, so B is printed.
Program 2:
#include <stdio.h> int main(void) { int n = 3; while (n--) { printf("%d ", n); } printf("\n"); return 0; }Show output
2 1 0n– tests the old value and then decrements, so the body sees 2, 1 and 0. The loop ends when the old value is 0.
Program 3:
#include <stdio.h> int main(void) { int a = 10; if (a = 0) { printf("Zero\n"); } else { printf("Nonzero\n"); } printf("a = %d\n", a); return 0; }Show output
Nonzero a = 0a = 0 assigns zero, the expression is false, so the else runs. The assignment also changed a.
Program 4:
#include <stdio.h> int main(void) { int m = 75; if (m >= 40) { printf("D\n"); } else if (m >= 55) { printf("C\n"); } else if (m >= 70) { printf("B\n"); } return 0; }Show output
D75 is at least 40, so the first branch wins and the better grades are never tested. The order is wrong.
Program 5:
#include <stdio.h> int main(void) { int x = 0; while (x++ < 3) { printf("%d ", x); } printf("\nfinal x = %d\n", x); return 0; }Show output
1 2 3 final x = 4x++ compares the old value, then increments. The test fails when x was 3, and x ends at 4.
Program 6:
#include <stdio.h> int main(void) { int year = 1900; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { printf("leap\n"); } else { printf("not leap\n"); } int lvl = 50; if (lvl > 80) printf("FULL\n"); else if (lvl > 50) printf("GOOD\n"); else printf("LOW\n"); return 0; }Show output
not leap LOW1900 is divisible by 100 but not by 400, so not leap. 50 is not greater than 80 or 50, so the final else prints LOW.
Program 7: what happens if you compile and run the following?
int main(void) { int i = 0; while (i < 3); { i++; } return 0; }Show answer
It never terminates. The semicolon is the loop body, so i++ never executes and the condition stays true. gcc warns about misleading indentation.
Part C: Programming Challenges
Write each program yourself, compile with gcc -Wall -Wextra -std=c11, and test the boundary values. Do not look up solutions before you have tried.
Median of three. Read three integers and print the middle value using only nested if. Hint: the median is the value that is neither the largest nor the smallest. Test with ties such as 4 4 9.
Temperature bands. Read a temperature in degrees Celsius and print FREEZING, COLD, MILD, WARM or HOT using an else if ladder with boundaries you choose. Reject values outside minus 50 to 150.
Days in a month. Read a month number and a year and print how many days that month has, using the leap year rule for February. Print an error for a month outside 1 to 12.
Reverse a number. Read a positive integer and print its digits in reverse order using a while loop, for example 40729 becomes 92704.
Retry with timeout. Simulate a peripheral that becomes ready after a random number of reads. Poll it with a while loop but stop and print TIMEOUT after 10 attempts.
Part D: C Programming Interview Questions on Control Statements
What is the dangling else problem and how do you avoid it?
Show answer
An else pairs with the nearest preceding if that has no else, regardless of indentation. Avoid it by using braces around every if and else body and by compiling with -Wall.
What is the difference between an else if ladder and separate if statements?
Show answer
In a ladder, the first true condition runs and the rest are skipped, so exactly one branch runs. Separate ifs evaluate every condition and can run several blocks.
Why should the order of conditions in a ladder be chosen carefully?
Show answer
The first true condition wins. A broad test placed early can make later branches unreachable, as in testing level >= 5 before level >= 80.
What is the difference between = and == in a condition?
Show answer
= assigns and yields the assigned value; == compares. if (x = 5) always runs because 5 is non-zero. Writing the constant on the left or enabling warnings catches this.
Does C check indentation to decide which statements belong to a loop?
Show answer
No. Without braces only the first statement belongs to the loop. Indentation is only for human readers.
What does while (1) mean and where is it used?
Show answer
It is a loop whose condition is always true. Embedded main functions use it as a super loop because firmware must never return.
Can a while loop execute zero times? Why?
Show answer
Yes. It is entry-controlled, so the condition is tested before the first pass.
What is short-circuit evaluation and why does it matter for hardware access?
Show answer
&& and || stop evaluating once the result is known. It matters because the right operand might read a register or dereference a pointer that is only safe when the left operand is true.
How do you write a safe polling loop on hardware?
Show answer
Read a volatile status register in a while loop, count retries or use a timer, and return an error on timeout so the system never hangs.
Why must a flag shared with an interrupt be volatile?
Show answer
Without volatile the compiler may cache the flag in a register and never see the change made by the interrupt handler, causing an infinite loop.
How would you check for a leap year, and which test years prove your code?
Show answer
Divisible by 4 and not by 100, or divisible by 400. Test 1900, 2000, 2023, 2024, 2100 and 2400.
When would you choose switch over an else if ladder?
Show answer
Use switch for comparing one integer against discrete constants such as command codes or states. Use a ladder for ranges or compound conditions.
What is a compound statement and where does variable scope start and end?
Show answer
Statements grouped in braces. A variable declared inside the block exists only until the closing brace.
What happens if you put a semicolon after while (condition)?
Show answer
The body becomes an empty statement. The loop spins on the test and the block after it is a normal block. It is a classic cause of hangs.
How do you avoid deep nesting in embedded code?
Show answer
Use early return, split logic into small named functions, and replace long chains with lookup tables or state machines.
Score Yourself
| Score (out of 29) | What it means |
|---|---|
| 26 to 29 | Excellent. You are ready for the do while loop and the for loop. |
| 20 to 25 | Good. Re-read the lecture on the topics you missed. |
| 12 to 19 | Revise lectures 4, 5 and 6 and retry the output prediction programs. |
| Below 12 | Go back to Chapter 4 on operators, then repeat Chapter 5. |
Key Takeaways
- Revise the dangling else rule, ladder order and loop traps until you can explain them aloud.
- Always predict output on paper before running the code.
- Compile with -Wall -Wextra to learn what the compiler can catch.
- Interview answers are strongest when backed by a tiny example.
Conclusion
Practice turns reading into skill. Work through every part of this lab, then write your own versions of the challenges. This lab is part of our free C programming course, a free C tutorial for C programming for embedded systems, and it doubles as interview preparation for anyone learning embedded C programming.
Frequently Asked Questions
Are these C programming interview questions suitable for freshers?
Yes. They cover the control statement topics that freshers are asked most often in embedded C interviews.
Do I need to compile the output prediction programs?
Try to predict first, then compile them yourself with gcc to confirm. Every answer here came from a real run.
Where can I find the next quiz in this free embedded C course?
The next lab follows the do while loop and the for loop lectures in this free C tutorial.
How many times should I retry the quiz?
Retry until you score above 26 without checking the answers, then move on.
Is this free C programming course enough for embedded interviews?
It builds the foundation. Pair it with the later chapters on pointers, memory and bit manipulation for full interview readiness.
Take Your Free Embedded C Course Further
Finished the lab? Continue with the next lecture of this free embedded C course and free C tutorial.

2 Comments