Question: Question 5 0 / 1 point When designing a class, what is one of the first tasks that need to be done? Question options: Defining

Question 5

0 / 1 point

When designing a class, what is one of the first tasks that need to be done?

Question options:

Defining the instance variables

Writing the method headers

Specifying the public interface

Adding comments to your program

Question 6

0 / 1 point

Consider the following code segment:

class Fruit : 
 _type = "Fruit" 
 def __init__(self, color) : 
 self._color = color 

What is the name of the class variable?

Question options:

color

_color

self

_type

Question 7

0 / 1 point

Where is an object's data stored?

Question options:

Global variables

Instance variables

Local variables

Parameter variables

Question 10

0 / 1 point

Which of the following is NOT a pattern used to help design the data representation of a class?

Question options:

An instance variable for thte total is updated in methods that increase or decrease the total amount

An object reference specifies the location of an object

An object can collect other objects in a list

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

Question 12

0 / 1 point

What method name is used for a constructor?

Question options:

__begin__

__construct__

__create__

__init__

Question 13

0 / 1 point

Which name would be best for a private instance variable?

Question options:

_confidential

HIDDEN

private

Secret

Question 18

0 / 1 point

How do you access instance variables in a method?

Question options:

Using the constructor

Using the public interface

Using a default value

Using the self reference

Question 19

0 / 1 point

Which of the following patterns can be used for designing your class for a train object that drives along a track and keeps track of the distance from the terminus?

Question options:

Describing the Position of an Object

Modeling Objects with Distinct States

Managing Properties of an Object

Collecting Values

Consider the following class:

class Counter : 
 def getValue(self) : 
 return self._value 
 def click(self) : 
 self._value = self._value + 1 
 def unClick(self) : 
 self._value = self._value - 1 
 def reset(self) : 
 self._value = 0 

Which method creates a new instance variable?

Question options:

click

getValue

reset

unClick

Question 7

0 / 1 point

Consider the following class:

class Fruit : # Line 1 
 def getColor(self) : # Line 2 
 retval = self._color # Line 3 
 return retval # Line 4 

Which line(s) are part of the public interface for the class?

Question options:

Only line 1

Only line 2

Only lines 2 and 3

Only lines 3 and 4

Question 9

0 / 1 point

Consider the following class:

class Counter : 
 def getValue(self) : 
 return self._value 
 def click(self) : 
 self._value = self._value + 1 
 def unClick(self) : 
 self._value = self._value - 1 
 def reset(self) : 
 self._value = 0 

Which method is an accessor?

Question options:

getValue

click

unClick

reset

Question 11

0 / 1 point

Consider the following class:

class Counter : 
 def getValue(self) : 
 return self._value 
 def click(self) : 
 self._value = self._value + 1 
 def unClick(self) : 
 self._value = self._value - 1 
 def reset(self) : 
 self._value = 0 

Which method(s) are mutators?

Question options:

Only reset

Only click and unClick

Only click, unClick and reset

All four methods are mutators

Question 14

0 / 1 point

Consider the following class:

class Contact : 
 def __init__(self, name, phone="") 
 self._name = name 
 self._phone = phone 
 def getName(self) : 
 return self._name 
 def setName(self, new_name) : 
 self._name = new_name 
 def getPhone(self) : 
 return self._phone 
 def setPhone(self, new_phone) : 
 self._phone = new_phone 

What is output by the following code segment?

p1 = Contact("Bob", "555-123-4567") 
p2 = Contact("Joe", "555-000-0000") 
p1.setName(p2.getName()) 
print(p1.getPhone()) 

Question options:

Bob

Joe

555-000-0000

555-123-4567

Question 16

0 / 1 point

Which method below would be considered an accessor method?

Question options:

 getCount() 
 addItem() 
 clearCount() 
 updateItem() 

Question 17

0 / 1 point

Which statement determines if the middleInit variable is empty or does not refer to any object?

Question options:

 if middleInit is None : 
 print("No middle initial") 
 if middleInit is not None : 
 print("No middle initial") 
 if middleInit == None : 
 print("No middle initial") 
 if middleInit != None : 
 print("No middle initial") 

Question 18

0 / 1 point

Which of the following statements determines if x currently refers to an object containing an integer?

Question options:

if int(x) :

if x == int :

if x is int :

if isinstance(x, int) :

