ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
l = [1, 2, 3, 4, 5] l[1:4] = [] print(l)
- A. [1, 5]
- B. [1, 2, 3, 4, 5]
- C. []
- D. [1, None, None, None, 5]
Correct answer: A. [1, 5]
Assigning an empty list to the slice l[1:4] deletes elements at indices 1, 2, 3 (values 2, 3, 4), leaving [1, 5].
More Python Programming questions
- What is printed? python class V: def init(self, x): self.x = x def add(self, o): return V(self.x + o.x) def repr(self): return…
- What is printed? python class W: def init(self, x): self.x = x def lt(self, o): return self.x < o.x ws = [W(3), W(1), W(2)] print([w.x for…
- What is printed? python class N: def init(self, v): self.v = v def bool(self): return self.v != 0 n1 = N(0) n2 = N(5) print(bool(n1)…
- What is printed? python class Base: def repr(self): return "Base" class Sub(Base): pass print([Sub(), Sub()])
- What is printed? python class CM3: def enter(self): return 42 def exit(self, et, ev, tb): return False with CM3() as val: print(val)
- What is printed? python from contextlib import contextmanager @contextmanager def cm(): print("setup") yield 1 print("teardown") with cm()…