ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
x = 5
def f(a, b=x * 2, *, c=x):
return a + b + c
x = 100
print(f(1), f(1, c=x))- A. 201 201
- B. 16 16
- C. 111 111
- D. 16 111
Correct answer: D. 16 111
Default expressions are evaluated once, when def executes (x was 5): b = 10, c = 5, giving 1+10+5 = 16. Passing c=x explicitly uses the current x = 100: 1+10+100 = 111.
More Python Programming questions
- What is printed? python def f(a, L=None): if L is None: L = [] L.append(a) return L r = f(1) print(f(2, r), f(3), r)
- What is printed? python class Cart: def init(self, items=[]): self.items = items a = Cart(); b = Cart(); c = Cart([])…
- What is printed? python fs = [] for i in range(3): fs.append(lambda x: x + i) i = 10 print([f(1) for f in fs])
- What is printed? python def make(): fs = [] for i in range(3): fs.append(lambda: i) i = "z" return fs print([f() for f in make()])
- What is printed? python from functools import partial def p(a, b): return a 10 + b fs = [partial(p, i) for i in range(3)] gs = [lambda b…
- What is printed? python def counter(): count = 0 def inc(): nonlocal count count += 1 return count def peek(): return count return inc…