Suppose you have a class ShoppingList, with instance variables _quantity, _cost, and _itemName. How should you access these variables when writing code that is not contained within the ShoppingList class?

Question options:

Directly access the instance variables by using the object and the instance variables' names.

Use methods provided by the ShoppingList class.

It is not possible to access the instance variables outside of the ShoppingList class.

Use the self parameter followed by the instance variables' names.

Question 7

0 / 1 point

What is the purpose of the self parameter?

Question options:

Enables changes in the implementation without affecting users of a class

To create variables with public visibility

Store the data for all objects created by the same class

Refers to the object on which the method was invoked

Question 11

0 / 1 point

When using the process of tracing objects for problem solving, which types of methods update the values of the instance variables?

Question options:

Accessor methods

Mutator methods

Constructors

Both B and C

Question 12

0 / 1 point

Before invoking a method on an object, what must be done first?

Question options:

Initialize all instance variables

Invoke the accessor and mutator methods

Create a public interface

Construct the object

Question 14

0 / 1 point

Which of the following method headers represent a constructor for an Employee class?

Question options:

 def Employee(self) : 
 def __init()__ : 
 def __Employee__(self) : 
 def __init__(self) : 

Question 15

0 / 1 point

Which of the following statements is not legal?

Question options:

["Hello", "World"].lower()

"Hello, World".lower()

["Hello", "World"].pop()

["Hello, World"].pop()

Question 16

0 / 1 point

Consider the following class:

class Pet: 
 def __init__(self, name): 
 self._name = name 
 def getName(self): 
 ____________________ 

What line of code should be placed in the blank to complete the getName accessor method that is supposed to return the pet's name?

Question options:

return

return _name

return self

return self._name

Question 19

0 / 1 point

In the following example, which data is considered instance data?

You are assigned the task of writing a program that calculates payroll for a small company. To get started the program should do the following:

Add new employees including their first and last name and hourly wage

Ask for the number of hours worked

Calculate payroll (applying 1.5 for any hours greater than 40)

Print a report of all employees' salary for the week, total of all hours and total of all salaries

Question options:

 firstName, lastName, hoursWorked, hourlyWage 
 firstName, lastName, hoursWorked, hourlyWage, payrollAmount 
 firstName, lastName, hoursWorked, hourlyWage, totalHours, totalSalary 
 firstName, lastName, hoursWorked, hourlyWage, payrollAmount, totalHours, totalSalary 

Question 20

0 / 1 point

Which method below would be considered a mutator method?

Question options:

 getCount() 
 addItem() 
 getTotal() 
 printItem() 

A subclass object can always be used when a superclass object is expected. This fact is referred to as:

Question options:

generalized interoperability

method overriding

the replacement policy

the substitution principle

Question 4

0 / 1 point

Assume that a Programmer class inherits from an Employee class, and that both classes have an implementation of an increaseSalary method with the same set of parameters. Which class's increaseSalary method is to be executed is determined by:

Question options:

checking whether or not the super function is called in the method body.

the number of instance variables in the class.

the order in which the programmer declared the classes.

the type of the object on which the method is invoked.

Question 6

0 / 1 point

Which method is being overridden in the following code segment?

class Car : 
 def __init__(self, make, model, color) : 
 . . . 
 def getMake(self) : 
 . . . 
 def getModel(self) : 
 . . . 
 def getColor(self) : 
 . . . 
class Apple : 
 def __init__(self, color) : 
 . . . 
 def getColor(self) : 
 . . . 

Question options:

getMake

getModel

getColor

No method is being overridden in this code segment

Question 7

0 / 1 point

Consider the following code snippet:

anEmployee = new Programmer() 
anEmployee.increaseSalary(2500) 

If the Programmer class inherits from the Employee class, and both classes have an implementation of the increaseSalary method with the same set of parameters, which statement is correct?

Question options:

An additional line of code must be added to the snippet shown previously to specify which method will execute.

It is not possible to determine which class's method will be executed without seeing the complete class definitions.

The increaseSalary method of the Employee class will be executed.

The increaseSalary method of the Programmer class will be executed.

Question 8

0 / 1 point

You are creating a class inheritance hierarchy about motor vehicles that will contain classes named Vehicle, Auto, and Motorcycle. Which of the following statements is correct?

Question options:

Vehicle should be the subclass, while Auto, and Motorcycle should be the default classes

