ProgrammingModerateInfosys · Accenture · Capgemini pattern
What is printed?
What is printed?
a = [1, 2, 3] b = a b += [4] print(a)
- A. [1, 2, 3, [4]]
- B. [1, 2, 3]
- C. [4]
- D. [1, 2, 3, 4]
Correct answer: D. [1, 2, 3, 4]
b = a makes b another name for the same list, and += on a list extends it in place. So the change made through b shows up when a is printed.
More Python Programming questions
- What is printed? python print(bool("False"), bool(""), bool(0.0))
- What is printed? python nums = [3, 1, 2] print(nums.sort())
- What is printed? python print([x x for x in range(5) if x % 2])
- What is printed? python s = "hello" print(s[::-2])
- What is printed? python t = (1,) print(type(t).name, type((1)).name)
- What is printed? python def f(args, kw): print(len(args), len(kw)) f(1, 2, a=3)