CS21 Final Exam Study Guide

This study guide is provided as a courtesy. We will not provide solutions to the questions in the study guide. You may work with other students on the questions and ask questions about the guide during office hours or review sessions.

The final exam is cumulative

In addition to the concepts below, you should know the concepts that were tested on all the quizzes in the course. There are many practice problems in these study guides that you can work on:

Historically, the final exam includes questions about each of the big ideas and skills in the course, such as:

  • Python expressions and types

  • Write a complete program with multiple functions

  • Trace a program, showing output and the stack diagram

  • Analysis of algorithms

  • Searching and sorting

  • Recursion and iteration

  • Write and test a class

Recent Topics and Practice Problems

Below are some of the new topics covered since the last quiz, as well as some additional practice problems.

You should be able to define and explain the following terms:

  • Object-oriented programming

    • methods

    • member variables/instance variables (two ways to refer to the same concept)

  • Defining classes, including:

    • the constructor, or __init__ method

    • the self variable

    • the __str__ method

    • getters

    • setters (sometimes also called mutators)

  • Using classes, including:

    • creating an object using the constructor

    • calling methods using dot notation (e.g. lst.append)

  • Creating and using list of lists

    • double indexing

    • looping over rows/columns

Practice problems:

  1. Consider the code snippet below, which contains two for loops.

    • What will each for loop print?

    • What is the difference between the two for loops?

      L = [3, 18, 19, -4, 11, 6.2]
      
      # first loop
      for x in L:
          print(x)
      
      # second loop
      for y in range(len(L)):
          print(y)
  2. Write a complete program that plays a guessing game with numbers. The goal is for the user to guess a randomly selected number between 1 and 100 as quickly as possible. The program will inform the user whether the hidden number is higher or lower than the current guess. You must break the problem up into a main() function and at least one additional function.

    The following is a sample run of the program:

    I'm thinking of a number between 1 and 100.
    
    Enter guess: 50
    Number is higher than 50
    Invalid, try again: 588
    Invalid, try again: 75
    Number is lower than 75
    Enter guess: 62
    Number is lower than 62
    Enter guess: 56
    Correct!
    You guessed it in 4 tries!
  3. Write a Student class that stores information for a Swarthmore student. The class should include the following attributes:

    • ID, an integer identifier for the student

    • lastName, a string for the student’s last name

    • credit, an integer representing the number of course-credits the student has earned

    • courseLoad, an integer representing the current number of credits in progress

      Include the following methods in your Student class:

    • A constructor that, given an ID and a name, creates a Student object with those values, as well as (initially) 0 course-credits and 0 course load. Here’s an example of calling the constructor: stu1 = Student(25,"Danner")

    • A registerCurrent(load) method that sets the current course load. The course load must be positive and not greater than 4 credits, otherwise no change happens. Here’s an example: stu1.registerCurrent(3)

    • A withdraw() method which decreases the course load by one credit (but courseLoad should never go below 0): stu1.withdraw()

    • A passedCourse() method which removes one course from the current course load and adds to the student’s overall course-credits: stu1.passedCourse()

    • A createEmail() method, which returns a string. The string should be an email address of the student’s name in lowercase combined with their ID and the post-fix @swarthmore.edu. For example, a student with last name "Danner", and ID 25 should return "danner25@swarthmore.edu"

    • A __str__ method that returns a string representation of the object. E.g., "ID: 25\nName: Newhall\nEarned credits: 17\nCourse load: 2"

    • Finally, write a main() function that tests the class. Each method should be called/tested at least once.

  4. Write recursive and iterative versions of a count(item, L, pos) function that, given an item and a list, returns how many times item appears in the list L, starting from position pos. If pos is less than zero or greater than the length of the list, the function should return 0.

    For example, calling the function with "R" and list("SWARTHMORE") would return 2 when starting from position 0.

    Here are some additional examples that should work:

    • count("hello",["we","love","cs"], 0) should return 0

    • count(99,[99,99,99,99,99], 1) should return 4

    • count(37,[], 1) should return 0

  5. Consider a list of lists grid of integers representing a 2D grid. Write a function sumGrid(grid) that returns the sum of all the integers in the grid. For example, calling the function with the following grid:

    [[1,  2,  3],
    [4,  5,  6]
    [7,  8,  9]]

    should return 45.

    • Write a function that finds the maximum column sum. In the example above, the function should return 18, which is the sum of the last column [3, 6, 9]. Your function should work for any size grid.