Question: Consider the triangleArea function from the textbook shown below: 1. def triangleArea(sideLength) : 2. if sideLength
Consider the triangleArea function from the textbook shown below:
1. def triangleArea(sideLength) :
2. if sideLength <= 0 :
3. return 0
4. if sideLength == 1 :
5. return 1
6. smallerSideLength = sideLength - 1
7. smallerArea = triangleArea(smallerSideLength)
8. area = smallerArea + sideLength
9. return area
What will happen if lines #2 and #3 are replaced with the following code segment?
if sideLength <= 0 :
return sideLength
Question options:
| The function will still return correct results for all triangles with non-negative side lengths. | |||
| The function will return incorrect results when the side length is equal to 0. | |||
| The function will return incorrect results when the side length is equal to 1. | |||
| The function will return an area value that is too large for all triangles with non-negative side lengths. | |||
| Question 6 | 0 / 1 point | ||
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 7 | 0 / 1 point | ||
Consider the triangleArea function from the textbook shown below:
1. def triangleArea(sideLength) :
2. if sideLength <= 0 :
3. return 0
4. if sideLength == 1 :
5. return 1
6. smallerSideLength = sideLength - 1
7. smallerArea = triangleArea(smallerSideLength)
8. area = smallerArea + sideLength
9. return area
Assume that line #5 is changed to this:
smallerArea = triangleArea(sideLength)
This would cause infinite recursion for __________________________________
Question options:
| triangles with sideLength equal to 0 | |||
| triangles with sideLength equal to 1 | |||
| triangles with sideLength greater than or equal to 1 | |||
| triangles of any sideLength | |||
| Question 8 | 0 / 1 point | ||
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) |
How many permutations are in a 5 letter word?
Question options:
| 5 | |||
| 120 | |||
| 12 | |||
| 1 | |||
| Question 2 | 0 / 1 point | ||
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 7 | 0 / 1 point | ||
Which of the following strings is a palindrome?
Question options:
| "Test" | |||
| "B" | |||
| "canal" | |||
| "salami" | |||
| Question 10 | 0 / 1 point | ||
How many squares are drawn by the following code segment?
def tSquare(width, x, y, canvas) :
canvas.drawRect(x, y, width, width)
if width >= 4 :
tSquare(width / 2, x, y, canvas)
tSquare(width / 2, x + width / 2, canvas)
tSquare(width / 2, x, y + width / 2, canvas)
tSquare(width / 2, x + width / 2, y + width / 2, canvas)
# Code to setup the canvas has been omitted
tSquare(0, 0, 16, canvas)
Question options:
| 16 | |
| 21 | |
| 64 | |
| 85 |
| Question 3 | 0 / 1 point |
Which of the following options could be used as a terminating condition for a recursive function that finds the middle character of a String with any number of characters?
I. the length of the String is 1
II. first and last String characters match
III. the String is not empty
Question options:
| I | |||
| II | |||
| I, II, and III | |||
| I and III | |||
| Question 5 | 0 / 1 point | ||
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)
What is the best interpretation of lines 2 and 3?
Question options:
| One is a power of two. | |||
| One is not a power of two. | |||
| Any multiple of one is a power of two. | |||
| The integer 1 is an invalid choice for n. | |||
| Question 7 | 0 / 1 point | ||
A unique permutation is one that is different from any other generated permutation. How many unique permutations does the string "bee" have?
Question options:
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| Question 8 | 0 / 1 point | ||
The following code segment is supposed to determine whether or not a string is a palindrome, meaning that it is the same forward and backward.
def isPalindrome(s) :
return palindromeHelper(s, l, h)
def palidromeHelper(s, l, h) :
if h <= l :
return True
if s[l] != s[h] :
return False
____________________
What line of code should be placed in the blank to achieve this goal?
Question options:
| isPalindrome(s, l, h) | |||
| isPalindrome(s, l + 1, h - 1) | |||
| palindromeHelper(s, l, h) | |||
| palindromeHelper(s, l + 1, h - 1) | |||
| Question 9 | 0 / 1 point | ||
What is the purpose of a recursive helper function?
Question options:
| Shield the user of the recursive function from the recursive details | |
| Speed up the execution | |
| Eliminate the recursion | |
| Add another base case |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