Vehicle should be the subclass, while Auto, and Motorcycle should be the superclasses

Vehicle should be the default class, while Auto, and Motorcycle should be the subclasses

Vehicle should be the superclass, while Auto, and Motorcycle should be the subclasses

Question 10

0 / 1 point

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

Attempt Score:

Assuming the Rectangle class is already designed with a constructor __init__(self, x, y, width, height), which code snippet creates a square in the middle of a frame with FRAME_WIDTH = 400, FRAME_HEIGHT = 600?

Question options:

 square = Rectangle(FRAME_WIDTH/2, FRAME_HEIGHT/2, 100, 100) 
 square = Rectangle(FRAME_WIDTH, FRAME_HEIGHT, 100, 100) 
 square = Rectangle(FRAME_WIDTH-50, FRAME_HEIGHT - 50, 100, 100) 
 square = Rectangle(FRAME_WIDTH/2 - 50, FRAME_HEIGHT/2 - 50, 100, 100) 

Question 7

0 / 1 point

Consider the following class hierarchy:

class Vehicle : 
 
 def __init__(self, type) : 
 self._type = type 
 
class LandVehicle(Vehicle) : 
 def __init__(self, type) : 
 super().__init__(type) 
 
class Auto(Vehicle) : 
 def __init__(self, type) : 
 ____________________________ 

Complete the code in the Auto class constructor to store the type data.

Question options:

 super(type) 
 super().super().__init__(type) 
 super().__init__(type) 

This cannot be done unless the Auto class declares an instance variable named type.

Question 8

0 / 1 point

Which of the following statements indicates that ClassA is a superclass of ClassB?

Question options:

class ClassB(ClassA) :

class ClassA(ClassB) :

class ClassB extends ClassA :

class ClassA extends ClassB :

Question 10

0 / 1 point

Which of the following statements is true?

Question options:

A method that is overridden in a subclass can perform a task that is totally different from the task performed by the same method in the superclass.

A method that is overridden in a subclass cannot invoke the method in the superclass with the same name.

A method that is overridden in a subclass must extend the functionality of the superclass by invoking the superclass method.

Every method in a subclass must override a superclass method.

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 6

0 / 1 point

Which of the following statements about abstract methods is true?

Question options:

An abstract method has a name and parameters, but its implementation is not specified.

An abstract method has parameters and is fully implemented, but it has no defined name.

An abstract method has a name and is fully implemented, but it has no parameters.

An abstract method has only a name, but it implementation is not specified and it has no parameters.

Question 9

0 / 1 point

Which of the following is true regarding subclasses?

Question options:

A subclass that inherits methods from its superclass may not override the methods.

A subclass that inherits instance variables from its superclass may not declare additional instance variables.

A subclass may inherit methods or instance variables from its superclass but not both.

A subclass may inherit methods and instance variables from its superclass, and may also implement its own methods and declare its own instance variables.

Complete the code for the myFactorial recursive function shown below, which is intended to compute the factorial of the value passed to the function:

1. def myFactorial(n) : 
2. if n == 1 : 
3. return 1 
4. else : 
5. _______________________ 

Question options:

 return n * myFactorial(n) 
 return (n - 1) * myFactorial(n) 
 return n * myFactorial(n - 1) 
 return (n - 1) * myFactorial(n - 1) 

Question 2

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(3,6)

Question options:

 3 
 6 
 18 
 729 

Question 3

0 / 1 point

A palindrome is a word or phrase that reads the same forward and backward. Consider the functions palindrome and isPal shown below:

def palindrome(s) : 
 return isPal(s, 0, len(s) - 1) 
 
def isPal(s, left, right) : 
 if left >= right : 
 return True 
 elif s[left] == s[right] : 
 return isPal(s, left + 1, right - 1) 
 else : 
 return False 

The function isPal as shown here would be considered to be a ____ function.

Question options:

iterative

method

helper

static

Question 4

0 / 1 point

Consider the following recursive function:

