ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
x = [1] * 3
for i in range(3):
x[i] = x[i - 1] + i
print(x)- A. [1, 2, 3]
- B. [1, 1, 2]
- C. IndexError
- D. [1, 2, 4]
Correct answer: D. [1, 2, 4]
On the first pass x[i - 1] is x[-1], the LAST element (1), not an error: x[0] = 1 + 0 = 1. Then x[1] = 1 + 1 = 2 and x[2] = 2 + 2 = 4.
More Python Programming questions
- What is printed? python x = "g" def outer(): x = "o" def inner(): global x x = "i" inner() return x print(outer(), x)
- What is printed? python s = " Py Thon " print(s.strip().lower().replace(" ", "").count("o"), s.split(" ").count(""))
- What is printed? python def f(x): try: r = 10 // x except ZeroDivisionError: return "Z" else: return r finally: print("F", end=" ")…
- What is printed? python class P: def repr(self): return "R" def str(self): return "S" print(P(), [P()], f"{P()!r}")
- What is printed? python class K: def eq(self, other): return True try: print({K(): 1}) except TypeError: print("TypeError")
- What is printed? python it = iter([1, 2, 3, 4, 5]) print(list(zip(it, it)))