ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
a = [1, 2, 3, 4, 5] a[1:3] = [9] a[::2] = [0, 0] print(a)
- A. [0, 9, 4, 0]
- B. [0, 2, 0, 4, 0]
- C. ValueError
- D. [0, 9, 0, 5]
Correct answer: D. [0, 9, 0, 5]
a[1:3] = [9] replaces two items with one, giving [1, 9, 4, 5]. The extended slice a[::2] then covers indices 0 and 2 (two places), and the two zeros fit exactly, giving [0, 9, 0, 5]. With a wrong count, an extended slice assignment would raise ValueError.
More Python Programming questions
- What is printed? python d = {} d[1] = 'a' d[1.0] = 'b' d[True] = 'c' print(d)
- What is printed? python x = [1, 2, 3] for i in x: x.remove(i) print(x)
- What is printed? python fs = [lambda: i for i in range(3)] print([f() for f in fs])
- What is printed? python def f(x, lst=[]): lst.append(x) return lst a = f(1) b = f(2, []) c = f(3) print(a, b, c)
- What is printed? python def f(): try: return 1 finally: return 2 print(f())
- What is printed? python print(1 < 2 > 1.5, 3 > 2 == 2, (3 > 2) == 2)