Exam 2 Review#

Course Announcements

  • CL6 due Fri

  • A4 due Sun

  • E2 is next week

    • sign up if you have not yet!

    • Practice Exam is available - take multiple times!

Day of Exam:#

What to Bring:

  • An ID

  • Your Brain

  • A writing utensil

Reminders:

  • You will put your belongings in a locker; leave time for this

  • You are not allowed to bring in water/coffee

  • They will provide scratch paper & a calculator

  • You’ll need to sign into your PL using your SSO

Location: TTC-CBTF - Applied Physics & Mathematics (AP&M) B349 (basement)

Exam Window: 5/18-5/22 (Mon-Fri); 45min

Exam 2 Plan#

Exam (16 Qs): (there were 25 on E1)

  • Q1 - blank jupyter notebook (no credit)

  • Q2-7 Multiple Choice (6)

  • Q8-13 “Short Answer” (line of code, matching, drop-down, check box) (6)

  • Q14-16 Code Reading & Debugging (3)

Topics Focused on (#Qs):

  1. Loops (6 | 1 MC; 4 SA; 1 Debug)

  2. Methods (2 | 1 MC; 1 SA)

  3. Classes (4 | 4 MC; 1 SA; 2 Debug)

Note: This is the expected layout. Small changes to this could be made.

Topics from E1 (can’t forget)#

  • Variables

  • Operators

  • Functions

  • Conditionals

E2 Topics#

Loops#

Types:

  • for and while

  • range, break, continue

Methods#

  • list, string, and dictionary methods (need to know .append() and .items(); others will be explained)

  • in place vs not in place

Classes#

  • class

  • class & instance attributes

  • methods

  • self

  • instance

  • accessing attributes

  • executing methods

Questions#

What questions do you have!?

Practice#

The practice questions below are NOT representative of the typical question on the exam, but are representative of the more difficult questions on the exam. Many students are feeling pretty good about MC & matching questions (and you’ve done lots of those on PL so far) and less confident when there’s less structure. So, I figured if we did these together…we’d get everyone feeling a bit more confident!

The form we’ll use for today (we’ll just keep using the same one….): https://forms.gle/NbSqNnTjRYtKRsUk9

Q1#

Q1. Fix the StudyTracker class below, so that it accomplishes the following:

  • by default sets hours_studied at 0, but allows the user to specify a different value when initializing an instance

  • has a method add_hours that increases the number of hours in the hours attribute by the input to the method

  • has a method study_feedback that returns a different string if you’ve studied less than 1 hour, less than 3 hours, or 3 hours or more. (Note: the strings do not need to be changed, but the conditional logic may)

#original
class StudyTracker:
    def __init__(self):
        hours = 0

    def add_hours(additional_hours):
        hours = additional_hours

    def study_feedback():
        if hours == 1:
            return "Getting started is the hardest part—keep going!"
        elif hours == 3:
            return "Nice! You're building solid study habits."
        else:
            return "Awesome dedication! You're setting yourself up for success!"
#fixed
# TEST IT OUT HERE

Concepts in Q1:

  • classes

  • debugging

Reminder: You can ask GenAI to:

  • create similar practice problems (and not give the answer)

  • check your work

  • explain the logic without giving you code

  • explain the concepts underlying a given question -> study those concepts

  • explain code

  • Give you a hint when you’re stuck

Q2#

Q2. Below is an attempt at implementing a CoffeeTracker class. Its instance attributes are all working as intended…but buy_coffee is not quite there. Fix the buy_coffee method so that when it takes in a list of prices, it will update the total_spent attribute to:

  • include the sum of all prices in prices

  • update total_coffees attribute to count how many drinks were in the prices list

  • If the number of total_coffees is more than zero, the average_price attribute will be updated, calculating the value in the total_spent attribute divided by the value in the total_coffees attribute. The print statements at the end do NOT need to be changed.

# original
class CoffeeTracker:
    def __init__(self, name):
        self.name = name
        self.total_spent = 0.0
        self.total_coffees = 0
        self.average_price = 0.0

    def buy_coffee(prices):
        total_spent = price
        total_coffees = 1
        
        if total_coffees > 0:
            average_price = total_spent / total_coffees
        
        print(f"{self.name} bought {self.total_coffees} coffees.")
        print(f"Average price per coffee: ${self.average_price:.2f}")
#debugged

Concepts in Q2:

  • Classes

  • Debugging

  • loops

  • conditionals

Q3#

Q3a. Assume a fictitious class Exam2 has been defined. It contains:

  • a class attribute course which stores the string ‘COGS 18’

  • two instance attributes storing the name of a student and the score that they earned

  • a single method calculate_percentage that calculates the student’s percentage out of 100 by dividng score by 15

How would you create an instance my_exam2 of this object? How would you access the name attribute? How would you execute the calculate_percentage method?

# YOUR ANSWER HERE

Q3b Time permitting: Try to write the code for the class described above. Note: You will NOT have to write a whole class from scratch on the exam.

## YOUR CODE HERE