ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
import copy a = [1, 2, 3] b = a.copy() c = list(a) d = a[:] e = copy.copy(a) a.append(4) print(b, c, d, e)
- A. [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3, 4]
- B. [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]
- C. [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3]
- D. [1, 2, 3, 4] [1, 2, 3] [1, 2, 3] [1, 2, 3]
Correct answer: C. [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3]
a.copy(), list(a), a[:] and copy.copy(a) all produce four SEPARATE shallow copies of the flat list made before the append; appending to a afterward changes only a, leaving all four copies at [1, 2, 3].
More Python Programming questions
- What is printed? python try: print("a") except Exception: print("b") else: print("c") finally: print("d")
- What is printed? python def h(): try: x = 1 / 0 except ZeroDivisionError: return "caught" else: return "no error" finally: print("always")…
- What is printed? python def k(): try: return "try" except Exception: return "except" else: return "else" print(k())
- What is printed? python s = "placement" print(s[-100:5])
- What is printed? python s = "abcdef" print(s[10:20])
- What is printed? python s = "abcdef" s2 = list(s) s2[1:3] = ["X", "Y", "Z"] print("".join(s2))