AP CSA 4.16 Debugging: The Base Case That Returns Zero

Unit 4: Data Collections · Lesson 4.16 · Debugging

The Base Case That Returns Zero

Recursion. This is not a blank editor: it is someone else's attempt. Find what is wrong, fix it, and submit to be graded against hidden test cases.

Why debugging is its own skill

The recursion terminates, the structure is textbook, and every answer is zero. A base case is not just a stopping point: it is the value the entire chain of multiplications is built on top of, and returning 0 from it annihilates everything the recursion computed on the way down.

What is wrong with it

  1. The factorial base case returns 0. Every result is then multiplied by that 0 as the calls unwind, so the answer is always 0. Return the value that leaves a product unchanged.
  2. The sum-to-n method recurses on n instead of n - 1, so it never approaches its base case. Change the recursive call so the problem gets smaller each time.
  3. The countdown method is correct. Notice that it does reduce its argument, and use it as the model.

The program reads

A single integer n from 1 to 12.

The program should print

The countdown from n to 1 one per line, then n factorial, then the sum of 1 through n.

Worked examples

These show what the FIXED program should print. There are more cases you cannot see, and they use different values, so patching around just these numbers will fail.

Example 1 input
5
Example 1 output
5
4
3
2
1
120
15
Example 2 input
1
Example 2 output
1
1
1

Your answer

Main.java

Input for the Run button

Run sends whatever is in the code box below, bugs and all, so you can see the crash or the wrong answer for yourself before you fix anything.


  

  

Stuck?

Hint 1

Expand factorial(3) by hand: 3 * factorial(2), which is 3 * 2 * factorial(1), which is 3 * 2 * 0. Anything multiplied by zero is zero, so the base case silently destroys the whole product. The identity for multiplication is 1.

Hint 2

A recursive call must make the problem strictly smaller or it never reaches the base case. sumTo(n) calling sumTo(n) is the same problem again, forever.

Hint 3

Compare the three methods. countdown passes n - 1 and factorial passes n - 1. Only sumTo passes n unchanged, which makes it the odd one out.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]