What is the role of polymorphism? Question options: Polymorphism allows a progra
ID: 3828435 • Letter: W
Question
What is the role of polymorphism?
Question options:
Polymorphism allows a programmer to manipulate objects that share a set of tasks, even though the tasks are executed in different ways.
Polymorphism allows a programmer to use a subclass object in place of a superclass object.
Polymorphism allows a subclass to override a superclass method by providing a completely new implementation.
Polymorphism allows a subclass to extend a superclass method by performing the superclass task plus some additional work.
Assume that you have a class Apple which is a subclass of Fruit. Which statement can be used in Apple's constructor to invoke Fruit's constructor?
Question options:
__init__()
Apple().__init__()
Fruit().__init__()
super().__init__()
What is dynamic method lookup?
Question options:
Dynamic method lookup is the process of determining, at runtime, what method will be invoked based on the type of the object.
Dynamic method lookup is the process of finding a method amongst a collection of classes that do not have a common superclass (other than object).
Dynamic method lookup is the process of finding a method in a superclass when it has not been overridden in a subclass.
Dynamic method lookup is the process of overriding a method in a subclass that has already been defined in the superclass.
What is the purpose of using an inheritance hierarchy?
Question options:
To share common code among the classes
To create objects from concrete classes
To create objects from abstract classes
To create objects using constructors
Assuming the Rectangle class is already designed with a constructor __init__(self, x, y, width, height), which code snippet creates a square in the top left corner?
Question options:
square = Rectangle(0, 0, 100, 100)
square = Rectangle(100, 100, 0, 0)
square = Rectangle(0, 0, 0, 0)
square = Rectangle(100, 100, 100, 100)
0 / 1 point
Assuming that a class called Oval is already defined with a constructor __init__(self, x, y, width, height), how could this be used to make a circle?
Question options:
Use one value for x and y
Pass the same value for each width and height
Use one value for x, y, width, and height
It is not possible to make a circle using the Oval class
0 / 1 point
Given the code snippet below, what methods does an object of the Rectangle class have?
class GeometricShape :
def __init__(self, x, y) :
self._x = x
self._y = y
self._fill = None
self._outline = "blue"
. . .
def getX(self) :
return self._x
def getY(self) :
return self._y
class Rectangle(GeometricShape) :
def __init__(self, x, y, width, height) :
super().__init__(x, y)
self._width = width
self._height = height
def getWidth(self) :
return self._width
def getHeight(self) :
return self._height
Question options:
getWidth(), getHeight()
getX(), getY(), getWidth(), getHeight()
getX(), getY(), setColor()
getX(), getY(), getWidth(), getHeight()
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 4
0 / 1 point
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 9
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 10
0 / 1 point
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
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 3
0 / 1 point
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 4
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 5
0 / 1 point
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 6
0 / 1 point
What is displayed when the following program executes?
def mystery(s) :
return s[len(s) - 1] + mystery(s[0 : len(s) - 1])
mystery("123")
Question options:
0
6
123
321
Question 7
0 / 1 point
How many permutations are in a 5 letter word?
Question options:
5
120
12
1
Question 9
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)
Question 10
0 / 1 point
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.
Consider the following code segment:
def triangleArea(sideLength) :
if sideLength == 1:
return 1
return triangleArea(sideLength - 1) + sideLength
print(triangleArea(5))
How many times is the triangleArea function called when this code segment executes?
Question options:
1
4
5
6
Question 8
0 / 1 point
Why does the best recursive function usually run slightly slower than its iterative counterpart?
Question options:
Testing the terminating condition takes longer.
Each recursive function call takes processor time.
Multiple recursive cases must be considered.
Checking multiple terminating conditions take more processor time.
Question 9
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)
How many recursive calls are made from the original call powerOfTwo(64) (not including the original call)?
Question options:
8
6
4
2
Consider the code for mergeSort shown below, which works correctly:
def mergeSort(values) :
if len(values) <= 1 : return
mid = len(values) // 2
first = values[ : mid]
second = values[mid : ]
mergeSort(first)
mergeSort(second)
mergeLists(first, second, values)
What would happen if the line mid = len(values) // 2 was replaced with the line mid = len(values) // 4
Question options:
Merge sort would continue to work at the same speed as the original version
Merge sort would continue to work, but it would be slower than the original version
Merge sort would continue to work, and it would be faster than the original
The modified version would not sort the list successfully
Question 4
0 / 1 point
Consider the following function, which correctly merges two lists:
def mergeLists(first, second, values) :
iFrist = 0
iSecond = 0
j = 0
while iFirst < len(first) and iSecond < len(second) :
if first[iFirst] < second[iSecond] :
values[j] = first[iFirst]
iFirst = iFirst + 1
else :
values[j] = second[iSecond]
iSecond = iSecond + 1
j = j + 1
while iFrist < len(first) :
values[j] = first[iFirst]
iFirst = iFirst + 1
j = j + 1
while iSecond < len(second) :
values[j] = second[iSecond]
iSecond = iSecond + 1
j = j + 1
What is the big-Oh complexity of this algorithm, where n is the total number of elements in first and second?
Question options:
O(1)
O(log(n))
O(n)
O(n2)
Question 5
0 / 1 point
Consider the selection sort function and function call shown below:
def selectionSort(values) :
for i in range(len(values)) :
print(values)
minPos = minimumPosition(values, i)
swap(values, minPos, i)
data = [9, 1, 7, 2]
selectionSort(data)
print(data)
What is displayed when this code segment executes?
Question options:
[1, 2, 7, 9]
[9, 1, 7, 2]
[1, 9, 7, 2]
[1, 2, 7, 9]
[9, 1, 7, 2]
[1, 9, 7, 2]
[1, 2, 7, 9]
[1, 2, 7, 9]
[9, 1, 7, 2]
[1, 9, 7, 2]
[1, 2, 7, 9]
[1, 2, 7, 9]
[1, 2, 7, 9]
Question 7
0 / 1 point
How many comparisons does selection sort make when sorting a list of length n?
Question options:
n
log(2n)
n(n + 1) / 2
n / 2
Question 8
0 / 1 point
What is printed when the following code segment executes?
def mergeSort(values) :
if len(values) <= 1 : return
print(values)
mid = len(values) // 2
first = values[ : mid]
second = values[mid : ]
mergeSort(first)
mergeSort(second)
mergeLists(first, second, values)
# The implementation of mergeLists has been omitted.
# It can be found in the textbook.
data = [3, 1, 2, 4]
mergeSort(data)
Question options:
[1, 2, 3, 4]
[3, 1, 2, 4]
[1, 2]
[3, 4]
[3, 1, 2, 4]
[3 ,1]
[2, 4]
[3, 1, 2, 4]
[1, 2]
[3, 4]
[1, 2, 3, 4]
In the textbook, we found that the number of element visits for merge sort totaled n + 5nlog2n. Which of the following is the appropriate big-Oh notation for merge sort?
Question options:
5nlog2n
n + log2
n + 5n
nlog2n
Question 2
0 / 1 point
Which sorting algorithm does the following code segment employ?
def mysterySort(values) :
if len(values) <= 1 :
return
first = values[ : mid]
second = values[mid : ]
mysterySort(first)
mysterySort(second)
mergeLists(first, second, values)
Question options:
Merge Sort
Quicksort
Selection Sort
Shell Sort
Question 3
0 / 1 point
If an element is present in a list of length n, how many element visits, on average, are necessary to find it using a linear search?
Question options:
n / 2
n
2n
n2
Question 4
0 / 1 point
Consider the selection sort function shown below:
def selectionSort(values) :
for i in range(len(values)) :
minPos = minimumPosition(values, i)
swap(values, minPos, i)
The function works correctly in its current form. What would happen if the for loop was replaced with: for i in range(len(values) - 1) :?
Question options:
The list would still be sorted, but it would take one less iteration
The list would still be sorted, using the same number of iterations
The list would still be sorted, but it would take one more iteration
A runtime error would occur
Question 5
0 / 1 point
In a sorting algorithm, it may be necessary to find the position of the maximum element in a list, starting from some initial position, start. What code should be placed in the blank to complete the maximumPosition function?
def maximumPosition(values, start) :
maxPos = start
for i in range(start + 1, len(values)) :
____________________
maxPos = i
return maxPos
Question options:
if values[maxPos] > values[i] :
if values[i] > values[maxPos] :
if values[i] < values[maxPos] :
if values[i] <= values[maxPos] :
Question 6
0 / 1 point
Which of the following completes the selection sort function minimumPosition()?
def minimumPosition(values, from) :
minPos = from
for i in range(from + 1, len(values)) :
_____________________________
return minPos
Question options:
if values[i] > values[minPos] : minPos = i
if values[i] < values[minPos] : minPos = i
if values[i] < values[i] : minPos = i
if values[i] < values[minPos] : i = minPos
Question 7
0 / 1 point
What requirement must be satisified before an element can be located in a list using a binary search?
Question options:
The list must only contain integers
The list must be sorted
The length of the list must be a power of 2
The list must not contain any repeated values
Question 8
0 / 1 point
Which of the following expressions most precisely describes the growth behavior of an algorithm?
Question options:
O(n2) (big-Oh)
(n2) (Theta)
(n2) (Omega)
(n2) (Phi)
Question 9
0 / 1 point
An algorithm that cuts the work in half in each step is an ____ algorithm.
Question options:
O(n)
O(n2)
O(log n)
O(n log n)
What must hold true after 5 iterations of selection sort when it is working to sort a list of 10 elements?
Question options:
Exactly 5 more iterations are always necessary to complete the sort
Exactly 4 more iterations are always necessary to complete the sort
Up to 5 more iterations may be needed to complete the sort
Up to 4 more iterations may be needed to complete the sort
Question 5
0 / 1 point
Consider a list with n elements. If we visit each element n times, how many total visits will there be?
Question options:
n
2n
nn
n2
Question 6
0 / 1 point
After 9 iterations of selection sort working on an list of 10 elements, what must hold true?
Question options:
The largest element is correctly placed by default.
One more iteration is needed to complete the sort.
The smallest element is incorrectly placed.
The largest element is incorrectly placed.
Question 7
0 / 1 point
Consider the following code segment that examines the elements of two lists:
matches = 0
for i in range(len(lst1)) :
for j in range(len(lst2)) :
if lst1[i] == lst2[j] :
matches = matches + 1
What can you conclude about the running time of this code segment if both lists contain n elements?
Question options:
Its running time will be O(n).
Its running time will be O(n2).
Its running time will be O(log n).
Its running time will be O(n log n).
Question 8
0 / 1 point
Which element does selection sort place in the correct location during each iteration?
Question options:
The smallest in the list
The smallest element that has not been placed in the correct location during a prior iteration
The largest element in the list
A random element
Question 9
0 / 1 point
Consider the following function for performing a binary search:
def binarySearch(values, low, high, target) :
if low <= high :
mid = (low + high) // 2
if values[mid] == target :
return mid
elif values[mid] < target :
return binarySearch(values, mid + 1, high, target)
else :
return binarySearch(values, low, mid - 1, target)
else :
return -1
How many elements will be visited when values is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], low is 0, high is 9, and target is 2?
Question options:
1
3
5
10
Polymorphism allows a programmer to manipulate objects that share a set of tasks, even though the tasks are executed in different ways.
Polymorphism allows a programmer to use a subclass object in place of a superclass object.
Polymorphism allows a subclass to override a superclass method by providing a completely new implementation.
Polymorphism allows a subclass to extend a superclass method by performing the superclass task plus some additional work.
Explanation / Answer
1. Polymorphism allows a subclass to override a superclass method by providing a completely new implementation.
2. Apple().__init__()
Because in inheritance base class default constructor is automatically called when the subclass constructor is called. Unless you define the other parameterized constructors of base class in subclass.
3.Dynamic method lookup is the process of determining, at runtime, what method will be invoked based on the type of the object.
4.To share common code among the classes
The main use of inheritance is to share the common code so that code redundancy also will be reduced as well.
5.square = Rectangle(0, 0, 100, 100)
Because the position values always starts from 0 and that too it will start from top left only. So that the square will be created in top left only.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.