2022 Fall Prelim2
2022 Fall Prelim2
This 90-minute exam has 5 questions worth a total of 100 points. Scan the whole test before starting.
Budget your time wisely. Use the back of the pages if you need more space. You may tear the pages
apart; we have a stapler at the front of the room.
It is a violation of the Academic Integrity Code to look at any exam other than your
own, look at any reference material, or otherwise give or receive unauthorized help.
You will be expected to write Python code on this exam. We recommend that you draw vertical
lines to make your indentation clear, as follows:
def foo():
if something:
do something
do more things
do something last
You should not use while-loops on this exam. Beyond that, you may use any Python feature that
you have learned about in class (if-statements, try-except, lists, for-loops, recursion and so on).
1. [2 points] Write your last name, first name, and netid, at the top of each page.
Last Name: First: Netid:
Reference Sheet
String Operations
Operation Description
List Operations
Operation Description
Dictionary Operations
Function
Description
or Method
len(d) Returns: number of keys in dictionary d; it can be 0.
y in d Returns: True if y is a key d; False otherwise.
d[k] = v Assigns value v to the key k in d.
del d[k] Deletes the key k (and its value) from the dictionary d.
d.clear() Removes all keys (and values) from the dictionary d.
Page 2
Last Name: First: Netid:
Page 3
Last Name: First: Netid:
Precond: s is a string"""
Each element at position i in the new list becomes the sum up to and including
that position from the list nums. If nums is empty, so is the list returned.
Ex: sumfold([0,1,2,3,4]) returns [0,1,3,6,10].
sumfold([3]) returns the COPY [3]
Page 4
Last Name: First: Netid:
Account
Attribute Invariant Category
IN_USE list of all user names (str) Class attribute
_username a nonempty string of only letters and digits Immutable instance attribute
_realname a nonempty string Mutable instance attribute
_password a string with at least 8 characters of only let- Mutable instance attribute
ters and digits; it cannot be only letters or
only digits
We have not added headers for the getters and setters. You are to write these from scratch.
However, you are not expected to write specifications for them. For the other methods,
pay attention to the provided specifications. The only parameters are those in the preconditions.
You should enforce preconditions with assert unless you are given a specific error to use instead.
Type-based preconditions should all be managed with isinstance and not the function type.
As one last restriction, the class Adminstrator may not use any attribute or getter/setter
inherited from Account. It may only use super() to access overridden methods.
Page 5
Last Name: First: Netid:
Attribute IN_USE: A CLASS ATTRIBUTE list of all user names on the server.
This list starts off empty, as there are no accounts to start with."""
# IMMUTABLE ATTRIBUTES
# ATTRIBUTE _username: Unique account name. A nonempty string of letters and digits.
# MUTABLE ATTRIBUTES
# ATTRIBUTE _realname: Account holder's name. A nonempty string.
# ATTRIBUTE _password: The account password. A string of size >= 8.
# _password has only letters and digits but it cannot be only letters or only digits.
Page 6
Last Name: First: Netid:
The parameter name is used to set both the user name and the real name.
No account can share the same user name as another. On creation, the
user name is added to the class attribute IN_USE.
Example: returns 'wmw2' if both username and realname are 'wmw2', but
'wmw2 (Walker)' if username is 'wmw2' and real name is 'Walker'."""
An account object is equal to this one (self) if it has the same user name.
Objects that are not instances of Account cannot be equal to this one.
Page 7
Last Name: First: Netid:
Page 8
Last Name: First: Netid:
(a) [5 points] Draw the class folders in the heap for these two classes.
(b) [21 points] Assume that you have already created the following object, which is shown
in the diagram on the next page.
> > > p = B(3,-1)
On the next two pages, diagram the evolution of the call
> > > z = p.f(1)
Diagram the state of the entire call stack for the method call when it starts, for each line
executed, and when the frame is erased. If any other methods are called, you should do
this for them as well (at the appropriate time). This will require a total of nine diagrams,
excluding the initial (pre-call) diagram.
You should draw also the state of global space and the heap at each step. You are allowed
to write “unchanged” if no changes were made to either global or the heap.
Hint: Do not diagram the constructor call. There should be a call frame in your very first
diagram.
Page 9
Last Name: First: Netid:
Page 10
Last Name: First: Netid:
Page 11