ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
x = 0
def f():
global x
x += 1
return x
d = {f(): f(), f(): f()}
L = [f(), f()]
L[f() - 7] = f()
print(d, L)- A. {2: 1, 4: 3} [5, 7]
- B. {1: 2, 3: 4} [5, 7]
- C. {1: 2, 3: 4} [8, 6]
- D. {1: 2, 3: 4} [5, 8]
Correct answer: B. {1: 2, 3: 4} [5, 7]
Dict displays evaluate each key before its value, left to right: {1: 2, 3: 4}; L = [5, 6]. In an assignment statement the right-hand side is evaluated first (f() = 7) and the target subscript afterwards (8 - 7 = 1), so L[1] = 7.
More Python Programming questions
- What is printed? python def f(): return f.n f.n = 5 g = f f = lambda: 0 try: print(g()) except AttributeError: print("AE", g.n)
- What is printed? python words = ["kiwi", "fig", "apple", "date"] print(sorted(words, key=len), max(words, key=len), min(words))
- What is printed? python import copy class Node: def init(self): self.kids = [] a = Node() a.kids.append(a) b = copy.deepcopy(a)…
- What is printed? python a = [1, [2, 3]] b = a[:] c = list(a) d = [a] a[1] += [4] a[0] += 10 print(b, c is d, d[1] is a[1])
- What is printed? python s = "abc" it = iter(s) print(next(it), "".join(it), list(it), [c for c in s if c in "ab" or c == "z"])
- What is printed? python def f(n): if n == 0: return [] return [n] + f(n - 1) if n % 2 else f(n - 1) + [n] print(f(5))