ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
def f(x):
try:
return int(x)
except ValueError:
return float(x)
except TypeError:
return -1
print(f("3"), f("3.5"), f(None), f(2.9))- A. 3 3.5 -1 2.9
- B. 3 ValueError -1 2
- C. 3 3.5 -1 2
- D. 3 3 -1 2
Correct answer: C. 3 3.5 -1 2
int('3.5') raises ValueError, so float('3.5') is returned. int(None) raises TypeError. int(2.9) truncates the float to 2 without raising.
More Python Programming questions
- What is printed? python import math print(math.isclose(0.1 + 0.2, 0.3), math.isclose(1e-10, 0), math.isclose(1e-10, 0, abstol=1e-9)…
- What is printed? python a = 0.1 + 0.2 print(f"{a:.20f}"[:6], a == 0.3, round(a, 15) == round(0.3, 15), str(a) == "0.30000000000000004"…
- What is printed? python print(10 / 4, 10 // 4, -10 // 4, 10 % -4, 2 -2, 7 // 2.0)
- What is printed? python s = "abcdefghij" print(s[2:8:3], s[-2:-9:-3], s[8:2:3] == "", s[::4] + s[1::4])
- What is printed? python L = list(range(10)) L[2:8:2] = "XYZ" del L[-3:] L[len(L):] = [99] print(L)
- What is printed? python d = {"b": 2, "a": 1} print(sorted(d.items()), sorted(d, key=d.get), list(reversed(sorted(d.values()))), {d, "a"…