Python Unit 8: Functions and Exceptions Practice Questions
Practice questions for Python Unit 8 on functions, parameters, return values, and exception handling, with explanations for every answer.
Questions
What does this program print? def greet(): print("Hello") print("Welcome") greet() greet()
- Hello Welcome Hello Welcome
- Hello Hello Welcome Welcome
- Hello Welcome
- Nothing is printed
What does this program print? def greet(): print("Hello") print("Start")
- Start
- Hello Start
- Start Hello
- Nothing is printed
The last line has no parentheses. What prints? def greet(): print("Hello") greet
- Nothing is printed
- NameError
- SyntaxError
- Hello
What happens when this runs? cheer() def cheer(): print("Go Danes")
- NameError
- Go Danes
- SyntaxError
- Nothing is printed
a() calls b(), which is defined below a. What prints? def a(): print("A") b() def b(): print("B") a()
- A B
- A NameError
- NameError
- B A
What does this program print? def show(): print("one") print("two") show()
- two one
- one two
- one
- two
What does this program print? print("x") def f(): print("y") f() print("z")
- x y z
- y x z
- x z y
- x z
Which program prints hi?
- def wave(): print("hi") wave()
- def wave() print("hi") wave()
- fn wave(): print("hi") wave()
- def wave: print("hi") wave()
What does this program print? def line(): print("-----") for i in range(3): line()
- ----- ----- -----
- ---------------
- -----
- None
What does this program print? def countdown(): n = 3 while n > 0: print(n) n = n - 1 countdown() print("Go")
- 3 2 1 Go
- 3 2 1 0 Go
- Go
- Go 3 2 1
A program draws a house: walls, roof, and door. Which design shows good decomposition?
- Three functions: draw_walls(), draw_roof(), draw_door()
- One loop that repeats the whole house three times
- A separate function for every single print line
- One long block of print statements with no functions
You call send_message("hi") in a chat app without knowing how the message travels over the network. Which idea lets you do that?
- Abstraction
- Selection
- Iteration
- Decomposition
What does this program print? def f(): print("in f") f() f = 5 print(f)
- in f 5
- in f in f
- TypeError
- 5
What does this program print? def menu(): print("1. Play") print("2. Quit") choice = "2" if choice == "1": menu() print("Bye")
- Bye
- Nothing is printed
- 1. Play 2. Quit
- 1. Play 2. Quit Bye
What does this program print? def beep(): print("beep") def alarm(): beep() beep() alarm() beep()
- beep beep beep
- beep
- beep beep
- beep beep beep beep
What happens when this runs? def hello(): print("hi")
- IndentationError
- hi
- SyntaxError
- NameError
What does this program print? def hello(): print("hi") hello() hello
- hi
- hi hi
- NameError
- Nothing is printed
Why put a block of code you use in several places into a function?
- Reuse it by name; fix it in one place
- Functions make every program run faster
- Python requires all code to be in functions
- Functions stop runtime errors from happening
A function must be defined before the line that calls it runs.
Which def lines are valid Python?
- def f():
- def f(a, b=2):
- def f(a=1, b):
- def f[]:
What does this program print? def square_of(n): print(n * n) square_of(4) square_of(-3)
- 16 9
- 16
- 8 -6
- 16 -9
The arguments are given in the wrong order. What prints? def intro(name, grade): print(f"{name} is in grade {grade}") intro(10, "Mia")
- 10 is in grade Mia
- TypeError
- Mia is in grade 10
- NameError
The function joins text with +. What happens? def intro(name, grade): print(name + " is in grade " + grade) intro("Mia", 10)
- TypeError
- Nothing is printed
- 10 is in grade Mia
- Mia is in grade 10
What happens when this runs? def intro(name, grade): print(f"{name} {grade}") intro("Mia")
- TypeError
- Mia
- NameError
- Mia None
What happens when this runs? def intro(name, grade): print(f"{name} {grade}") intro("Mia", 10, 11)
- TypeError
- SyntaxError
- Mia 10 11
- Mia 10
What does this program print? def power(base, exp=2): print(base ** exp) power(3) power(2, 5)
- 9 32
- TypeError
- 6 10
- 9 10
What does this program print? def tip(bill, rate=0.2): print(round(bill * rate, 2)) tip(50) tip(50, 0.15)
- 10.0 7.5
- 10.0 10.0
- 10 7.5
- 0.2 0.15
Which def line is valid Python?
- def order(item, qty=1):
- def order(item, qty==1):
- def order(qty=1, item):
- def order(item; qty=1):
What does this program print? def repeat(word, times): for i in range(times): print(word) repeat("go", 2) repeat("stop", 0)
- go go
- go go stop
- go
- go go stop stop
What does this program print? def area(w, h): print(w * h) x = 3 y = 4 area(y, x)
- 12
- 43
- 34
- 7
What does this program print? def diff(a, b): print(a - b) diff(10, 4) diff(4, 10)
- 6 -6
- 4 10
- 6 6
- -6 6
What does this program print? def label(score, cutoff=70): if score >= cutoff: print("pass") else: print("retry") label(65) label(65, 60)
- retry pass
- pass pass
- pass retry
- retry retry
What does this program print? def show(x): x = x + 1 print(x) n = 5 show(n) print(n)
- 6 5
- UnboundLocalError
- 6 6
- 5 5
What does this program print? def greet(name="friend"): print(f"Hi, {name}") greet() greet("Ana")
- Hi, friend Hi, Ana
- Hi, friend Hi, friend
- TypeError
- Hi, Ana Hi, Ana
A function draws a square of # characters. What should be its parameter?
- The side length
- The # character only
- Nothing; always draw size 5
- The number of times the program runs
What does this program print? def stars(n): line = "" for i in range(n): line = line + "*" print(line) for k in range(1, 4): stars(k)
- * ** ***
- *** ** *
- ***
- * * *
What does this program print? def countdown(start): while start > 0: print(start) start = start - 2 countdown(5)
- 5 3 1
- 5 3
- 5 3 1 -1
- 4 2
In the call add(2, 3) for def add(a, b):, what are 2 and 3? def add(a, b): print(a + b) add(2, 3)
- Arguments
- Parameters
- Return values
- Global variables
What does this program print? def show(a, b=1, c=2): print(a, b, c) show(5, 6)
- 5 6 2
- 5 1 2
- TypeError
- 5 6 1
What does this program print? def convert(c): print(round(1.8 * c + 32, 1)) convert(-40) convert(37)
- -40.0 98.6
- -40 98.6
- -40.0 98.60000000000001
- 8.0 98.6
What does this program print? def check(age): if age >= 18: print("adult") print("checked") check(20) check(12)
- adult checked checked
- adult checked
- adult checked adult checked
- checked checked
The user types 4. What prints? def double_it(n): print(n * 2) val = input("n: ") double_it(val)
- n: 4 44
- n: 4 TypeError
- 8
- n: 4 8
Using def book(title, pages, author="unknown"):, which call gives title Dune, 300 pages, and the default author?
- book("Dune", 300)
- book(300, "Dune")
- book("Dune")
- book(title, 300)
What does this program print? def ratio(a, b=2): print(a / b) ratio(9) ratio(9, 3)
- 4.5 3.0
- 4.5 3
- 4 3
- 4.5 4.5
Parameters with default values must come after parameters without defaults.
What does this program print? def f(a, b=3): return a * b print(f(4))
What does this program print? def area(w, h=2): return w * h print(area(3) + area(3, 3))
What happens when this runs? def setup(): level = 3 setup() print(level)
- NameError
- None
- level
- 3
What does this program print? level = 1 def setup(): level = 3 setup() print(level)
- 1
- NameError
- UnboundLocalError
- 3
What does this program print? level = 1 def show(): print(level) show()
- 1
- UnboundLocalError
- None
- NameError