What is C Control Statements Quiz- Embedded C Programming Course Online

🚀 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 · Quiz Lab

C Control Statements Quiz

Free C programming quiz and C interview questions on switch, leap year, weekday and triad numbers for embedded C students.

15
MCQs
3
Output puzzles
15
Interview Qs

This final lecture of the chapter is a hands-on lab for our free C programming course. You will find 15 multiple choice questions, 3 predict-the-output puzzles, 4 programming challenges and 15 C programming interview questions with short answers. Everything is based on the last four lectures: switch fall-through, the day of year program, leap year and weekday logic, and triad numbers.

Try each question first, then open the answer. This is the fastest way to prepare for embedded C programming interviews and college exams, and it is the practice that makes the free C tutorial stick.

What You Will Learn

  • Spot fall-through behaviour in a few seconds
  • Trace a program by hand to predict its output
  • Apply the leap year rule and weekday formula without notes
  • Explain bit flags, goto and digit extraction in an interview
  • Answer the most common C programming interview questions on control statements

Prerequisites

  • Lectures cp5_35 to cp5_38 of this free embedded C course
  • A C compiler for the programming challenges

Quick Revision Table

TopicKey fact
Fall-throughAfter a case runs, execution continues into the next case unless break, return or goto stops it
default positionCan appear anywhere, chosen only when nothing matches
Day of yearDay number plus complete earlier months, plus 1 in leap years after February
Leap yearDivisible by 4 and not 100, or divisible by 400
Weekday(y + j + p/4 – p/100 + p/400) % 7 with p = y – 1, index 0 is Saturday
Triad limitFirst number from 100 to 333
Bit flagSet with |= 1u << n, test with & (1u << n)

Multiple Choice Quiz on C Control Statements

Choose one option for each question. Answers are hidden below each question.

Q1. In switch (2) { case 3: a(); case 2: b(); case 1: c(); }, which functions run?

  • A) Only b()
  • B) b() and c()
  • C) a(), b() and c()
  • D) None
Show answer

Answer: B. Control jumps to case 2 and falls into case 1. Case 3 is above the entry point and never runs.

Q2. Where may the default label be written inside a switch?

  • A) Only at the end
  • B) Only at the start
  • C) Anywhere
  • D) Only after a break
Show answer

Answer: C. The compiler uses default only when no case matches, regardless of where it is written.

Q3. Change is 97 cents and the largest coin is 10 cents. What is printed for 10c, 5c and 1c coins?

  • A) 10, 0, 7
  • B) 9, 1, 2
  • C) 9, 0, 7
  • D) 8, 3, 2
Show answer

Answer: B. 97/10 = 9 remainder 7, then 7/5 = 1 remainder 2, then 2 one-cent coins.

Q4. Which year is a leap year?

  • A) 1900
  • B) 2026
  • C) 2100
  • D) 2000
Show answer

Answer: D. 2000 is divisible by 400. 1900 and 2100 are divisible by 100 but not 400, and 2026 is not divisible by 4.

Q5. What is the day of year for 1 March 2023?

  • A) 59
  • B) 60
  • C) 61
  • D) 62
Show answer

Answer: B. 31 + 28 + 1 = 60 in a normal year.

Q6. What is the day of year for 1 March 2024?

  • A) 60
  • B) 61
  • C) 62
  • D) 59
Show answer

Answer: B. 2024 is a leap year, so February has 29 days and the total is 31 + 29 + 1 = 61.

Q7. In our weekday program, index 6 means which day?

  • A) Sunday
  • B) Thursday
  • C) Friday
  • D) Saturday
Show answer

Answer: C. The names array runs Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday for indexes 0 to 6.

Q8. For year 2024, what is p/4 when p = y – 1 using integer division?

  • A) 505
  • B) 506
  • C) 500
  • D) 2023
Show answer

Answer: A. 2023 / 4 = 505 with the fraction dropped.

Q9. Why does the triad search stop at 333?

  • A) 333 is a prime
  • B) 3 times the first must stay three digits
  • C) The digits would repeat
  • D) The loop is slow after that
Show answer

Answer: B. 3 x 333 = 999, the largest three-digit number.

Q10. What integer value does 1u << 3 produce?

  • A) 3
  • B) 6
  • C) 8
  • D) 16
Show answer

Answer: C. Shifting 1 left by three places gives binary 1000, which is 8.

Q11. What is the most accepted use of goto in C?

  • A) Replacing every loop
  • B) Leaving nested loops or cleaning up after errors
  • C) Making code shorter
  • D) Jumping into a case label
Show answer

Answer: B. A single jump out of deep nesting or to an error cleanup label is the standard justified use.

Q12. What does case 4: case 6: case 9: case 11: return 30; do?

  • A) Compile error
  • B) Returns 30 only for 11
  • C) Returns 30 for all four values
  • D) Returns 30 for every value
Show answer

Answer: C. Empty cases fall through, so all four labels reach the same return.

Q13. What is 657 % 10?

  • A) 65
  • B) 7
  • C) 6
  • D) 57
Show answer

Answer: B. The remainder after dividing by 10 is the last digit.

Q14. What does scanf("%d/%d/%d", &d, &m, &y) return for the input 02-05-2002?

  • A) 3
  • B) 2
  • C) 1
  • D) 0
