ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
nums = [5, 3, 8] nums2 = nums nums += [1] nums3 = nums[:] nums3.pop() print(nums2, nums3)
- A. [5, 3, 8, 1] [5, 3, 8]
- B. [5, 3, 8] [5, 3, 8]
- C. [5, 3, 8, 1] [5, 3, 8, 1]
- D. [5, 3, 8] [5, 3]
Correct answer: A. [5, 3, 8, 1] [5, 3, 8]
+= on a list extends the same object, so nums2 sees the new item too. nums3 is an independent copy, and pop removes its last value 1, leaving [5, 3, 8].
More Python Programming questions
- What is printed? python from math import gcd print(gcd(0, 5), gcd(-4, 6), gcd(0, 0))
- What is printed? python x = 12 x //= 5 y = 12 y /= 5 print(x, y, type(x).name, type(y).name)
- What is printed? python import itertools print(list(itertools.islice(itertools.cycle('ab'), 5)), list(itertools.repeat(7, 2)))
- What is printed? python def f(): yield 1 yield 2 print(sum(f()), max(f()), list(zip(f(), f())))
- What is printed? python s = 'a-b-c' print(s.split('-', 1), s.rsplit('-', 1)[-1], s.partition('-')[2])
- What is printed? python print(divmod(-1, 10), divmod(1, -10), -1 % 10)