Python Exceptions: Practice Questions
For each snippet, write the output, the exception type, or “Runs Forever.” If it prints nothing, write “No Output”.
Question 1
xs = []
total = 0
while xs:
total += 1
print('total=', total)Answer
total= 0
Question 2
numbers = [4, 8]
print(numbers[-3])Answer
IndexError
Question 3
n = 1
while n != 4:
n += 2
print(n)Answer
runs forever
Question 4
grades={
'alice':{'hw1':99,'hw2':88},
'bob':{'hw1':82,'hw2':91},
}
for k,v in sorted(grades.items()):
print(v['hw1'])Answer
99
82
Question 5
xs = [2, 4]
assert len(xs) > 0
print(xs[0] + xs[1])Answer
6
Question 6
xs = []
while xs:
total += 1
assert(xs)
print('total=', total)Answer
NameError
Question 7
total = 0
print(8 // total)Answer
ZeroDivisionError
Question 8
xs = [0, 1, 2]
xs.replace(1, 2)
print("xs=", xs)Answer
AttributeError
Question 9
grades={
'alice':{'hw1':99,'hw2':88},
'bob':{'hw1':82,'hw2':91},
}
for k,v in sorted(grades.items()):
print(k['hw1'])Answer
TypeError
Question 10
grades={
'alice':{'hw1':99,'hw2':88},
'bob':{'hw1':82,'hw2':91},
}
for k,v in sorted(grades.items()):
print(v[0])Answer
KeyError
Question 11
grades={
'alice':{'hw1':99,'hw2':88},
'bob':{'hw1':82,'hw2':91},
}
output = "grade=" + grades['alice']['hw1']
print('output=', output)Answer
TypeError
Question 12
grades={
'alice':{'hw1':99,'hw2':88},
'bob':{'hw1':82,'hw2':91},
}
output = "grade=" + grades['charlie']['hw1']
print('output=', output)Answer
KeyError
Question 13
grades={
'alice':{'hw1':99,'hw2':88},
'bob':{'hw1':82,'hw2':91},
}
output = alice['hw1']
print('output=', output)Answer
NameError
Question 14
xs = [1, 2, 3]
while xs:
xs.append('test')
assert('t' in xs)
print('len(xs)=',len(xs))Answer
AssertionError
Question 15
def foo(xs):
assert(len(xs) > 0)
foo()Answer
TypeError
Question 16
print(int('2.3'))Answer
ValueError
Question 17
xs = [1, 2, 3]
try:
result = xs[3]
except IndexError:
result = -1
print('result=', result)Answer
result= -1
Question 18
grades={
'alice':{'hw1':99,'hw2':88},
'bob':{'hw1':82,'hw2':91},
}
try:
output = "grade=" + grades['charlie']['hw1']
except KeyError:
output = 'oops'
print('output=', output)Answer
output= oops
Question 19
def foo(xs):
assert(len(xs) > 0)
try:
foo([1,2,3])
except AssertionError:
passAnswer
No Output
Question 20
result = 10
try:
result += int('3')
except ValueError:
result = 0
print(result)Answer
13
Question 21
xs = [1, 2, 3]
try:
result = xs[3]
except NameError:
result = -1
print('result=', result)Answer
IndexError
Question 22
try:
number = int('two')
except TypeError:
print('wrong type')
except ValueError:
print('wrong value')Answer
wrong value
Question 23
try:
with open('missing-practice-scores.txt', encoding='utf-8') as f:
print(f.read())
except FileNotFoundError:
print('no file')
print('done')Answer
no file
done
Question 24
def foo(xs):
assert(len(xs) > 0)
result = 0
try:
result += foo([1,2,3])
except AssertionError:
result -= 1
print('result=', result)Answer
TypeError
Question 25
total = 0
try:
for text in ['2', 'bad', '5']:
total += int(text)
except ValueError:
print('invalid')
print(total)Answer
invalid
2
Question 26
total = 0
for text in ['2', 'bad', '5']:
try:
total += int(text)
except ValueError:
print('invalid')
print(total)Answer
invalid
7
Question 27
total = 0
errors = 0
for value in ['4', None, 'x']:
try:
total += int(value)
except (ValueError, TypeError):
errors += 1
print(total, errors)Answer
4 2
Question 28
for text in ['bad', '6', '8']:
try:
number = int(text)
except ValueError:
continue
print(number)
breakAnswer
6
Question 29
def double(n):
if n < 0:
raise ValueError('negative input')
return n * 2
try:
print(double(-1))
except ValueError as error:
print(error)Answer
negative input
Question 30
def bar(xs):
assert(len(xs) > 0)
return len(xs)*2
result = 0
try:
result += bar([1,2,3])
result += bar([2,3])
result += bar([])
result += bar([5])
except AssertionError:
pass
print('result=', result)Answer
result= 10
Question 31
def positive(n):
if n < 0:
raise ValueError('negative')
return n
total = 0
for n in [2, -1, 4]:
try:
total += positive(n)
except ValueError:
pass
print(total)Answer
6
Question 32
def bar(xs):
assert(len(xs) > 0)
return len(xs)*2
result = 0
try:
result += bar([1,2,3])
result += bar([2,3])
result += bar()
result += bar([5])
except AssertionError:
result += 1
except TypeError:
result += 5
print('result=', result)Answer
result= 15