ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
x = 10
def f(x, y=x):
x += 1
return x, y
x = 20
print(f(5), f(y=1, x=2), x)- A. (6, 20) (3, 1) 20
- B. (6, 10) (3, 1) 20
- C. (6, 10) (3, 1) 21
- D. (6, 5) (3, 1) 20
Correct answer: B. (6, 10) (3, 1) 20
The default y=x is evaluated at def time using the global x = 10. Inside f, x is the parameter, so x += 1 affects only the local name; the global stays 20.
More Python Programming questions
- What is printed? python for i in range(3): if i == 5: break else: print("else", i, end=" ") n = 0 while n < 3: n += 1 if n == 2: break…
- What is printed? python def find(xs, t): for i, x in enumerate(xs): if x == t: return i else: return -1 return "unreachable"…
- What is printed? python def kind(v): match v: case 0 | 1: return "bit" case int(n) if n < 0: return "neg" case [x, rest]: return…
- What is printed? python RED = "r" def f(c): match c: case RED: return "red:" + RED print(f("g"), RED)
- What is printed? python data = [3, 8, 2, 9, 4] if (n := len(data)) > 4: print(n, end=" ") print([y for x in data if (y := x 2) > 10], y)
- What is printed? python a = [5, 3, 5, 1] a.remove(5) a.insert(10, 7) a.insert(-10, 0) print(a, a.index(5), a.pop(1))