ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
lst = [1, 2, 3] lst2 = lst lst *= 2 lst2 += [9] print(lst, lst2, lst is lst2)
- A. [1, 2, 3, 1, 2, 3, 9] [1, 2, 3, 1, 2, 3, 9] True
- B. [1, 2, 3, 1, 2, 3] [1, 2, 3, 1, 2, 3, 9] False
- C. [1, 2, 3, 9] [1, 2, 3, 9] True
- D. [1, 2, 3, 1, 2, 3, 9] [1, 2, 3, 1, 2, 3] True
Correct answer: A. [1, 2, 3, 1, 2, 3, 9] [1, 2, 3, 1, 2, 3, 9] True
*= and += on a list change the same object in place, so lst and lst2 are still one list. The last += is therefore visible through both names.
More Python Programming questions
- What is printed? python print(f'{0.1 + 0.2:.17g}', f'{1/7:.10f}', f'{100/3:.0f}', f'{2.345:.2f}')
- What is printed? python a = [3, 1, 2] b = sorted(a, key=lambda x: -x) a.sort(reverse=True) print(a == b, a, b)
- What is printed? python print(pow(7, 222, 10), pow(7, 222, 1000), 7222 % 1000, pow(-7, 3, -5))
- What is printed? python print(int(True) + int('0b11', 2), int(3.7 // 1), int(-0.5), round(-0.5), round(0.5))
- What is printed? python n = 0b10110000 print(n & -n, n & (n - 1), bin(n >> 4), n.bitcount())
- What is printed? python a, b = 5, 9 a ^= b b ^= a a ^= b print(a, b, a ^ b)