ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
print([x for x in [1, 2, 3] for _ in range(x)])
- A. [1, 2, 3]
- B. [1, 2, 2, 3, 3, 3]
- C. [1, 1, 2, 3]
- D. [1, 2, 2, 3, 3]
Correct answer: B. [1, 2, 2, 3, 3, 3]
The outer loop picks each x and the inner loop repeats it x times, so 1 appears once, 2 twice and 3 three times.
More Python Programming questions
- What is printed? python print(sorted(['b10', 'b9', 'a100', 'c2'], key=lambda s: (s[0], int(s[1:]))))
- What is printed? python print(sorted([-2, 1, 2, -1], key=abs))
- What is printed? python print('%5.1f|%-5d|' % (3.14159, 42))
- What is printed? python print(max(range(7), key=lambda i: (i % 3, i)))
- What is printed? python print(5 // 0.5, 5 % 0.5)
- What is printed? python print('xy'.replace('', '-'))