Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1: Consider the code for the recursive function myPrint shown in this c

ID: 3828710 • Letter: Q

Question

Question 1:

Consider the code for the recursive function myPrint shown in this code snippet:

1. def myPrint(n) :

2.    if n == 0 :

3.       return 0

4.    else :

5.       return n + mysteryPrint(n - 1)

To avoid infinite recursion, which of the following lines of code should replace the current terminating case?

Question options:

if n == -1

if n <= 0

if n >= 0

The terminating case as shown will avoid infinite recursion

Question 2

Which problem is well suited to being solved with mutually recursive functions?

Question options:

Computing the factorial of a number

Determining if a string is a palindrome

Computing Fibonacci numbers

Evaluating a numeric expression

Question 3

Recall the backtracking strategy outlined in the textbook. A similar strategy can be used to solve Sudoku puzzles.

def solve(gameBoard) :

   status = examine(gameBoard)

   if status == CONTINUE :

      for nextBoard in extend(gameBoard) :

         solve(nextBoard)

   elif status == ACCEPT:

      ____________________

What code should be placed in the blank to complete the solution to this problem?

Question options:

print(status)

print(gameBoard)

solve(gameBoard)

gameBoard = extend(gameBoard)

Question 4

Consider the following recursive code snippet:

1. def mystery(n, m) :

2.    if n == 0 :

3.       return 0

4.    if n == 1 :

5.       return m

6.    return m + mystery(n - 1, m)

What value is returned from a call to mystery(1, 5)?

Question options:

1

5

6

11

Question 5

Consider the function powerOfTwo shown below:

1. def powerOfTwo(n) :

2.    if n == 1 :

3.       return True

4.    elif n % 2 == 1 :

5.       return False

6.    else :

7.       return powerOfTwo(n / 2)

How many recursive calls are made from the original call powerOfTwo(63) (not including the original call)?

Question options:

6

4

1

0

Question 6

Which statement(s) about recursion are true?

I. Recursion is faster than iteration

II. Recursion is often easier to understand than iteration

III. Recursive design has an economy of thought

Question options:

I

II

II and III

I and III

Question 7

Consider the following recursive function:

def myPrint(n) :

if n < 10 :

    print(n)

else :

    m = n % 10

    print(m)

    myPrint(n // 10)

What does this function do?

Question options:

It prints a positive value forward, digit by digit

It prints a positive value backward, digit by digit

It divides the number by 10 and prints out its last digit

It divides the number by 10 and prints out the result

Question 8

The following function is supposed to use recursion to compute the area of a square from the length of its sides. For example, squareArea(3) should return 9.

def squareArea(sideLength) :

   if sideLength == 1 :

      return 1

   else :

      ____________________

     

What line of code should be placed in the blank to achieve this goal?

Question options:

return squareArea(sideLength - 1)

return 2 * squareArea(sideLength - 1)

return 2 * sideLength + squareArea(sideLength - 1)

return 2 * (sideLength - 1) + squareArea(sideLength - 1)

Question 9

Consider the following code snippet for calculating Fibonacci numbers recursively:

1. def fib(n) :

2.    # assumes n >= 0

3.    if n <= 1 :

4.       return n

5.    else :

6.       return fib(n - 1) + fib(n - 2)

Identify the terminating condition in this recursive function.

Question options:

n < 1

n <= 1

fib(n - 1)

fib(n - 1) + fib(n - 1)

Question 10

Which statement about backtracking is correct?

Question options:

Backtracking starts from the end of the program and works backward to the beginning.

Backtracking builds up partial solutions that get increasingly closer to the goal.

Backtracking never abandons a partial solution.

Backtracking explores only one path toward a solution

if n == -1

if n <= 0

if n >= 0

The terminating case as shown will avoid infinite recursion

Explanation / Answer

Hi, I have answered first 5 Questions.

Please repost other in separate post.

Please let me know in case of any issue in first 5.

Q1)

   Ans: if n <= 0

   Currently in code we have : if n == 0
   if n value is negative then it goes in infinite loop

Q2)
   Ans: Computing Fibonacci numbers

   We call: F(n) = F(n-1) + F(n-2)

Q3)
   Ans: print(gameBoard)

   If you have done the print board

Q4)
   Ans: 5
   mystery(1, 5)
   Since n =1 , so it retirn 5

Q5)
   Ans: 0
   powerOfTwo(63)
       since 63%2 == 1, so it return false
       So, it make 0 recursive call

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote