C Functions Quiz and Interview- 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 →

Free C Programming Course • Functions • Lecture 43

C Functions Quiz and Interview

Test recursion, the call stack, base conversion, nCr and Pascal’s triangle with multiple-choice questions, output prediction, challenges and real embedded C interview questions.

This C functions quiz closes the recursion and practice-problem section of our free C programming course. Work through the questions before you open any answer; that effort is what builds skill. Every question reflects what interviewers ask about embedded C programming, so this free C tutorial page doubles as interview preparation. Keep a compiler open: for every output question, type the program in and check your prediction.

What You Will Learn

  • How to check your understanding of recursion, winding and unwinding.
  • How to predict program output without running it.
  • How to explain stack usage, recursion limits and overflow in an interview.
  • How to approach short programming challenges on base conversion and combinatorics.
  • How to answer common embedded C interview questions about recursion with confidence.

Prerequisites

Finish lectures 39 to 42 of the Functions chapter first: Tower of Hanoi, the recursion call stack, recursion versus iteration, and the base conversion, nPr, nCr and Pascal’s triangle problems. Use a C99 compiler such as GCC for the checking exercises.

How to Use This C Functions Quiz

SectionQuestionsSuggested time
Multiple-choice questions1010 minutes
Output prediction615 minutes
Programming challenges330 minutes
Interview questions12Read aloud and answer in your own words

Click a question’s summary to reveal the answer. Write your own answer first, then compare.

Part 1: C Functions Quiz Multiple-Choice Questions

Q1. How many moves does the Tower of Hanoi need for 6 discs?

  1. 36
  2. 63
  3. 64
  4. 127
Show answer

B (63). The minimum is 2n − 1, so 26 − 1 = 63.

Q2. A recursive function has no base case. What is the most likely result?

  1. It returns 0
  2. It runs once
  3. Stack overflow
  4. Compile error
Show answer

C. Every call adds a frame and nothing stops the descent, so the stack is eventually exhausted. The compiler cannot detect this in general.

Q3. Where is an automatic local variable of a recursive function stored?

  1. In a single shared data area
  2. In each call’s own stack frame
  3. In the heap
  4. In program memory
Show answer

B. Each active call has its own copy of automatic locals inside its stack frame.

Q4. For hanoi_walk with 7 discs, what is the maximum call depth?

  1. 7
  2. 8
  3. 127
  4. 255
Show answer

B (8). Maximum depth is n + 1 because the zero-disc leaf call is one more frame than the disc count.

Q5. What does a static local variable do in a recursive function?

  1. Gets a new copy per call
  2. Is shared by all calls
  3. Is destroyed on return
  4. Is stored on the stack
Show answer

B. There is exactly one copy, with program lifetime, shared by every call.

Q6. Why is naive recursive Fibonacci slow?

  1. It uses floating point
  2. It recomputes the same sub-results many times
  3. It uses too many globals
  4. printf is slow
Show answer

B. Overlapping sub-problems are recomputed. For n = 30 it makes 2,692,537 calls against 29 loop steps.

Q7. Which result does the digit check digit >= base catch?

  1. The digit 1 in binary
  2. The digit 8 in octal
  3. The digit 7 in octal
  4. The digit 0 in binary
Show answer

B. Octal digits are 0 to 7, so 8 is not smaller than the base 8 and is rejected.

Q8. What is C(6, 2)?

  1. 12
  2. 15
  3. 30
  4. 36
Show answer

B (15). P(6, 2) = 30 and C = 30 / 2! = 15.

Q9. In the single-array Pascal program, why must the inner loop run right to left?

  1. To print in reverse
  2. To use the old left neighbour before it is overwritten
  3. To save memory
  4. It does not matter
Show answer

B. Each update reads the entry to its left. Going left to right would overwrite that neighbour first and corrupt the row.

Q10. What does 21! look like in a 64-bit unsigned variable?

  1. The correct value
  2. A compile error
  3. A wrapped, incorrect value with no warning
  4. Zero
Show answer

C. Unsigned overflow wraps modulo 264. The program printed 14197454024290336768, which is wrong but looks plausible.

Part 2: C Functions Quiz Output Prediction

Assume a C99 compiler. Write the output, then check by running.

Q11. What does down(3) print?

static void down(int n)
{
    if (n == 0) return;
    printf("%d ", n);
    down(n - 1);
}
Show answer

3 2 1. The print comes before the recursive call, so it runs while winding.

Q12. What does up(3) print?

static void up(int n)
{
    if (n == 0) return;
    up(n - 1);
    printf("%d ", n);
}
Show answer

1 2 3. The print comes after the recursive call, so it runs while unwinding, in reverse order of the calls.

Q13. What does both(3) print?

static void both(int n)
{
    if (n == 0) return;
    printf("%d ", n);
    both(n - 1);
    printf("%d ", n);
}
Show answer

3 2 1 1 2 3. First half while descending, second half while returning.

Q14. What does this print?

static int counter(void)
{
    static int c;
    c++;
    return c;
}

int a = counter();
int b = counter();
int c = counter();
printf("%d %d %d\n", a, b, c);
Show answer

1 2 3. The static variable keeps its value between calls and starts at zero. Warning: if you instead wrote three counter() calls inside one printf, the order of evaluation of the arguments is unspecified and the output could differ between compilers.

Q15. What does total(4) return?

static int total(int n)
{
    return n == 0 ? 0 : n + total(n - 1);
}
Show answer

10. It computes 4 + 3 + 2 + 1 + 0.

Q16. What does mystery(7) return?

static int mystery(int n)
{
    if (n <= 1) return 1;
    return n * mystery(n - 2);
}
Show answer

