🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Free Embedded C Course Quiz
Test yourself with predict-the-output questions, multiple choice, programming challenges and interview answers from our free embedded C course.
This is the checkpoint lecture of the Functions chapter in our free embedded C course. You have built a Pascal’s triangle generator, a Roman numeral converter, two digit-peeling recursions and a base converter. Now it is time to find out what stuck. This free embedded C course lecture gives you a recap table, a predict-the-output exercise with real compiler results, twelve multiple-choice questions, five programming challenges and sixteen interview questions with model answers, all written for people preparing for firmware and embedded software roles.
Treat the page as a workout, not a reading assignment. Cover the answers, commit to a response, and only then open the explanation. The effort of recalling is what turns a lecture into a skill.
What You Will Learn in This Free Embedded C Course Quiz
- How to spot the core technique and the main trap of each of the five previous programs.
- How to trace recursive code by hand and predict its output before running it.
- How integer width, overflow and divisor cost shape design decisions on microcontrollers.
- How to explain recursion, tail calls, stack depth and table-driven design the way interviewers expect.
- Which follow-up programs to write next to turn the lessons into portfolio-quality code.
Prerequisites
Work through the earlier lectures in this group first: Pascal’s triangle, the Roman numeral converter, recursive digit processing and recursive base conversion. The questions assume you have seen the code and the real outputs there. Having a C compiler available helps, because several challenges ask you to run your own solution.
Free Embedded C Course Recap: Functions Chapter Programs
| Program | Core technique | Main trap | How we verified it |
|---|---|---|---|
| Pascal’s triangle | Layered functions, stepwise nCr, in-place row addition | Factorial overflow from n of 21 on 64 bits | Two independent methods compared for 34 rows |
| Roman numerals | Descending greedy loop over a static const table | Buffer overrun and missing range checks | Round trip of all 3999 values |
| Reverse digits | Remainder and division by ten, accumulator recursion | Leading zeros and accumulator overflow | Recursive and loop versions compared |
| Divisible by 11 | Alternating digit sum, reduce until small | Sign handling of negative sums | Compared with the remainder operator for 200001 values |
| Base conversion | Recurse on quotient, print remainder on unwinding | Bases 0 and 1, and bases above 36 | Compared with printf for 100001 values |
Part One: Predict the Output
Read the program below and write down what each numbered line prints before you open the answers. Trace the recursive calls on paper, writing each frame as you descend and each print as you return.
#include <stdio.h>
static int f(int n)
{
if (n <= 1)
return 1;
return n * f(n - 1);
}
static void p(int n)
{
if (n == 0)
return;
printf("%d", n % 10);
p(n / 10);
}
static void b(int n)
{
if (n > 0) {
b(n / 2);
printf("%d", n % 2);
}
}
static int s(int n)
{
return n == 0 ? 0 : n % 10 - s(n / 10);
}
static int g(int n)
{
if (n == 0)
return 0;
return g(n / 10) + 1;
}
static void h(int n)
{
if (n > 0) {
printf("%d ", n);
h(n - 1);
printf("%d ", n);
}
}
int main(void)
{
printf("Q1: %d\n", f(5));
printf("Q2: ");
p(4050);
printf("\n");
printf("Q3a: [");
b(6);
printf("]\nQ3b: [");
b(0);
printf("]\n");
printf("Q4: %d\n", s(1234));
printf("Q5: %d %d\n", g(90210), g(0));
printf("Q6: ");
h(3);
printf("\n");
printf("Q7: %d\n", 17 / 5 * 5 + 17 % 5);
return 0;
}
Show the real output
Q1: 120
Q2: 0504
Q3a: [110]
Q3b: []
Q4: 2
Q5: 5 0
Q6: 3 2 1 1 2 3
Q7: 17
Show the explanations
- Q1 prints 120. The function multiplies 5 by the result for 4, and so on down to the base case, giving 5 times 4 times 3 times 2 times 1.
- Q2 prints 0504. Each call prints the last digit before recursing, so 4050 emits 0, 5, 0, 4. The zeros are printed, which is why printing and numeric reversal differ.
- Q3 prints 110 for 6 and nothing for 0. The recursion happens before the print, so the most significant bit appears first. Because the condition is n greater than 0, an input of 0 prints an empty string.
- Q4 prints 2. It expands to 4 minus (3 minus (2 minus (1 minus 0))), which equals 4 – 3 + 2 – 1.
- Q5 prints 5 and 0. The function counts digits by recursion. For zero it returns 0, which shows that a digit counter written this way treats 0 as having no digits.
- Q6 prints 3 2 1 1 2 3. Numbers are printed on the way down, then again on the way up in reverse.
- Q7 prints 17. Integer division and remainder always satisfy a equals (a divided by b) times b plus (a remainder b), so 3 times 5 plus 2.
Part Two: Multiple Choice Questions
Choose one answer for each question, then open the answer below it.
- What is the largest n for which n factorial fits in an unsigned 32-bit integer?
- A) 10
- B) 12
- C) 13
- D) 20
Answer
B. 12 factorial is 479,001,600, while 13 factorial is 6,227,020,800, which exceeds 4,294,967,295.
- Why must the in-place row update for Pascal’s triangle run from the right end toward the left?
- A) It is faster
- B) Left to right would overwrite values still needed
- C) The array is stored backwards
- D) It avoids overflow
Answer
B. Each element is the sum of itself and its left neighbour, so the left neighbour must still hold its old value when it is used.
- In the stepwise nCr loop, why is each division exact?
- A) The compiler rounds correctly
- B) The running value is itself a binomial coefficient at every step
- C) Division of unsigned values never truncates
- D) It is not exact, errors cancel at the end
Answer
B. After step i the value equals C(n – r + i, i), always an integer, so no remainder is lost.
- Our Roman numeral converter returns -1 for which inputs?
- A) Even numbers
- B) Values below 1 or above 3999
- C) Values with subtractive pairs
- D) Only zero
Answer
B. The valid range is 1 through 3999; both zero and 4000 were rejected in the real run.
- Which arrangement prints the digits of a number left to right using recursion on n / 10?
- A) Print first, then recurse
- B) Recurse first, then print n % 10
- C) Print twice
- D) It cannot be done recursively
Answer
B. Output then happens during unwinding, starting from the most significant digit.
- What is the numeric reversal of 1200?
- A) 0021
- B) 21
- C) 2100
- D) 12
Answer
B. A numeric value cannot hold leading zeros, so 0021 becomes 21.
- The divisibility rule for 11 works because:
- A) 11 is prime
- B) Ten is congruent to minus one modulo 11
- C) 11 is odd
- D) Digit sums are always smaller
Answer
B. Powers of ten alternate between 1 and -1 modulo 11, so the number behaves like its alternating digit sum.
- What happens if the recursive base converter is called with base 1?
- A) It prints the number in unary
- B) It returns 0
- C) It recurses without end because n / 1 never shrinks
- D) Compilation fails
Answer
C. The quotient equals the input, the stop condition is never met, and the stack is exhausted.
- What is the maximum recursion depth when printing a 32-bit unsigned value in binary?
- A) 8
- B) 16
- C) 32
- D) 64
Answer
C. One frame per binary digit, and a 32-bit value has at most 32 of them.
- How many bytes are needed to hold the base 2 text of any 64-bit value, including the terminator?
- A) 32
- B) 64
- C) 65
- D) 66
Answer
C. Sixty-four digits plus one byte for the terminating null.
- About tail recursion in C:
- A) The standard guarantees the frame is reused
- B) The compiler may optimise it, but the standard does not promise it
- C) It is illegal
- D) It always uses less stack than a loop
Answer
B. Optimisers such as GCC often convert tail calls to jumps at higher optimisation levels, but you must not depend on it for correctness.
- On a Cortex-M0 class core with no hardware divider, what does the expression n / 10 typically compile to?
- A) A single divide instruction
- B) A call to a library division routine, or a multiplication trick for constant divisors
- C) A shift by ten
- D) A compile error
Answer
B. Without a divide instruction the compiler either calls a software routine or, for a constant divisor, may use a multiply-and-shift sequence. Either way it costs more than an addition.
Part Three: Programming Challenges
Write each solution yourself, compile with -Wall -Wextra -pedantic, and test it against a reference. Hints are provided but no solutions, because the learning happens in the struggle.
| Challenge | Task | Hint |
|---|---|---|
| 1 | Compute C(n, r) in 32-bit unsigned arithmetic and return an error when the result would overflow. | Use the stepwise method and check each multiplication with __builtin_mul_overflow in GCC or Clang, or test against UINT32_MAX / factor first. |
| 2 | Write a Roman numeral validator that rejects IIII, VX and IC. | Convert the parsed value back with to_roman and compare strings. |
| 3 | Reverse an unsigned 32-bit number and report overflow instead of wrapping. | Before each multiply by ten, compare the accumulator with the limit divided by ten. |
| 4 | Print a value as fixed-width hexadecimal, for example eight digits with leading zeros. | Count digits first, or pass a minimum width and print padding zeros on the way down. |
| 5 | Test divisibility by 7, 11 and 13 together for a digit string of any length. | Because 1001 equals 7 times 11 times 13, alternate the sum of three-digit groups. |
Part Four: Embedded C Interview Questions
These questions appear, in various wordings, in firmware and embedded software interviews. Say the answer out loud before reading the model response.
- What is the base case of a recursive function, and what happens without one?
Answer
The base case is the condition that stops further calls. Without it, or if calls do not move toward it, recursion continues until the stack is exhausted, which on a microcontroller can corrupt neighbouring memory.
- Explain the difference between printing a number’s digits in reverse and computing its reversed value.
Answer
Printing emits every digit including zeros at the end of the input, so 1200 gives 0021. A numeric reversal cannot store leading zeros, so it gives 21.
- What does a stack frame contain, and why does it matter for recursion?
Answer
A return address, saved registers, arguments and local variables, depending on the ABI. Each recursive call adds a frame, so depth times frame size must fit in the stack.
- Why does recursing before printing reverse the order of produced remainders?
Answer
Calls resume in last-in first-out order. The deepest call, holding the most significant digit, finishes and prints first.
- How do you detect overflow in C arithmetic?
Answer
For unsigned types, test before the operation, for example check that a is not greater than the maximum divided by b. For signed types overflow is undefined behaviour, so you must test before it happens. GCC and Clang offer builtins such as
__builtin_mul_overflow. - Why is signed integer overflow dangerous?
Answer
It is undefined behaviour, so the compiler may assume it never occurs and remove checks or reorder code in surprising ways. Unsigned arithmetic wraps predictably.
- What is tail recursion, and does C guarantee tail call optimisation?
Answer
It is a recursive call that is the last action of the function. C does not guarantee optimisation, though compilers often perform it with optimisation enabled, so do not rely on it for stack safety.
- Why do some coding standards forbid recursion?
Answer
Stack usage becomes hard to bound statically, which matters in safety-critical systems. MISRA C:2012, for example, contains a rule against functions that call themselves.
- Where does a
static constlookup table normally live on a microcontroller?Answer
Typically in read-only memory such as flash, not RAM, though exact placement depends on the toolchain and linker script.
- Why pass a buffer and its size instead of returning a pointer to a local array?
Answer
A local array ceases to exist when the function returns, so the pointer would dangle. A caller-owned buffer with a size also prevents overruns.
- Why is the stepwise nCr algorithm exact?
Answer
After each step the running value is itself a binomial coefficient, hence an integer, so the integer division loses nothing.
- How would you find the largest factorial that fits in a type on your target?
Answer
Use the type’s maximum from
limits.horstdint.hand divide it repeatedly, or multiply upward while checking that the product divided by the factor equals the previous value. - Outline converting an integer to hexadecimal text without printf.
Answer
Use a digit lookup string and either recursion that prints the quotient first or a loop that fills a buffer from the end and returns the start pointer. For hexadecimal, shifts by four and masks of 0xF replace division.
- Why is division expensive on small cores, and how do you avoid it?
Answer
Cores such as Cortex-M0 have no divide instruction, so division is a software routine. Use shifts and masks for powers of two, addition-based algorithms, or multiply-by-reciprocal tricks the compiler may generate for constants.
- How can you test a function thoroughly when the input space is small?
Answer
Test every input and compare with a trusted reference or an inverse function, as done for all 3999 Roman numerals and for 200001 values in the divisibility check.
- What does
staticmean on a file-scope function?Answer
It gives internal linkage: the function is visible only inside its translation unit, which prevents name clashes and lets the compiler inline or discard it freely.
Common Mistakes and Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Predicted output differs from the real one | Print placed before or after the recursive call misread | Write frames on paper and mark when each print happens |
| Challenge result differs from the reference | Integer type too narrow | Check the width of every variable with sizeof and fixed-width types |
| Test passes on the PC and fails on the board | int or long has a different width on the target | Use stdint.h types |
| Program hangs on certain input | Missing base case guard or unchecked scanf | Validate inputs and test the return value of scanf |
Best Practices, Performance and Security
- Practise by tracing on paper first, running the code second.
- Keep a personal list of traps: overflow, sentinel values, off-by-one buffers, unchecked input.
- Performance: know the cost of division on your target and prefer shifts or addition where the algorithm allows.
- Security: bounds-check every buffer and validate every external number, because most memory-safety bugs start with unchecked sizes.
- Always verify new code against an independent method, a reverse function or the standard library.
Key Takeaways and Conclusion
Key takeaways
- The order of statements around a recursive call controls the order of output.
- Integer width and overflow behaviour are design inputs, not afterthoughts.
- Table-driven code, caller-owned buffers and status codes make C functions safe and testable.
- Exhaustive testing against a reference is practical whenever the input space is small.
- Interviewers care less about the final code than about the reasoning on stack depth, overflow and cost.
If you answered most of these without peeking, you have absorbed the functions material at the depth embedded work demands. If some answers surprised you, return to the matching lecture, rerun the code and change one thing at a time. Keep going: the free embedded C course continues with the next chapter, and the same discipline of tracing, bounding and verifying will carry over to every topic that follows.
Frequently Asked Questions: Quiz and Interview in C
How should I use this quiz?
Cover the answers, write down your own response, and only then open the explanation. Tracing recursive code on paper before running it builds the habit interviewers look for.
Do I need a development board to attempt it?
No. Every exercise runs on a normal PC with GCC or Clang. Using fixed-width types lets you reason about what would happen on a smaller target.
How do the interview questions relate to embedded jobs?
They cover recursion and stack depth, overflow, division cost on small cores, static tables and buffer safety, which are recurring themes in firmware interviews.
Are solutions provided for the programming challenges?
Hints are given but not full solutions, because writing and debugging the code yourself is where the learning happens. Verify each answer against a reference method.
Is prior knowledge required for this free embedded C course quiz?
You should have read the earlier lectures on Pascal’s triangle, Roman numerals, digit recursion and base conversion, since the questions refer to that code and its output.
What should I study next?
Continue to the next chapter of the course, and revisit any recap table row where you could not state the main trap without looking.
Keep Practising
Continue the free embedded C course with the next chapter and keep verifying every program you write.

2 Comments