Consider the code for the recursive method printSum shown in this code snippet, which is intended to return the sum of digits from 1 to n:
Public static int printSum(int n)
{
If (n <= 0) // line #1
{
Return 0; // line #
}
Else
{
Return (n + printSum(n) ) ; //line #3
}
}
Which of the following statements is correct?
A) line #1 is incorrect, and should be changed to if (n <= 1)
B) line #3 is incorrect, and should be changed to return (printSum (n - 1) ) ;
C) line #3 is incorrect, and should be changed to return (n + printSum (n + 1) ) ;
D) line #3 is incorrect, and should be changed to return (n + printSum (n - 1) ) ;
Correct Answer:
Verified
Q53: Given the following class code:
Public class RecurseSample
{
Public
Q54: Complete the code for the myFactorial recursive
Q56: Complete the code for the recursive method
Q57: Given the following class code:
Public class RecurseMore
{
Public
Q59: Complete the code for the calcPower recursive
Q60: Complete the following code snippet, which is
Q60: Given the following code snippet:
Public static int
Q61: Consider the iterative version of the fib
Q62: Consider the recursive version of the fib
Q63: Consider the recursive square method shown below
Unlock this Answer For Free Now!
View this answer and more for free by performing one of the following actions
Scan the QR code to install the App and get 2 free unlocks
Unlock quizzes for free by uploading documents