Chapter 0 · Section 4 practice

Functions, Formatting and Error Handling

0 of 6 answered

These questions review the examples in the companion slides. Choose an answer, then check the explanation.

Question 1

def show(x): print(x) then r = show(5). What is r?

  • 5
  • None
  • "5"
  • A TypeError

Question 2

def add_city(c, cities=[]): cities.append(c); return cities. After add_city(“NYC”), what does add_city(“LA”) return?

  • ["LA"]
  • []
  • ["NYC", "LA"]
  • A TypeError

Question 3

demo(a, b=10, *args, scale=1.0, **kwargs) returns (a + b + sum(args)) * scale. What is demo(1, 2, 3, 4, scale=0.5)[“total”]?

  • 10.0
  • 5.0
  • 11.0
  • 2.5

Question 4

x = “global”; def demo(): x = “local”; return x. What do print(demo()) and print(x) show?

  • local then global
  • local then local
  • global then global
  • An UnboundLocalError

Question 5

list(zip([“Ana”, “Bo”, “Cy”], [88, 92])) returns?

  • [("Ana", 88), ("Bo", 92), ("Cy", None)]
  • [("Ana", 88), ("Bo", 92)]
  • A ValueError
  • [("Ana", "Bo", "Cy"), (88, 92)]

Question 6

Which exception does int(“abc”) raise?

  • KeyError
  • TypeError
  • IndexError
  • ValueError