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)
  1. A. {2: 1, 4: 3} [5, 7]
  2. B. {1: 2, 3: 4} [5, 7]
  3. C. {1: 2, 3: 4} [8, 6]
  4. 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

Exams that ask this topic