You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/ch7_recursion.ptx
+15-15Lines changed: 15 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -356,12 +356,16 @@ public class ArrayProcessor {
356
356
<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>
357
357
</li>
358
358
</ol></p>
359
-
<reading-questionsxml:id="rqs-recursion-java">
359
+
<reading-questionsxml:id="rqs-recursion-java">
360
360
<exerciselabel="recursion-method-signature">
361
361
<statement>
362
362
<p>Which method signature and behavior best match a typical Java recursive factorial implementation?</p>
363
363
</statement>
364
364
<choices>
365
+
<choice>
366
+
<statement><p><c>public static int factorial(int n)</c> that returns <c>0</c> when <c>n <= 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>
365
369
<choice>
366
370
<statement><p><c>public void factorial(int n)</c> that prints each partial product and stops when <c>n</c> reaches zero.</p></statement>
367
371
<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 {
371
375
<feedback><p>Correct. This matches the standard recursive factorial definition in Java.</p></feedback>
372
376
</choice>
373
377
<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>
380
380
</choice>
381
381
</choices>
382
382
</exercise>
@@ -408,20 +408,20 @@ public class ArrayProcessor {
408
408
<p>Which statement about recursion limits and errors is accurate?</p>
409
409
</statement>
410
410
<choices>
411
-
<choice>
411
+
<choicecorrect="yes">
412
412
<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>
414
414
</statement>
415
415
<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>
417
417
</feedback>
418
418
</choice>
419
-
<choicecorrect="yes">
419
+
<choice>
420
420
<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>
422
422
</statement>
423
423
<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>
425
425
</feedback>
426
426
</choice>
427
427
<choice>
@@ -434,10 +434,10 @@ public class ArrayProcessor {
434
434
</choice>
435
435
<choice>
436
436
<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>
438
438
</statement>
439
439
<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>
0 commit comments