105. It multiplies 7 × 5 × 3 × 1, a product of every second number.

Part 3: Programming Challenges

Challenge 1. Extend to_decimal so it supports bases up to 16 using letters A to F, and rejects lowercase or unknown characters.

Show hint

Map each character to a value: digits give c - '0', letters A to F give c - 'A' + 10. Reject any value that is not smaller than the base. Keep Horner’s loop unchanged.

Challenge 2. Write a function that prints the Tower of Hanoi moves without recursion for any disc count up to 10. Verify the move count against 2n − 1.

Show hint

Use the observation that the smallest disc moves on every odd step in one fixed direction and the even steps make the only legal move that does not involve the smallest disc. Keep three small arrays as pegs.

Challenge 3. Add overflow protection to n_c_r so it returns an error indicator when the true answer does not fit in 64 bits.

Show hint

Before multiplying, test whether result exceeds the maximum value divided by (n - r + i). If it does, report failure instead of multiplying.

Part 4: Embedded C Interview Questions

1. What is recursion and what are its two required parts?

Recursion is a function calling itself on a smaller version of the problem. It needs a base case that stops the calls and a recursive case that moves toward the base case.

2. Why do many embedded coding standards discourage recursion?

Stack depth must be provable on devices with tiny RAM, and recursion makes the worst case depend on data. Loops make stack use constant and easy to bound. Some safety standards forbid recursion outright.

3. How would you estimate the stack cost of a recursive function?

Multiply the maximum depth by the size of one frame (arguments, locals, return address and saved registers), then add headroom for interrupt handlers. Verify with a depth counter and, on the target, by painting the stack and measuring the high-water mark.

4. What is the difference between winding and unwinding?

Winding is the series of calls going down to the base case; code before the recursive call runs here. Unwinding is the return path; code after the recursive call runs here, in reverse order.

5. Do recursive calls share local variables?

No. Automatic locals and parameters are per call. Static and global variables are shared.

6. What is tail recursion and can you rely on it?

A tail call is the last action of a function. A compiler may turn it into a jump, but C does not guarantee this, so safety cannot depend on it.

7. How many moves does the Tower of Hanoi need and why?

2n − 1. The recurrence T(n) = 2T(n−1) + 1 comes from moving n−1 discs twice plus one move of the largest disc.

8. Why is recursive Fibonacci a poor implementation?

Overlapping sub-problems are recomputed, giving exponential calls. An iterative loop or stored results needs a linear number of steps.

9. How do you avoid integer overflow when computing nCr?

Do not compute full factorials. Multiply and divide step by step using the multiplicative formula, and use the symmetry C(n, r) = C(n, n−r) to keep r small. Check the result type’s range.

10. How do you convert a numeric string in any base to an integer?

Process characters left to right: value = value × base + digit. Validate each digit against the base and guard against overflow for long inputs.

11. What happens on unsigned integer overflow in C?

It wraps modulo 2n and is well defined, but silent. Signed overflow is undefined behaviour, so prefer unsigned types for counters and check ranges.

12. When would you still choose recursion in a real project?

For host-side tools, tests and prototypes, and in firmware only when the depth is small, bounded by a constant, and documented, such as walking a fixed-depth tree.

Common Mistakes Seen in Interviews

MistakeBetter answer
Saying recursion is always slowerSay it adds call overhead, and that the difference matters most when calls number in the millions
Forgetting the base case when writing code on a whiteboardWrite the base case first and say it out loud
Ignoring stack limitsMention depth, frame size and interrupt headroom
Assuming static variables are per callState clearly there is one shared copy
Not testing boundary valuesTest 0, 1, the maximum and invalid inputs

Best Practices for Interview Preparation

  • Treat the C functions quiz as a starting point: run every program yourself. Predicting output and then checking builds real understanding.
  • Practise explaining the call stack aloud, drawing a few frames on paper.
  • Be ready to convert any recursive answer into a loop and discuss which one you would ship.
  • Mention safety and security: bounded depth, validated input, no unsigned overflow surprises.

Summary and Key Takeaways

  • Recursion needs a base case and steady progress toward it.
  • Code before the recursive call runs while winding; code after it runs while unwinding.
  • Locals are per call; static variables are shared.
  • Calls and stack depth are separate costs: Hanoi makes 2n+1 − 1 calls but only n + 1 frames.
  • Safe numeric code validates input and avoids overflow with multiplicative methods.

Conclusion

If you answered most of this C functions quiz without help, you have a solid grasp of functions and recursion for C programming for embedded systems. Revisit any lecture where an answer surprised you, then move to the next chapter of this free embedded C course. Consistent practice, not memorising, is what carries these ideas into real firmware.

Frequently Asked Questions

Is this C functions quiz suitable for beginners?

Yes. It starts with simple multiple-choice questions and builds toward output prediction and challenges. Beginners should read the earlier lectures first.

Will these questions help for an embedded C interview?

They cover topics interviewers commonly ask: recursion, stack usage, static variables, overflow and numeric parsing. Practise answering aloud as well as reading.

Where can I find the next chapter of the free C programming course?

Use the next-lecture link at the top or bottom of this page, or the course index page on EmbeddedPathashala.

Do I need special hardware for this free embedded C course?

No. Every program here runs on a normal computer with GCC or Clang. The same code carries over to microcontroller toolchains.

How should I check my answers to output prediction questions?

Compile with -Wall -Wextra, run the program and compare. If your answer differed, trace the call stack by hand and find where the prediction diverged.

Keep Learning for Free

Continue with the next chapter of the free C programming course and build the skills behind embedded C programming.

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