# CS21: Final Exam Study Guide

## 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:

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
• Write and test a class
• Trace a program, showing output and the stack diagram
• Recursion and iteration
• Analysis of algorithms
• Searching and sorting

## 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
• an object's attributes (or data members)
• Properties of object-oriented programming, including:
• encapsulation
• abstraction
• Defining classes, including:
• the constructor, or __init__ method
• the __str__() method
• access methods (aka getters, accessors)
• update methods (aka setters, mutators)
• the self variable

### Things you are not responsible for:

While you will need to understand how objects work, you do not need to memorize the methods and objects for the Zelle graphics library.

## Practice problems:

1. 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
Enter guess: pony
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!
1. 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
• credits, an integer representing the number of course-credits the student has earned
• courseLoad, an integer representing the current number of credits in progress

Write the following methods for your Student class:

• A constructor that, given an id and a name, creates a Student object with those values, as well as 0 course-credits and 0 course load.
stu1 = Student(25,"Knerr")
• A registerCurrent() method that sets the current course load. The course load must be positive and not greater than 4 credits, otherwise no change happens
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 combined with their ID and the post-fix @swarthmore.edu. For example, a student with last name "Knerr", and ID 25 should return "knerr25@swarthmore.edu"
email = stu1.createEmail()
print(email)
• A __str__() or __repr__() method (whichever one you learned) that returns a string representation of the object. E.g., "ID: 25\nName: Knerr\nEarned credits: 17\nCourse load: 2"
print(stu1)

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

1. Write recursive and iterative versions of a function that, given a string and a character, returns a new string with the character before and after each letter in the string. For example, calling the function with "hello" and "*" would return "*h*e*l*l*o*":
>>> insertChar("hello","*")
'*h*e*l*l*o*'
>>> insertChar("a",'+')
'+a+'
>>> insertChar("","-")
''
1. Assume we have a word-count list, such as this:
wc = [['around', 99], ['beer', 300], ['bottle', 3], ['bottles', 297], ['buy', 1], ['down', 99]]

Write a function to take a word-count list and return the word that has the highest count. For example, if called on the list above, the function should return "beer".