Old version
This is the CS 111 site as it appeared on May 10, 2018.
Final Exam Practice Problems
As we get closer to the exam, solutions will be posted under Other Content on Blackboard.
-
What does the following code print?
a = [1, 2, 3, 4] b = a b[2] = 6 print('a =', a, 'b =', b)
-
Using a memory diagram and a couple of sentences, explain the result printed in problem number 1.
-
What is printed when you invoke
prob3()
below?def eat(x): x[1] = 9 x[3] = 11 def prob3(): food = [4, 5, 6, 7] eat(food) print('food =', food)
-
Using a memory diagram and a couple of sentences, explain the result printed in problem number 3.
-
Write a function
create_2d
that takes as input two integersheight
andwidth
, and that creates and returns a 2D list (i.e., a list of lists) with values that are the row number multiplied by the column number. For example:>>> create_2d(3, 5) [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
-
Write a function
add_one
that takes an inputgrid
that is a 2D list (a list of lists). Your function should add 1 to each element ofgrid
, but it should not return anything. For example:>>> my_grid = create_2d(3, 5) >>> add_one(my_grid) >>> my_grid [[1, 1, 1, 1, 1], [1, 2, 3, 4, 5], [1, 3, 5, 7, 9]]
-
Create a Python class named
Phonebook
with a single attribute calledentries
. Begin by including a constructor that initializesentries
to be an empty dictionary. -
Next add a method called
add_entry
that takes a string representing a person’s name and an integer representing the person’s phone number and that adds an entry to thePhonebook
object’s dictionary in which the key is the name and the value is the number. For example:>>> book = Phonebook() >>> book.add_entry('Turing', 6173538919)
-
Add a method named
contains
to yourPhonebook
class. It should take a parametername
and returnTrue
ifname
is present in the phonebook, andFalse
otherwise. For example:>>> book = Phonebook() >>> book.contains('Turing') False >>> book.add_entry('Turing', 6173538919) >>> book.contains('Turing') True
-
Write another method for your
Phonebook
class callednumber_for
that takes a parametername
and returns the phone number forname
in the calledPhonebook
object. It should return-1
ifname
is not found. Here is an example:>>> book = Phonebook() >>> book.add_entry('Turing', 6173538919) >>> book.add_entry('Hopper', 6174951000) >>> book.number_for('Turing') 6173538919 >>> book.number_for('Hopper') 6174951000 >>> book.number_for('Codd') -1
Hint: Consider using your
contains
method from problem 9! -
Create a Python class called
Triangle
. The constructor for this class should take two arguments,base
andheight
, and store those values in appropriately named attributes. In addition, you should add a method calledarea
that computes and returns the area of the triangle. (The area of a triangle is 0.5 times the product of its base and height.) For example:>>> tri = Triangle(3, 4) >>> tri.area() 6.0
-
Add a method to your
Triangle
class that enables you print aTriangle
object in a readable way. For example:>>> tri = Triangle(3, 4) >>> print(tri) triangle with base 3 and height 4
-
Add a method to your
Triangle
class that will allow you to use the==
operator to test if twoTriangle
objects are equal–i.e., if they have the same base and height. For example:>>> tri1 = Triangle(3, 4) >>> tri2 = Triangle(3, 4) >>> tri3 = Triangle(4, 3) >>> tri1 == tri2 True >>> tri1 == tri3 False
-
Write a function called
main
that creates three triangle objectstri1
(with base 3 and height 4),tri2
(with base 6 and height 6), andtri3
(also with base 3 and height 4). The function should print the three objects and their areas. Next, it should test whethertri1
andtri2
are equal and report the result. Finally, it should test whethertri1
andtri3
are equal and report the result. Your function should take full advantage of theTriangle
methods you have written. Here is the desired output:>>> main() tri1: triangle with base 3 and height 4 (area = 6.0) tri2: triangle with base 6 and height 6 (area = 18.0) tri3: triangle with base 3 and height 4 (area = 6.0) tri1 and tri2 are not equal tri1 and tri3 are equal
-
Write a subclass of
Triangle
calledEquilateralTriangle
. Its constructor should take a single parameterside
representing the length of a side. However, the new class should not have any new attributes. Rather, it should use the attributes that are inherited fromTriangle
, and you should initialize those attributes by calling the superclass constructor and passing it the appropriate values. (You should approximate the height of the triangle as 0.866 times the side length.) For example:>>> tri1 = EquilateralTriangle(6) >>> print(tri1) triangle with base 6 and height 5.196
-
Override the appropriate method in
EquilateralTriangle
so that printing anEquilateralTriangle
object produces an output that looks like the following:>>> tri1 = EquilateralTriangle(6) >>> print(tri1) equilateral triangle with side 6
-
Construct a deterministic finite state machine that accepts all bit strings that contains three ones in a row – i.e., all bit strings that include the pattern
111
somewhere in the input – and that rejects all other bit strings.