ProgrammingModerateProduct-company MCQ pattern
What is printed?
What is printed?
print([(i, j) for i in range(3) for j in range(i)])
- A. [(0, 1), (0, 2), (1, 2)]
- B. [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (2, 2)]
- C. []
- D. [(1, 0), (2, 0), (2, 1)]
Correct answer: D. [(1, 0), (2, 0), (2, 1)]
The for clauses nest left to right like ordinary loops, so the inner j loop runs over range(i). For i = 0 it runs no times, for i = 1 it gives j = 0, and for i = 2 it gives j = 0 and 1.
More Python Programming questions
- What is printed? python def g(): x = yield 1 yield x 2 it = g() print(next(it), it.send(5))
- What is printed? python calls = 0 def f(n): global calls calls += 1 return 1 if n <= 1 else f(n - 1) + f(n - 2) print(f(5), calls)
- What is printed? python import copy a = [[1, 2], [3]] b = copy.copy(a) c = copy.deepcopy(a) a[0].append(9) a.append([4]) print(len(b)…
- What is printed? python x = [1] 3 for i in range(3): x[i] = x[i - 1] + i print(x)
- 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(""))