Question

What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Show that mergesort is a stable algorithm by modifiying the algorithm to sort a list of tuples (first and last names) by...

    Show that mergesort is a stable algorithm by modifiying the algorithm to sort a list of tuples (first and last names) by their second position. (Modify the merge section to break ties) Modify in python def mergesort(mlist): if len(mlist) < 2: return mlist else: mid = len(mlist)//2 return merge( mergesort(mlist[:mid]), mergesort(mlist[mid:])) # merge two sorted lists def merge(left, right): if left == []: return right elif right == []: return left elif left[0] < right[0]: return [left[0]] + merge(left[1:], right)...

  • Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list...

    Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list using node. (from chapter 24). Your code should utilize the selectionsort method from attached and create another method call selectionsort in linkedlist class. To demonstrate the usage of the selection sort method, you should manually create a link list of random integer (say of 5 numbers), and you need to demonstrate the use the selection sort to sorted the link list. Please submit your...

  • Python Merge Sort Adjust the following code so that you can create random lists of numbers of lengths 10, 15, and 20. You will run the merge sort 10 times for each length. Record the run time for the...

    Python Merge Sort Adjust the following code so that you can create random lists of numbers of lengths 10, 15, and 20. You will run the merge sort 10 times for each length. Record the run time for the length and then calculate the average time to sort. Finally, compare the average run time for each length to the average time for the Merge Sort. -------------------------------------------- Initial python code: import random import time def mergeSort(alist): print("Splitting ",alist) if len(alist)>1: mid...

  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

  • without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr,...

    without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr, low, high, x): # Check base case if low > high : return None else: mid = (high + low) // 2 element arr[mid] == X: if element return mid elif element > X: return binary-search(arr, low, mid 1, x) else: return binary_search(arr, mid + 1, high, x) Selection Sort: def selection_sort (arr): for i in range (len(arr)): smallest index = i smallest value...

  • Python - Recursive to non-recursive quick sort. What I have at the moment is a recursive...

    Python - Recursive to non-recursive quick sort. What I have at the moment is a recursive quick sort, I need to make it non-recursive, any help is appreciated! def quickSort(list):     quickSortHelper(list, 0, len(list) - 1) def quickSortHelper(list, first, last):     if last > first:         pivotIndex = partition(list, first, last)         quickSortHelper(list, first, pivotIndex - 1)         quickSortHelper(list, pivotIndex + 1, last) # Partition list[first..last] def partition(list, first, last):     pivot = list[first] # Choose the first element as...

  • Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new...

    Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. Note that as discussed in the video lecture, the index range of the array implementation of a heap is 1:n, NOT 0:n-1 • parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None •...

  • Please answer python questions? class ItemPack ): 21 def-init--(self): self.--storage=[] self. _.jump -1 self. _.mid-self. ..jump...

    Please answer python questions? class ItemPack ): 21 def-init--(self): self.--storage=[] self. _.jump -1 self. _.mid-self. ..jump def __iter.(self) self._.mid-int (len (self..storage)-1)/2.0) self. _.jump 0 return (self def __next..(self): 10 ind self .-_aid+self.--jump if ind<0 or ind >=len (self.--storage): 12 13 14 15 raise StopIteration O vToRet self.--storage [ind] if self.--jump <=0 : self.--Jump 1-self.--Jump - else return (vToRet) self..storage.append (item) return (len(self.--storage)=#0) 17 self.--jump*=-1 19 20def stuff (self, item) 21 22def isEmpty (self) 24def unpack (self): if len (self. .storage)0return...

  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

  • (Please help me with Coding in Python3) AVLTree complete the following implementation of the balanced (AVL)...

    (Please help me with Coding in Python3) AVLTree complete the following implementation of the balanced (AVL) binary search tree. Note that you should not be implementing the map-based API described in the plain (unbalanced) BSTree notebook — i.e., nodes in the AVLTree will only contain a single value. class AVLTree: class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right def rotate_right(self): n = self.left self.val, n.val = n.val, self.val self.left, n.left, self.right, n.right...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT