Questions
Below are optional practice problems for Quiz 04. These problems are intended to help you become more comfortable working with classes and objects. This practice is intended to reinforce important concepts and does not necessarily reflect the format of the questions on the quiz. Solutions for each problem can be found at the bottom of this page.
Object-Oriented Programming T/F
- A class definition provides a pattern for creating objects, but doesn’t make any objects itself. (T/F)
- An object can be declared prior to the class definition. (T/F)
- A constructor declaration specifies a return type. (T/F)
- Objects are stored in memory on the stack. (T/F)
- When an object is passed to a function as an argument, the function accesses a copy of it, rather than a reference to the original object. (T/F)
- Attributes are defined on each object. (T/F)
- When you define a method in a class, all objects of that class have that method associated with it. (T/F)
- The first parameter of a method is a reference to the object the method is called on. (T/F)
- Each time you call a constructor, a new object is created on the heap. (T/F)
Function Writing
- Given the class definition of a
Course
, write a function calledfind_courses
that takes in a list of Courses and a string (representing a course prerequisite) to search for. The function should iterate through all of the courses, and return a list of the names of each course that is a 400+ level course and whoseprerequisites
list contains the given string.
Class Writing
Create a
Time
class that has two attributes: hours and minutes.
Define a constructor for the class that takes in 2 optional values for hours and minutes. The default for hours is 12 and the default for minutes is 0.
Write anadd_time
method that takes in an additionalTime
object and returns a newTime
object with the sum of the two times. Note: Result should be in standard (12-hour) time. Example: (10 hr and 50 min) + (4 hr and 20 min) is (3 hr and 10 min)
Write adisplay_time
method that prints out the time in the following format: “Time is __ hours and __ minutes.”
Write adisplay_minute
method that prints out the total minutes in the time. Example: (1 hr 2 min) should print “62 minutes”Write a single line of code that would create a third Time object whose initial hours and minutes values are the sum of the following two Time objects:
t1: Time = Time(2, 50)
t2: Time = Time(1, 20)
Diagramming
Draw a diagram of the code listing below, and answer the questions that follow.
- How many individual
Product
objects were required to evaluate the code listing? - What type would
b[0].uses[0]
evaluate to?
Spot the Error
Answer the questions that follow based on the code listing.
- When this code is run, an error ocurrs. What is missing from the
Plant
class constructor that causes an error? - Assuming the issue from question 18 is corrected, could you access the
species
attribute ofa
with the linea[0]
?
Solutions
Object-Oriented Programming T/F
- T
- F
- F
- F
- F
- T
- T
- T
- T
Function Writing
Class Writing
t3: Time = t1.addTime(t2)
Diagramming 1
- 2
- str
Spot the Error
The first parameter in the constructor should be
self
and lines 8 and 9 should useself
to initialize the attributes. These corrections are shown below:No,
Plant
objects are not subscriptable the way the class is defined right now. If we, for some reason, wanted to do this, we would define the method__getitem__
.