Skip to content

Commit ee5d231

Browse files
committed
improve summary and reading question
1 parent 5ddc2d5 commit ee5d231

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

source/ch7_recursion.ptx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,16 @@ public class ArrayProcessor {
356356
<p>Recursive methods in Java must specify return types explicitly, unlike Python's dynamic typing, which affects how you handle error cases and return values.</p>
357357
</li>
358358
</ol></p>
359-
<reading-questions xml:id="rqs-recursion-java">
359+
<reading-questions xml:id="rqs-recursion-java">
360360
<exercise label="recursion-method-signature">
361361
<statement>
362362
<p>Which method signature and behavior best match a typical Java recursive factorial implementation?</p>
363363
</statement>
364364
<choices>
365+
<choice>
366+
<statement><p><c>public static int factorial(int n)</c> that returns <c>0</c> when <c>n &lt;= 0</c> and otherwise returns <c>n * factorial(n - 1)</c>.</p></statement>
367+
<feedback><p>No. While this handles negative numbers, the base case is incorrect - factorial of 0 should be 1, not 0.</p></feedback>
368+
</choice>
365369
<choice>
366370
<statement><p><c>public void factorial(int n)</c> that prints each partial product and stops when <c>n</c> reaches zero.</p></statement>
367371
<feedback><p>No. Printing results is fine for testing, but a proper factorial method should return the computed value.</p></feedback>
@@ -371,12 +375,8 @@ public class ArrayProcessor {
371375
<feedback><p>Correct. This matches the standard recursive factorial definition in Java.</p></feedback>
372376
</choice>
373377
<choice>
374-
<statement><p><c>private static int factorial(double n)</c> that repeatedly multiplies <c>n</c> and decrements it until it reaches 1.</p></statement>
375-
<feedback><p>No. Factorials are for integers, and using <c>double</c> here is unnecessary and can cause rounding issues.</p></feedback>
376-
</choice>
377-
<choice>
378-
<statement><p><c>public int factorial()</c> that uses a stored class field for <c>n</c> instead of a method parameter.</p></statement>
379-
<feedback><p>No. Relying on a class field hides the input and makes recursion less flexible.</p></feedback>
378+
<statement><p><c>public static long factorial(int n)</c> that returns <c>1</c> when <c>n == 0</c> and otherwise returns <c>n * factorial(n - 1)</c>.</p></statement>
379+
<feedback><p>No. While this logic is close, it doesn't handle the case when n = 1, and using long as return type when int parameter is used creates inconsistency.</p></feedback>
380380
</choice>
381381
</choices>
382382
</exercise>
@@ -408,20 +408,20 @@ public class ArrayProcessor {
408408
<p>Which statement about recursion limits and errors is accurate?</p>
409409
</statement>
410410
<choices>
411-
<choice>
411+
<choice correct="yes">
412412
<statement>
413-
<p>Java can handle very deep or even infinite recursion if the method body is short and does not perform significant operations.</p>
413+
<p>When the call stack is exhausted, Python raises a <c>RecursionError</c> whereas Java throws a <c>StackOverflowError</c>, and neither language applies automatic tail call optimization.</p>
414414
</statement>
415415
<feedback>
416-
<p>No. Regardless of the method’s complexity, each recursive call consumes stack space, and infinite recursion will always cause a stack overflow.</p>
416+
<p>Correct. This difference in exception types and the lack of built-in tail call optimization is a key distinction between the two languages.</p>
417417
</feedback>
418418
</choice>
419-
<choice correct="yes">
419+
<choice>
420420
<statement>
421-
<p>When the call stack is exhausted, Python raises a <c>RecursionError</c> whereas Java throws a <c>StackOverflowError</c>, and neither language applies automatic tail call optimization.</p>
421+
<p>Java automatically applies tail call optimization to recursive methods marked as <c>final</c>, preventing most stack overflows.</p>
422422
</statement>
423423
<feedback>
424-
<p>Correct. This difference in exception types and the lack of built-in tail call optimization is a key distinction between the two languages.</p>
424+
<p>No. Java does not perform automatic tail call optimization, regardless of whether methods are marked as final.</p>
425425
</feedback>
426426
</choice>
427427
<choice>
@@ -434,10 +434,10 @@ public class ArrayProcessor {
434434
</choice>
435435
<choice>
436436
<statement>
437-
<p>Increasing a method’s parameter type from <c>int</c> to <c>long</c> in Java can prevent stack overflows for large input values by storing bigger numbers more efficiently.</p>
437+
<p>The JVM can detect simple recursive patterns and automatically convert them to iterative loops to prevent stack overflow.</p>
438438
</statement>
439439
<feedback>
440-
<p>No. The size of the number type does not influence the maximum recursion depth; stack space usage depends on the number of active calls, not numeric range.</p>
440+
<p>No. The JVM does not automatically convert recursive methods to iterative ones. This optimization must be done manually by the programmer.</p>
441441
</feedback>
442442
</choice>
443443
</choices>

0 commit comments

Comments
 (0)