Week 12 Quiz
Predict, then check: this program saves the reading log and reads it back. What does it print?
daily = [12, 40, 25] with open("reading_log.txt", "w") as f: for pages in daily: f.write(str(pages) + "\n") total = 0 with open("reading_log.txt") as f: for line in f: total = total + int(line.strip()) print(total)Select one.
- 77
- 124025
- TypeError
- 0
Predict, then check: what does this program print, or which exception type ends it?
daily = [12, 40, 25] with open("reading_log.txt", "w") as f: for pages in daily: f.write(str(pages) + "\n") total = 0 with open("reading_log.txt") as f: for line in f: total = total + line print(total)Select one.
- 77
- ValueError
- TypeError
- NameError
Predict, then check: what does the last line of this program print?
with open("tasks.txt", "w") as f: f.write("email\n") f.write("lunch\n") with open("tasks.txt", "w") as f: f.write("gym\n") with open("tasks.txt") as f: print(f.read().split())Select one.
- ['email', 'lunch', 'gym']
- ['email', 'lunch']
- []
- ['gym']
Predict, then check: what does the last line of this program print?
daily = [12, 40, 25] with open("reading_log.txt", "w") as f: for pages in daily: f.write(str(pages) + "\n") with open("reading_log.txt") as f: lines = f.readlines() print(len(lines[0]))Select one.
- 2
- 3
- 1
- 12
Select every line this program prints.
try: n = int("42") print("ok", n + 1) except ValueError: print("not a number") try: n = int("forty-two") print("ok", n + 1) except ValueError: print("not a number") try: with open("no_such_file.txt") as f: print(f.read()) except FileNotFoundError: print("no file yet")Select every option that applies.
- ok 43
- not a number
- no file yet
- ok 42
- FileNotFoundError
Predict, then check: what is the second line this program prints?
try: print("before") print(10 / 0) print("after") except ZeroDivisionError: print("cannot divide by zero") print("still running")Select one.
- after
- still running
- ZeroDivisionError
- cannot divide by zero
Predict, then check: what does this program print, or which exception type ends it?
try: print(int("3.5")) except ZeroDivisionError: print("caught it")Select one.
- ValueError
- caught it
- 3
- ZeroDivisionError
Read the last line of the traceback: which exception type ends this program?
print(int("3.5"))Select one.
- TypeError
- ValueError
- NameError
- SyntaxError
True or false: the program pages = 0, then print("before"), then print(10 / pages), then print("after") prints before and then stops with ZeroDivisionError, so the line that would print after never runs.
True or false.
- True
- False
Predict, then check: what does this program print, or which exception type ends it?
daily = [12, 40, 25] try: total = 0 for pages in daily: total = total + pages print(totl) except: print("could not add the log")Select one.
- 77
- Nothing prints
- could not add the log
- NameError
Built with The Course Maker · samples.thecoursemaker.com