Practice Exam – TA Version

Review each question carefully and select or provide your answers

Question 1 – Function Behavior

8 points
PYTHON

Review the code and select the correct explanation of what the function does.

1
def total_cost(items, price_dict):
2
cost = 0
3
for item in items:
4
cost += price_dict[item]
5
return cost
6
7
prices = {
8
"pen": 1.50,
9
"notebook": 3.00,
10
"folder": 2.25
11
}
12
13
order = total_cost(["pen", "folder", "folder"], prices)
14
print(order)
The function takes a dictionary of item prices and a list of item names. It multiplies the price of each item by the number of times it appears and returns the total cost.
The function takes a list of item names and a dictionary of prices. It adds the price of each item in the list to a running total and returns that total.
The function takes a list of item names and returns only the price of the last item in the list.
The function takes a dictionary and list but only returns the count of unique items.

Question 2 – Code Behavior

8 points
PYTHON

Review the code and select the correct explanation of what it does.

9
available_rentals = {
10
"building1": ["Aspen", "Oak", "Maple"],
11
"building2": ["Cedar", "Pine", "Fir"],
12
"building3": ["Willow", "Magnolia", "Cypress"]
13
}
14
15
building_selection = "building1"
16
print(f"These rooms are available in {building_selection}:")
17
for room in available_rentals[building_selection]:
18
print(room)
This code iterates through all of the items in the available_rentals dictionary and displays all of the keys.
This code iterates through all of the items in the available_rentals dictionary and displays all of the values.
The code accesses a specified building from the available_rentals dictionary and iterates through a dictionary of rooms available in that building to display only rooms matching a specified value.
The code accesses a specified building from the available_rentals dictionary and iterates through the list of rooms available in that building to display each room name as output

Question 3 – Dictionary Update

3 points
PYTHON

Type exactly what this prints:

1
extensions = {
2
"hr": "x2001",
3
"it": "x4455",
4
"sales": "x9900"
5
}
6
7
extensions["it"] = "x1234"
8
print(extensions["it"])

Your Output:

Question 4 – Nested Loop and Condition

7 points
PYTHON

Type exactly what this prints:

1
target = 80
2
scores = {
3
"john": [75, 80, 95],
4
"maya": [88, 72, 81],
5
"li": [90, 90, 90]
6
}
7
8
for student in scores:
9
for s in scores[student]:
10
if s >= target:
11
print(f"{s} - {student}")

Your Output:

Question 5 – Debug Code

15 points
PYTHON

When this runs, it shows a syntax error. Fix the syntax so that the output is:

name: laptop price: 999

Starter Code (contains errors):

Output will appear here...

Question 6 – Debug KeyError

15 points
PYTHON

Fix the function so it does not crash when the key is missing. The main program must stay the same.

Output will appear here...

Modify only get_grade() to safely handle missing keys.

Question 7 – Modify Code

15 points
PYTHON

Add a new query parameter "temp_unit": "fahrenheit" so the API returns temperature in Fahrenheit.

Output will appear here...

Modify only the function.

Question 8 – Modify Code + Print Message

15 points
PYTHON

Modify the function so that when an item is removed, it also prints:

Removed item: <name>
Output will appear here...

Question 9 – Two-Function API Workflow

25 points
PYTHON

The following program consists of two functions and function calls from the main program that work together to:

  1. Request weather data from an API
  2. Check if the API request was successful (status code 200)
  3. Extract and display the temperature from the returned JSON data
  4. Handle cases where the API request fails or the temperature key is missing

You will use all of the blocks in your program. Drag blocks to build the complete solution:

Available Blocks

import requests
def get_weather(city):
url = f"https://api.weatherapi.com/v1/current.json"
params = {"key": "demo", "q": city}
response = requests.get(url, params)
if response.status_code == 200:
return response.json()
else:
print(f"Error: Status code {response.status_code}")
return None
def show_temperature(data):
if data and "current" in data:
print(f"Temperature: {data['current']['temp_f']}°F")
else:
print("Temperature data not available")
if __name__ == "__main__":
weather = get_weather("London")
show_temperature(weather)

Your Solution (Drag blocks here)

Drag blocks from the left to build your solution

Question 10 – Dictionary Filtering

15 points
PYTHON

Once rearranged, the code should display the names of all students who scored "A" or "B". You will not use 2 of the blocks in your program.

Blocks to use:

Available Blocks

grades = {"Alice": "A", "Bob": "C", "Carol": "B", "Dan": "A", "Eve": "D"}
def show_top_students(grade_dict):
for student, grade in grade_dict.items():
for student in grade_dict.keys():
if grade in ["A", "B"]:
if grade == "A" or grade == "B":
print(student)
if __name__ == "__main__":
show_top_students(grades)

Your Solution (Drag blocks here)

Drag blocks from the left to build your solution