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

Question 1 In the textbook, we found that the number of element visits for merge

ID: 3828703 • Letter: Q

Question

Question 1

In the textbook, we found that the number of element visits for merge sort totaled n + 5nlog2n. Lets consider sorting 1024 elements. How many visits are needed?

Question options:

1,024

6,144

51,200

52,224

Question 2

An object reference specifies the location of an object

Given the code snippet below, what instance variables 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"

      . . .

class Rectangle(GeometricShape) :

   def __init__(self, x, y, width, height) :

      super().__init__(x, y)

      self._width = width

      self._height = height

      . . .

Question options:

_x, _y, _width, _height, _fill, _outline

_x, _y, _width, _height

_x, _y

_width, _height

An object can collect other objects in a list

Question 3

To model a moving object, you need to store and update its position

If a class has an abstract method, which of the following statements is NOT true?

Question options:

You cannot inherit from this class.

You cannot construct an object from this class.

You can construct an object from this class.

All non-abstract subclasses of this class must implement this method.

Question 4

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()

Question 5

What is wrong with the following classes?

class Person :

   . . .

   def getName(self) :

      return self._name

   . . .

class Physician(Person) :

   . . .

   def getName(self) :

      return "Dr. " + self.getName()

   . . .

Question options:

The return statement cannot include string concatenation

The Physician class cannot contain a method named getName

The body of the getName method in Physician contains a logic error

Physician is not a subclass of Person

Question 6

Consider the following classes:

class Vehicle :

   def __init__(self, type) :

      self._type = type

   def getType(self) :

      return self._type

class LandVehicle(Vehicle) :

   def __init__(self, type) :

      super().__init__(type)

class Auto(LandVehicle) :

   def __init__(self, type) :

      super().__init__(type)

What is displayed by the following code segment?

x = Auto("Sedan")

print(x.getType())Question options:

Auto

Sedan

Vehicle

A runtime error occurs

Question 7

Consider the following program:

class Dinosaur :

   def __init__(self, name="dinosaur") :

      self._name = name

     

   def display(self) :

      print(self._name)

class Triceratops(Dinosaur) :

   def __init__(self) :

      super().__init__("triceratops")

x = Triceratops()

x.display()

What is displayed when it executes?

Question options:

dinosaur

triceratops

Nothing is displayed

The program crashes with a method not implemented error

Question 8

Which of the following statements about inheritance is correct?

Question options:

You can always use a superclass object in place of a subclass object.

You can always use a subclass object in place of a superclass object.

A superclass inherits data and behavior from a subclass.

A superclass inherits only behavior from a subclass.

Question 9

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()

Question 10

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 11

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 12

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 13

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 14

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

Question 15

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 16

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 line calling swap was replaced with: swap(values, i, minPos)?

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 17

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

Question 18

In the textbook, we found that the number of element visits for merge sort totaled n + 5nlog2n. Lets consider sorting 1024 elements. How many visits are needed?

Question options:

1,024

6,144

51,200

52,224

Question 19

Binary search is an ____ algorithm.

Question options:

O(n)

O(n2)

O(log n)

O(n log n)

Question 20

How many elements will be visited when values is [1, 5, 7, 6, 2, 4, 9, 3, 8, 0] and target is 2?

Question options:

0

2

5

10

1,024

6,144

51,200

52,224

Question 2

An object reference specifies the location of an object

Given the code snippet below, what instance variables 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"

      . . .

class Rectangle(GeometricShape) :

   def __init__(self, x, y, width, height) :

      super().__init__(x, y)

      self._width = width

      self._height = height

      . . .

Question options:

_x, _y, _width, _height, _fill, _outline

_x, _y, _width, _height

_x, _y

_width, _height

An object can collect other objects in a list

Question 3

To model a moving object, you need to store and update its position

Explanation / Answer

Answer in bold

Question 1

In the textbook, we found that the number of element visits for merge sort totaled n + 5nlog2n. Lets consider sorting 1024 elements. How many visits are needed?

Answer: 5224 (1024 + 5*1024log_2(1024))

Question 2

Given the code snippet below, what instance variables 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"

      . . .

class Rectangle(GeometricShape) :

   def __init__(self, x, y, width, height) :

      super().__init__(x, y)

      self._width = width

      self._height = height

      . . .

Question options:

Answer: _x, _y, _width, _height, _fill, _outline

Question 3

If a class has an abstract method, which of the following statements is NOT true?

Question options:

Answer: You can construct an object from this class.

Question 4

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:

Answer: getX(), getY(), getWidth(), getHeight()

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