2024 Fall Prelim2
2024 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
Throughout this exam, you may use any Python feature that you have learned about in class other
than generators.
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:
# Small data
if s == '':
return ''
elif len(s) == 1:
return s[0]+s[0]+s[0]
# Small data
if s == '':
return 0
elif len(s) == 1:
return 1
return left
Page 3
Last Name: First: Netid:
Cornellian
Attribute Invariant Category
NEXTID int > 0 Class attribute
_cuid int > 0 Immutable instance attribute
_name nonempty string Mutable instance attribute
Student
Attribute Invariant Category
_gpa float in 0.0 to 4.3 Mutable instance attribute
When enforcing type-based preconditions, you should use isinstance instead of type.
We have not added headers for any of the getters and setters. You are to write (and name) these
yourself. You are not expected to write specifications for the getters and setters. For
the other methods, pay attention to the provided specifications. The only parameters are those
indicated by the preconditions.
Important: Student is not allowed to access any hidden attributes of Cornellian. As an
additional restriction, Student may not access any getters and setters in Cornellian.
Page 4
Last Name: First: Netid:
Attribute NEXTID: A CLASS ATTRIBUTE that is an int > 0. Its initial value is 1."""
# Attribute _cuid: The Cornell id. An int > 0 (IMMUTABLE)
# Attribute _name: The full name of the person. A non-empty string (MUTABLE)
def getName(self):
"""
Returns the full name of the Cornellian
"""
return self._name
def setName(self,n):
"""
Sets full name of the Cornellian
# THE INTIALIZER
def __init__(self, n): # Fill in missing part
"""Initializes a Cornellian with name n.
Page 5
Last Name: First: Netid:
Two Cornellians are equal if they have the same Cornell ID EVEN
THOUGH their names may be different (people can change names).
def setGPA(self,value):
"""Sets student's GPA
Page 6
Last Name: First: Netid:
Examples: 'Bob Roberts [234781]' or "Emma Towns [492886], Dean's List" """
result = super().__str__()
if self.onDeansList():
result = result+", Dean's List"
return result
Page 7
Last Name: First: Netid:
4. [24 points total] Iteration. Implement the functions on the next two pages, according to their
specification, using loops (either for-loops or while loops). While we do not tell you which kind
of loop to use, we have given you rules to follow in class. Use those to guide your choice of loop.
For both of these questions, you do not need to enforce preconditions.
If a key appears in only one of dict1 or dict2, the value is the value
from that dictionary. If it is in both, the value is the sum of values.
Examples:
merge({'a':1,'b':2},{'b':3,'c':4}) returns {'a':1,'b':5,'c':4}
merge({'a':1,'b':2},{'c':3,'d':4}) returns {'a':1,'b':2,'c':3,'d':4}
merge({'a':1,'b':2},{}) returns {'a':1,'b':2}
merge({},{'b':3,'c':4}) returns {'b':3,'c':4}
Precond: dict1, dict2 are (possibly empty) dictionaries with int values"""
return result
Page 8
Last Name: First: Netid:
(b) [12 points] For the problem below you are only allowed to remove or delete elements from
nlist. You are not allowed to append or extend nlist (so you cannot clear the list
and then add the elements back one-by-one). However, you are allowed to create any
additional lists that you want and append to those. You may find a second list helpful for
keeping track of duplicates.
def remdups(nlist):
"""MODIFIES nlist to remove all duplicates.
Examples:
If a = [1,2,1,3,2,4], remdups(a) modifies a to [1,2,3,4]
If a = [1,2,1,2,1], remdups(a) modifies a to [1,2]
If a = [1,2,3,4], remdups(a) leaves a unchanged
If a = [], remdups(a) leaves a unchanged
Page 9
Last Name: First: Netid:
(a) [9 points] Draw the entire contents of the heap for these two class definitions. That means
you should draw the class folders, but also any object folders that might be associated with
the class definition.
(b) [15 points] On the next two pages, diagram the call
> > > x = B(5)
You will need eight diagrams. Draw the call stack, global space and heap space. If the
contents of any space are unchanged between diagrams, you may write unchanged. While
you will need to use the folders from part (a) in your answer, you do not need to draw
any of those folders again. Your heap should only contain the folders that are newly
creted by this part. However, make sure that any new folders do not share ids with folders
in part (a).
When diagramming a constructor, you should follow the rules from Assignment 5. Remem-
ber that __init__ is a helper to a constructor but it is not the same as the constructor.
In particular, there is an important first step before you create the call frame.
Page 10
Last Name: First: Netid:
1 id3
B
2 B.__init__ 20 id3
self id3 x 5 B
3 B.__init__ 20 id3
self id3 x 5 B
A.__init__ 6
self id3 x 6
Page 11
Last Name: First: Netid:
Page 12