1) Which of the following is considered by the text to be the most important consideration when designing a class?
A) Each class should represent a single concept or object from the problem domain.
2) Which of the following questions should you ask yourself in order to determine if you have named your class properly?
A) Can I visualize an object of the class?
3) Which statement is NOT a description of encapsulation?
A) A collection of methods through which the objects of the class can be manipulated
4) Which of the following statements is not legal?
A) [“Hello”, “World”].lower()
5) Which of the following is NOT a true statement regarding object-oriented programming?
A) Object oriented programming views the program as a list of tasks to perform.
6) Which statement about classes is true?
A) A class describes a set of objects that all have the same behavior.
7) Which statement is correct about the public interface for a class?
A) The public interface for a class is the set of all methods provided by a class, together with a description of their behavior.
8) What is the purpose of an object’s instance variables?
A) Store the data required for executing its methods
9) Naming conventions for Python dictate that instance variables should start with an underscore to represent:
A) non-public visibility
10) What is the purpose of a method?
A) To access the instance variables of the object on which it acts
11) What is the purpose of the self parameter?
A) Refers to the object on which the method was invoked
12) According to the textbook, what is the best practice for updating instance variables?
A) Restrict access to instance variables, only allow updates through methods
13) Assume a class exists named Fruit. Which of the follow statements constructs an object of the Fruit class?
A) x = Fruit()
14) Which of the following statements is used to begin the implementation of a new class named Fruit?
A) class Fruit :
15) What is the name of the instance variable in the following code segment?
class Fruit :
def getColor(self) :
return self._color
A) _color
16) What is the name of the method in the following code segment?
class Fruit :
def getColor(self) :
return self._color
A) getColor
17) How many methods are there in the following class?
class Person :
def getName(self) :
return self._name
def getSalary(self) :
return self._salary
def giveRaise(self, howMuch) :
self._salary = self._salary + howMuch
A) 3
18) 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?
A) reset
19) The following two objects are created from the Counter class: studentCounter, and teacherCounter to represent the total number of students and the total number of teachers respectively. If the Counter class contains an instance variable, _num, that is initialized to zero and increases every time the user executes the add method, what is stored in each object’s instance variable after the following code snippet executes?
studentCounter.add()
teacherCounter.add()
studentCounter.add()
A) studentCounter : 2, teacherCounter : 1
20) Which of the following is NOT a difference between methods and functions?
A) A function is defined within a class. A method is defined outside of a class.
21) 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?
A) Use methods provided by the ShoppingList class.
22) When designing a class, what is one of the first tasks that need to be done?
A) Specifying the public interface
23) What items should be considered when creating the public interface?
A) Method headers and method comments
24) Before invoking a method on an object, what must be done first?
A) Construct the object
25) Which method below would be considered an accessor method?
A) getCount()
26) Which method below would be considered a mutator method?
A) addItem()
27) 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?
A) Only line 2
28) 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?
A) getValue
29) 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
A) Only click, unClick and reset
30) Assume that the class ShoppingList has already been defined with the following public interface:
## A simulated grocery list
#
class ShoppingList :
## Adds an item to this shopping list.
# @param cost the cost of this item
# @param quantity the number of items
# @param itemName the name of the item
#
def addItem(self, cost, quantity, itemName) :
# implementation hidden
## Gets the number of items in the list.
# @return the item count
#
def getCount(self) :
# implementation hidden
## Clears the list.
#
def clear(self) :
# implementation hidden
What is wrong with the following code segment?
list.clear()
list = ShoppingList()
list.addItem(2.95, 1, "milk")
list.addItem(.50, 3, "cucumber")
print(list.getCount())
A) The list object has not been created before invoking the clear method.
31) Assume that the class ShoppingList has already been defined with the following public interface:
## A simulated grocery list
#
class ShoppingList :
## Adds an item to this shopping list.
# @param cost the cost of this item
# @param quantity the number of items
# @param itemName the name of the item
#
def addItem(self, cost, quantity, itemName) :
# implementation hidden
## Gets the number of items in the list.
# @return the item count
#
def getCount(self) :
# implementation hidden
## Clears the list.
#
def clear(self) :
# implementation hidden
What is wrong with the following code segment?
list = ShoppingList()
list.clear()
list.addItem(2.95, 1, "milk")
list.addItem(.50, 3, "cucumber")
print(list.getItems())
A) There is no getItems method defined for the ShoppingList class.
32) 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
A) firstName, lastName, hoursWorked, hourlyWage
33) Which name would be best for a non-public instance variable?
A) _confidential
34) What is the purpose of a constructor?
A) To define and initialize the instance variables of an object
35) When is a constructor invoked?
A) When an object is created
36) Which of the following method headers represent a constructor?
A) def __init__(self) :
37) Consider the class Employee:
class Employee :
def __init__(self, firstName, lastName, employeeId) :
self._name = lastName + "," + firstName
self._employeeId = employeeId
. . .
If an object is contructed as:
sam = Employee("Sam", "Fisher", 54321)
What is the contents of the instance variable _name?
A) Fisher, Sam
38) Which of the following method headers represent a constructor for an Employee class?
A) def __init__(self) :
39) How many constructors can be defined for each class?
A) Only one may be defined
40) What method name is used for a constructor?
A) __init__
41) What is wrong with the following constructor?
def __init__() :
self._weight = 0.0
self._color = "None"
A) The constructor must take the self parameter
42) Consider the following code segment which constructs two objects of type Fruit:
x = Fruit()
y = Fruit("Banana", "Yellow")
Which constructor header will construct both objects successfully?
A) def __init__(self, name=””, color=””) :
43) Which of the following is NOT true about constructors?
A) The constructor is defined using the special method name __default__
44) Consider the following code segment:
class PhoneNumber :
def __init__(self, lName, phone = "215-555-1212") :
self._name = lName
self._phone = phone
def getName(self):
return self._name
def getPhone(self):
return self._phone
Jones = PhoneNumber("Jones")
print(Jones.getName(), Jones.getPhone())
What will be printed when it executes?
A) Jones 215-555-1212
45) What change needs to be made in the following code segment so that lName will have a default value of “unknown“?
class PhoneNumber :
def __init__(self, lName, phone = "215-555-1212") :
self._name = lName
self._phone = phone
A) Replace the lName parameter with lName = "unknown"
46) How do you access instance variables in a method?
A) Using the self reference
47) Given the following code snippet, what statement completes the code to add several items to one grocery list:
def addItems(self, price, quantity, itemName) :
for i in range(quantity) :
________________________
def addItem(self, price, itemName) :
self._itemCount = self._ItemCount + 1
self._totalPrice = self._totalPrice + price
. . .
A) self.addItem(price, itemName)
48) Consider the following code segment:
def mutate(self, newType) :
self._type = newType
self._mutations = self._mutations + 1
What is the name of the local variable in it:
A) newType
49) Consider the following code segment:
class Fruit :
_type = "Fruit"
def __init__(self, color) :
self._color = color
What is the name of the class variable?
A) _type
50) Which of the following is NOT true about instance methods for a class?
A) The accessor and mutator methods are automatically called when an object is created
51) Consider the following class:
class Pet:
def __init__(self, name):
self._name = name
self._owner = "<unknown>"
def setOwner(self, owner):
____________________
What line of code should be placed in the blank to complete the setOwner mutator method that is supposed to update the pet owner’s name?
A) self._owner = owner
52) Class variables are also known as:
A) static variables
53) Where should instance variables be defined?
A) In constructors
54) Consider the following class:
class Pet:
____________________
def __init__(self, name):
self._name = name
Pet._lastID = Pet._lastID + 1
self._registrationID = Pet._lastID
What line of code should be placed in the blank to create a class variable that keeps track of the most recently used registration identifier?
A) _lastID = 0
55) Which of the following is NOT considered part of unit testing for a class?
A) Identify syntax errors
56) Which of the following is NOT a step carried out by a tester program?
A) Identify syntax errors
57) Which of the follow code segments could be used to help test the getColor method from the Fruit class?
A) print(f.getColor())
print(“Expected: Yellow”)
58) What is the purpose of unit testing?
A) To verify that a class works correctly in isolation, outside a complete program.
59) What term is used to describe the process of verifying that a class works correctly in isolation, outside of a complete program?
A) Unit testing
60) When using the process of tracing objects for problem solving, which types of methods update the values of the instance variables?
A) Both B and C
61) 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())
A) 555-123-4567
62) Which of the following patterns can be used for designing your class to help keep track of the number of fees charged by a bank?
A) Counting events
63) Which of the following patterns can be used for designing your class to update the balance of a bank account?
A) Keeping a total
64) Which of the following patterns can be used for designing your class to add error checking such as not allowing a blank name for a bank account?
A) Managing Properties of an Object
65) 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?
A) Describing the Position of an Object
66) Consider the following class which is used to represent a polygon consisting of an arbitrary number of (x, y) points:
class Polygon :
def __init__(self) :
self._x_points = []
self._y_points = []
Which of the following code segments is the correct implementation for the addPoint method that adds another point to the polygon?
A) def addPoint(self, x, y) :
self._x_points.append(x)
self._y_points.append(y)
67) Consider a class that represents a hardware device. The device can be in one of two states: Plugged in, or unplugged. Which of the following class definitions is best for this situation?
A) class Device :
PLUGGED_IN = 0
UNPLUGGED = 1
def __init__(self) :
. . .
68) Which of the following is NOT a pattern used to help design the data representation of a class?
A) An object can collect other objects in a list
69) What is the value of the variable bankAcct after the following code snippet is executed?
bankAcct = BankAccount("Fisher", 1000.00)
A) A memory location
70) Given the following code snippet, how can you test if they reference the same object (or does not refer to any object)?
bankAcct2 = bankAcct
A) if bankAcct is bankAcct2 :
print(“The variables are aliases”)
71) Which statement determines if the middleInit variable is empty or does not refer to any object?
A) if middleInit is None :
print(“No middle initial”)
72) What happens when an object is no longer referenced?
A) The garbage collector removes the object
73) Which of the following is NOT true about objects?
A) The == and != operators test whether two variables are aliases
74) Given the variable assignment sentence = "", what is the value of len(sentence)?
A) 0
75) Which of the following statements sets x so that it refers to no object at all?
A) x = None
76) Recall the Cash Register class developed in the textbook. What is output by the following code segment?
from cashregister2 import CashRegister
reg1 = CashRegister()
reg1.addItem(3.00)
reg2 = reg1
reg1.addItem(5.00)
print(reg2.getCount())
A) 2
77) Recall the Cash Register class developed in the textbook. What is output by the following code segment?
from cashregister2 import CashRegister
reg1 = CashRegister()
reg2 = reg1
reg1.addItem(3.00)
reg1.addItem(5.00)
reg2.clear()
print(reg1.getCount())
A) 0
78) Consider the following code segment:
bankAcct = BankAccount("Fisher", 1000.00)
bankAcct2 = bankAcct
What is the relationship between bankAcct and bankAcct2?
A) bankAcct and bankAcct2 are aliases for the same object reference
79) What does an object reference specify?
A) The location of an object
80) What part of the virtual machine is responsible for removing objects that are no longer referenced?
A) The garbage collector
81) Assume that x is a variable containing an object reference. Which special method is invoked when the following statement executes?
print(x)
A) __repr__
82) Which of the following statements determines if x currently refers to an object containing an integer?
A) if isinstance(x, int) :
83) Consider the following class which will be used to represent complex numbers:
class Complex:
def __init__(self, real, imaginary):
self._real = real
self._imaginary = imaginary
def ____________________:
real = self._real + rhsValue._real
imaginary = self._imaginary + rhsValue._imaginary
return Complex(real, imaginary)
What code should be placed in the blank so that two complex numbers can be added using the + operator?
A) __add__(self, rhsValue)
