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:
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:
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:
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:
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:
What is the purpose of using an inheritance hierarchy? Question options:
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:
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:
Which of the following statements indicates that ClassA is a superclass of ClassB? Question options:
Which of the following statements is true? Question options:
If a class has an abstract method, which of the following statements is NOT true?
Question options:
Which of the following statements about abstract methods is true? Question options:
Which of the following is true regarding subclasses? Question options:
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:
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:
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:
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:
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:
Why does the best recursive function usually run slightly slower than its iterative counterpart? Question options:
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:
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:
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:
| 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
Get step-by-step solutions from verified subject matter experts
