CS 111
Summer 2 2018

Final Exam Info

Final Exam Information

Material covered

The exam will cover the entire semester, with the exception of Picobot. It will only include topics that we covered in class and/or on the assignments. Topics from the readings that we did not cover in class will not appear on the exam.

Exam details

Preparing for the exam

Practice problems

Answers will be posted as we get closer to the exam.

  1. What does the following code print?

    a = [1, 2, 3, 4]
    b = a
    b[2] = 6
    print('a =', a, 'b =', b)
    
  2. Using a memory diagram and a couple of sentences, explain the result printed in problem number 1.

  3. 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)
    
  4. Using a memory diagram and a couple of sentences, explain the result printed in problem number 3.

  5. Write a function create_2d that takes as input two integers height and width, 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]]
    
  6. Write a function add_one that takes an input grid that is a 2D list (a list of lists). Your function should add 1 to each element of grid, 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]]
    
  7. Create a Python class named Phonebook with a single attribute called entries. The constructor should initialize entries to be a dictionary containing the following names and phone numbers:
    Bob 72345, Sally 71000, John 79999. Use the names as the keys of the dictionary and the phone numbers (which you should represent as ints) as the values.

  8. Add a method named contains to your Phonebook class. It should take a parameter name and return True if name is present in the phonebook, and False otherwise. For example:

    >>> book = Phonebook()
    >>> book.contains('Foo')
    False
    >>> book.contains('Bob')
    True
    
  9. Write another method for your Phonebook class called number_for that takes a parameter name and returns the phone number for name in the called Phonebook object. It should return -1 if name is not found. Here is an example:

    >>> book = Phonebook()
    >>> book.number_for('Sally')
    71000
    >>> book.number_for('foobar')
    -1
    

    Hint: Consider using your contains method from problem 8.

  10. Write another method for your Phonebook class called add_entry that takes as parameters a name and a number and adds an appropriate entry to the phonebook. For example:

    >>> book = Phonebook()
    >>> book.number_for('Turing')
    -1
    >>> book.add_entry('Turing', 77777)
    >>> book.number_for('Turing')
    77777
    
  11. Create a Python class called Triangle. The constructor for this class should take two arguments, base and height, and store those values in appropriately named attributes. In addition, you should add a method called area 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
    
  12. Add a method to your Triangle class that enables you print a Triangle object in a readable way. For example:

    >>> tri = Triangle(3, 4)
    >>> print(tri)
    triangle with base 3 and height 4
    
  13. Add a method to your Triangle class that will allow you to use the == operator to test if two Triangle 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
    
  14. Write a function called main that creates three triangle objects tri1 (with base 3 and height 4), tri2 (with base 6 and height 6), and tri3 (also with base 3 and height 4). The function should print the three objects and their areas. Next, it should test whether tri1 and tri2 are equal and report the result. Finally, it should test whether tri1 and tri3 are equal and report the result. Your function should take full advantage of the Triangle 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
    
  15. Write a subclass of Triangle called EquilateralTriangle. Its constructor should take a single parameter side 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 from Triangle, 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
    
  16. Override the appropriate method in EquilateralTriangle so that printing an EquilateralTriangle object produces an output that looks like the following:

    >>> tri1 = EquilateralTriangle(6)
    >>> print(tri1)
    equilateral triangle with side 6