def myPrint(n) : 
 if n < 10 : 
 print(n) 
 else : 
 m = n % 10 
 print(m, end="") 
 myPrint(n // 10) 

What is printed for the call myPrint(821)?

Question options:

10

12

128

821

Question 6

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 7

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 8

0 / 1 point

A palindrome is a word or phrase spelled which reads the same forward or backward. Consider the following code snippet:

def palindrome(s) : 
 return isPal(s, 0, len(s) - 1) 
 
def isPal(s, left, right) : 
 if left >= right : 
 return True 
 elif s[left] == s[right] : 
 return isPal(s, left + 1, right - 1) 
 else : 
 return False 

What does the function palindrome return?

Question options:

True

False

a palindrome not found exception

the Boolean value returned from the isPalfunction

Question 9

0 / 1 point

Consider the following function:

1. def mystery(n, m) : 
2. if n == 0 : # special case 1 
3. return 0 
4. if n == 1 : # special case 2 
5. return m 
6. return m + mystery(n - 1), m) 

What will happen if lines #2 and #3 were swapped with lines #4 and #5?

Question options:

The original function and the modified function will return the same result for all integer values of n and m.

The original function and the modified function will return different results for all integer value of n and m.

The original function and the modified function will return the same result when n is greater than m, and different results when m is greater than n.

The original function and the modified function will return the same result when n is less than m, and different results when m is less than n.

Question 10

0 / 1 point

Recursion will take place if any of the following happen:

I. function A calls function B, which calls function C

II. function A calls function B, which calls function A

III. function A calls function A

Question options:

I

I and II

II

II and III

6 / 10 - D

8 / 10 - B

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 2

0 / 1 point

Which statement finds all of the links in a webpage?

Question options:

elements = doc.find_all("a")

elements = doc.find_all("li")

elements = doc.find_all(links)

elements = doc.find_links()

Question 3

0 / 1 point

Complete the code for the myFactorial recursive function shown below, which is intended to compute the factorial of the value passed to the function:

1. def myFactorial(n) : 
2. if n == 1 : 
3. _____________________________ 
4. else : 
5. return n * myFactorial(n - 1) 

Question options:

 return 0 
 return -n 
 return 1 
 return myFactorial(n) 

Question 4

0 / 1 point

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

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

Question options:

8

6

4

2

Question 7

0 / 1 point

Consider the following code segment:

def f1(n) : 
 if n < 0 : 
 return 0 
 if n % 2 == 1 : 
 return n 
 return f2(n + 1) 
def f2(n) : 
 if n < 0 : 
 return 0 
 if n % 2 == 0 : 
 return n 
 return f1(n // 2) 

This code segment would be classified as:

Question options:

backtracking

mutually recursive

non-recursive

object oriented

Question 10

0 / 1 point

Consider the following code segment:

def f1(n) : 
 if n < 0 : 
 return 0 
 if n % 2 == 1 : 
 return n 
 return f2(n + 1) 
def f2(n) : 
 if n < 0 : 
 return 0 
 if n % 2 == 0 : 
 return n 
 return f1(n // 2) 
print(f2(7)) 

When this code is run, it will display:

Question options:

0

1

3

7

Question 2

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 3

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 4

0 / 1 point

Recall the permutations function from Section 11.5 in the textbook.

def permutations(word) : 
 result = [] 
 if len(word) == 0 : 
 result.append(word) 
 return result 
 else: 
 for i in range(len(word)) : 
 shorter = word[ : i] + word[i + 1 : ] 
 shorterPermutations = permutations(shorter) 
 for string in shorterPermutations : 
 result.append(word[i] + string) 
 return result 

How permutations will be generated by calling permutations("code")?

Question options:

1

4

10

24

Question 5

0 / 1 point

How many recursive calls to the fib function shown below would be made from an original call to fib(4)? (Do not count the original call)

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) 

Question options:

1

2

4

8

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 

What will happen if line #6 is changed to:

smallerSideLength = sideLength 

Question options:

Infinite recursion will occur when sideLength is equal to 0.

Infinite recursion will occur when sideLength is equal to 1.

Infinite recursion will occur when sideLength is greater than or equal to 2.

Infinite recursion will occur for any value of sideLength.

Question 10

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)

A particular algorithm visits n3 + nlog(n) + n! elements in order to perform its task on a list of n elements. Which big-Oh expression best describes the growth rate of this algorithm?

Question options:

O(n)

O(n3)

O(n!)

O(nlog(n))

Question 9

0 / 1 point

Which sort algorithm starts with an initial sequence of size 1, which is assumed to be sorted, and increases the size of the sorted sequence in the list in each iteration?

Question options:

insertion sort

selection sort

merge sort

quicksort

Question 10

0 / 1 point

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!