ProgrammingModerateGATE CS · Product-company placement
What is printed?
What is printed?
a = b = [] a.append(1) c, d = [], [] c.append(1) x = y = 5 x += 1 print(b, d, y)
- A. [] [] 5
- B. [1] [1] 6
- C. [1] [] 6
- D. [1] [] 5
Correct answer: D. [1] [] 5
Chained assignment a = b = [] binds both names to ONE list, so the append is seen through b. c and d are two separate lists. x += 1 rebinds x to a new int; y keeps 5.
More Python Programming questions
- What is printed? python a = [1, 2, 3] i = 0 i, a[i] = 2, 9 print(a, i)
- What is printed? python x = [0, 1] i = 0 x[i], i = 5, 1 j = 1 j, x[j] = 0, 7 print(x, i, j)
- What is printed? python a, b = 1, 2 a, b = b, a + b c = [3, 4] c[0], c[1] = c[1], c[0] m, n = n, m = 7, 8 print(a, b, c, m, n)
- What is printed? python b = "Hé".encode("utf-8") print(len("Hé"), len(b), b[1], b.decode("latin-1"))
- What is printed? python b = b"abc" print(b[0], b[:1], list(b"hi"), bytes([65, 66]), b"a" + bytearray(b"b"))
- What is printed? python print(chr(ord("a") + 25), ord("A") - ord("a"), "".join(chr(ord(c) ^ 32) for c in "PyThOn"), str(b"x", "ascii"))