ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
a = [3, 1, 4, 1, 5] a.insert(-1, 9) print(a, a.index(1), a.pop(), a.count(1))
- A. [3, 1, 4, 1, 9] 1 5 2
- B. [3, 1, 4, 1, 9, 5] 1 5 2
- C. [3, 1, 4, 1, 5, 9] 1 9 2
- D. [3, 1, 4, 1, 9, 5] 3 5 2
Correct answer: A. [3, 1, 4, 1, 9] 1 5 2
insert(-1, 9) puts 9 BEFORE the last element: [3, 1, 4, 1, 9, 5]. All four arguments are evaluated before print runs, and the first argument is the list object itself, not a snapshot. a.pop() removes 5 while the arguments are being built, so when print finally formats a it shows [3, 1, 4, 1, 9]. index(1) is the first 1 at index 1, and there are two 1s.
More Python Programming questions
- What is printed? python def g(): print("start", end=" ") yield 1 print("mid", end=" ") yield 2 it = g() print("made", end=" ")…
- What is printed? python x = 1 def f(flag): if flag: x = 2 return x print(f(True), end=" ") try: print(f(False)) except UnboundLocalError…
- What is printed? python class A: pass class B(A): pass try: class C(A, B): pass print("ok", end=" ") except TypeError: print("TypeError"…
- What is printed? python class R: def iter(self): print("it", end=" ") return iter([1, 2, 3]) r = R() print(2 in r, 5 in r)
- What is printed? python d = {1: "a", 2: "b"} for k in d: d[k] = d[k] 2 try: for k in d: d[k 10] = "x" except RuntimeError: print("RE"…
- What is printed? python s = "abcbaXabcba" print(s.strip("ab"), s.removeprefix("ab"), s.lstrip("cba"))