Questions
Solutions for each problem can be found at the bottom of this page.
Conceptual Questions
- A class definition provides a pattern for creating objects, but doesn’t make any objects itself. (T/F)
- By convention, Python class names start with a lowercase letter. (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 copy of the object the method is called on. (T/F)
- A class definition must come before an object of that class is instantiated. (T/F)
- You must have an instance of a class (an object) to call the class’s constructor. (T/F)
- Constructors must have the
self
parameter in their signature. (T/F) - Constructors must take at least one parameter other than the self parameter. (T/F)
- Objects are passed into functions by reference. (T/F)
- The type of an object is the same as the name of the class it is an instance of. (T/F)
- A method is not pure if it creates a new object. (T/F)
- If optional arguments are not passed to a function, the function call will fail. (T/F)
- Union types allow for arguments to be one of multiple types. (T/F)
Memory Diagrams
Produce a memory diagram for the following code snippet, being sure to include its stack and output.
Produce a memory diagram for the following code snippet, being sure to include its stack and output.
Produce a memory diagram for the following code snippet, being sure to include its stack and output.
Class Writing
- This class is slightly challenging, but take it step by step! Create a
ChristmasTreeFarm
class with the following specifications:- The
ChristmasTreeFarm
class should have one attribute: alist[int]
namedplots
. This list will hold values that represent the size of the tree planted in each plot. If the value at an index of the list is 0, then the plot at that index is empty (does not have a tree). Any value other than 0 indicates that a tree is growing in that plot! - The constructor for the class should take two arguments, both of type int. The first parameter should be called
plots
and is anint
representing the total number of plots in the farm. Notice that the attributeplots
and the parameterplots
for this constructor are different, and represent different things! The second parameter should be calledinitial_planting
representing the amount of plots that should be planted initially. These initially planted plots will be trees of size 1. The constructor should initialize theplots
attribute to an emptylist
, and then appendinitial_planting
trees of size 1. After that, the constructor should fill the rest of the plots with zeroes to indicate that they are empty! - The class should define a method called
plant
. This method should take an argument of typeint
, representing the plot index at which a tree should be planted. The tree should be size 1 when planted. If this method is called on a plot that already has a tree, the old tree will be uprooted and replaced with a new baby tree (size 1). - The class should define a method called
growth
. This method should increase the size of each planted tree by 1. Remember that unplanted plots are represented by a 0 in theplots
list. - The class should define a method called
harvest
. This method should take an argument calledreplant
of typebool
that will determine whether this method replants trees (sets them to size 1 after harvest) or leaves the plots empty (sets them to size 0 after harvest). For our method, trees that are at least size 5 will be harvested. The method will return the count of how many trees were successfully harvested (typeint
). - The class should overload the addition operator. This method should be pure and work between two objects of type
ChristmasTreeFarm
. The method should return a newChristmasTreeFarm
object whose size is the sum of the givenChristmasTreeFarm
s, and whose initial plantings are the sum of the number of planted trees in the givenChristmasTreeFarm
s.
- The
Function Writing
18A. Write a function called find_courses
. Given the following Course
class definition, find_courses
should take in a list[Courses]
and a str
prerequisite to search for. The function should return a list of the names
of each Course whose level
is 400+ and whose prerequisites
list contains the given string.
18B. Write a method called is_valid_course
for the Course
class. The method should take in a str
prerequisite and return a bool
that represents whether the course’s level
is 400+ and if its prerequisites
list contains the given string.
Solutions
Conceptual Questions
- True
- False
- True
- False
- True
- False
- True
- False
- True
- True
- False
- False
- True
Memory Diagrams
Note: In the Path constructor frame, variables should be named after parameters a
and b
Class Writing
Function Writing
18A.
18B.