ProgrammingVery hardProduct company OA
What is printed?
What is printed?
def f(x):
try:
r = 10 / x
except ZeroDivisionError:
r = "inf"
return r
else:
r = round(r)
finally:
r = str(r) + "!"
return r
print(f(0), f(4))- A. inf! 2!
- B. inf 3!
- C. inf! 3!
- D. inf 2!
Correct answer: D. inf 2!
For f(0) the return expression r is evaluated ("inf") before finally runs; finally rebinds the name r but cannot change the value already chosen for return, so "inf" is returned. For f(4), r = 2.5, round uses banker's rounding (ties to even) giving 2, finally makes it "2!" and the final return sends that back.
More Python Programming questions
- What is printed? python class A: def eq(self, o): print("A", end=" ") return NotImplemented class B: def eq(self, o): print("B", end=" ")…
- What is printed? python class N: def add(self, o): return "N.add" def radd(self, o): return "N.radd" class S(N): def radd(self, o): return…
- What is printed? python class C: k = 5 def f(self, v=k): return v def g(self): try: return k except NameError: return "NE" C.k = 9…
- What is printed? python d = {"a": 1} print(d.get("b", 0), "a" in d, 1 in d, d.get("b"))
- What is printed? python print(list("ab") + ["c"], tuple([1, 2]) 2)
- What is printed? python d = {"a": 1} try: d["k"] except KeyError as e: print(repr(e), e.args, len(d))