Show answer

Answer: C. It reads 2, then the dash does not match the slash, so only one value is stored.

Q15. MISRA C:2012 Rule 16.3 asks that every switch clause ends with what?

  • A) A comment
  • B) An unconditional break
  • C) A default
  • D) A goto
Show answer

Answer: B. Fall-through therefore needs a formal deviation in MISRA projects.

Predict the Output

Write down the output before opening the answer.

Puzzle 1

int x = 2, t = 0;
switch (x) {
case 1: t += 1;
case 2: t += 10;
case 3: t += 100; break;
case 4: t += 1000;
}
printf("%d\n", t);
Show answer

Output: 110. Entry at case 2 adds 10, falls into case 3 adding 100, then break stops.

Puzzle 2

switch (9) {
default: printf("D");
case 1:  printf("1"); break;
case 2:  printf("2");
}
Show answer

Output: D1. Nothing matches 9, so default runs, then falls into case 1 and breaks.

Puzzle 3

Using the day_of_year() function from lecture cp5_37, what does day_of_year(15, 3, 2024) return?

Show answer

It returns 75: 15 + 28 + 31 = 74, plus 1 because 2024 is leap and the month is after February.

Programming Challenges

  1. Calendar printer: read a year and print the weekday of the first day of every month. Check three results on a real calendar.
  2. Days between dates: use the day of year function to find the number of days between two dates in the same year.
  3. Change with limited stock: extend the coin program so each hopper has a maximum count and any shortfall is passed to smaller coins.
  4. Digit pairs: find numbers where the double has no digit in common with the original, using the bitmask technique.

Hint

Build each challenge from small functions and test with values you can verify by hand before trusting the output.

C Programming Interview Questions and Answers

1. What is fall-through in a switch statement?

After the matching case executes, control continues into the following case bodies until a break, return or goto stops it.

2. Does default have to be the last label?

No. It is used only when no case matches, but if it does not end with a jump it falls into whatever follows.

3. Why do coding standards like MISRA limit fall-through?

It is usually an accident caused by a forgotten break, so standards require every clause to end explicitly and any exception to be documented.

4. How do you mark intentional fall-through?

With a fall through comment, [[fallthrough]] in C23, __attribute__((fallthrough)) in GCC or Clang, or the fallthrough macro in the Linux kernel.

5. Can you switch on a float or a string?

No. The controlling expression must have an integer type and case labels must be integer constant expressions.

6. Can two case labels have the same value?

No. Duplicate case values cause a compile error.

7. What is the Gregorian leap year rule?

Divisible by 4 and not by 100, or divisible by 400.

8. Why does a normal year shift the weekday by one?

365 days is 52 full weeks plus 1 day, so the same date moves forward one weekday each year.

9. How do you extract digits from an integer?

Take n % 10 for the last digit and replace n with n / 10, repeating until n is zero.

10. How do you set, clear, test and toggle bit n?

Set with |= (1u << n), clear with &= ~(1u << n), test with & (1u << n), toggle with ^= (1u << n).

11. When is goto acceptable?

For leaving deeply nested loops or jumping to a single error cleanup section, as the Linux kernel does.

12. What is a jump table?

An array of code addresses the compiler may use to implement a dense switch, so selection takes one indexed jump.

13. Why declare lookup tables static const in embedded C?

The linker can place them in flash instead of RAM, and static keeps them private to the file.

14. Why must you check the return value of scanf?

If input does not match, variables stay uninitialised, which leads to undefined behaviour.

15. Is switch faster than if-else?

Often, because of jump tables, but it depends on the compiler and target, so check the generated assembly.

Common Mistakes Seen in Interviews

MistakeBetter answer
Saying fall-through is an errorIt is defined behaviour, just risky when unmarked
Forgetting century years in leap rulesMention the 100 and 400 layers
Using goto without a reasonExplain the nested loop or cleanup case
Ignoring input validationState that dates and sensor values are untrusted

Summary and Key Takeaways

  • A switch is a jump followed by straight-line execution.
  • Mark or avoid fall-through according to your coding standard.
  • The leap rule and weekday formula need integer arithmetic only.
  • Bit flags and digit extraction are everyday embedded skills.
  • Practise by tracing code on paper before running it.

Conclusion

You have now completed the problems from this part of the Control Statements chapter. If you scored well on the quiz, you understand how C control flow really executes. If a question tripped you, reread the matching lecture and trace its code again. Keep going through the free embedded C course for loops, functions and pointers.

Frequently Asked Questions

Where can I practise C programming interview questions for free?

This lecture and the rest of the free C course on EmbeddedPathashala include quizzes and interview questions after each topic.

Is this free C tutorial suitable for embedded systems students?

Yes. Every example is chosen to build skills used in embedded C programming, such as bit flags and lookup tables.

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

Trace small programs by hand, know the switch fall-through rules and practise explaining them aloud.

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

No. All programs here run on a normal computer with gcc.

What should I study after control statements?

Move on to loops in depth, functions, arrays and pointers, which are the next chapters of the course.

Keep Practising for Free

Move on to the next chapter of the free embedded C course and keep building your C programming skills.

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 →