๐ U of Pennsylvania
Additional practice exam inspired by the university of Pennsylvania Intro to Python
Inspired by the The University of Pennsylvaniaโs Intro to Python practice exam, I added the questions relevant to our course :)
Question 1 - List Math ๐งฎ
What will print?
x = [1, 2, 3]
x[len(x) - 1] = x[len(x) - x[0]] - x[1]
print(x[2])๐ Hint
Try drawing out what happens @ each index
โ Solution
1
Question 2 - Lists & Loops ๐
What will print?
numbers = [4, 2, -1]
x = 1
for num in numbers:
x = x * numbers
print(x)๐ Hint
Notice any syntax problems?
โ Solution
nothing shall print cause thereโs a problem! U canโt multiple a number by a list
Question 3 - Tracing Functions ๐
Hereโs a python file that features a few cool functions.
def func_three(x, y):
print(f"func_three arguments: x={x}, y={y}")
result1 = x * 3
result2 = y * 2
final_result = result1 + result2
print(f"func_three returning: {final_result}")
return final_result
def func_two(num):
print("func_two argument: num=" + str(num))
result = num * 2
print("funct_two returning: " + str(result))
return result
def func_one(a, b):
print(f"func_one arguments: a={a}, b={b}")
result1 = a + b
result2 = func_two(result1)
result3 = func_three(a, b)
final_result = result2 - result3
print(f"func_one returning: {final_result}")
return final_result
print("Starting main program")
x = 10
y = 5
z = func_one(y, x)
print("The final result is: " + str(z))When the program is run, eight lines are printed. For each of the following lines, fill in the blanks to show what values the variables have when they are printed out. Also, mark the order in which they are printed in the โOrderโ column starting at 1. The order for the first line is marked for you.
| Printed Line | Order |
|---|---|
| Starting main program | 1 |
| The final result is: ____ | |
func_one arguments: a=____ , b=____ |
|
func_one returning: ____ |
|
func_two argument: num=____ |
|
func_two returning: ____ |
|
func_three argument: x=____ , y=____ |
|
func_three returning: ____ |
๐ Hint
Add arrows to link from where the function is being called to the function definition. This will make it easier to follow the order of execution :^)
โ Solution
Starting main program
func_one arguments: a=5, b=10
func_two argument: num=15
funct_two returning: 30
func_three arguments: x=5, y=10
func_three returning: 35
func_one returning: -5
The final result is: -5
Question 4 - Time is Ticking โฐ
The following program is supposed to print out a time readout every fifteen minutes over the course of one day, starting with 12:00 AM, then 12:15 AM, then 12:30 AM and so on. Times in the second half of the day should be printed using the 12-hour clock, meaning that the full set of printed times should include 96 timestamps and should match the format & pattern shown in the following example:
12:00 AM
12:15 AM
12:30 AM
12:45 AM
1:00 AM
... // ellipses are not printed literally lol
11:30 AM
11:45 AM
12:00 PM
12:15 PM
...
11:45 AM
Help write this clock program by filling in the blanks
for hour in range(24):
for minute in range(0, 60, _BLANK_0_):
if _BLANK_2_ or hour == 12:
hour_to_print = "12"
else:
hour_to_print = str(hour % _BLANK_3_)
if minute == 0:
minute_to_print = ":00"
else:
minute_to_print = ":" + str(minute)
if _BLANK_4_:
suffix = "AM"
else:
suffix = "PM"
print(hour_to_print + minute_to_print + " " + suffix)๐ Hint
You can control how a loop increments by using a step. For example, range(0, 60) starts at 0, goes up to but does not include 60, and increases by 1 each time, while range(0, 60, 30) starts at 0, goes up to but does not include 60, and increases by 30 each time. If time is going up by 15 minutes, whats our โstepโ?
โ Solution
for hour in range(24):
for minute in range(0, 60, 15):
if hour % 12 == 0 or hour == 12:
hour_to_print = "12"
else:
hour_to_print = str(hour % 12)
if minute == 0:
minute_to_print = ":00"
else:
minute_to_print = ":" + str(minute)
if hour < 12:
suffix = "AM"
else:
suffix = "PM"
print(hour_to_print + minute_to_print + " " + suffix)