ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
print([x for x in range(20) if x % 2 == 0 if x % 3 == 0])
- A. [0, 6, 12, 18]
- B. [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
- C. [0, 3, 6, 9, 12, 15, 18]
- D. [6, 12, 18]
Correct answer: A. [0, 6, 12, 18]
Two consecutive if-clauses in a comprehension act as an AND: both conditions must hold. Only multiples of 6 (divisible by both 2 and 3) survive: 0, 6, 12, 18.
More Python Programming questions
- What is printed? python print([x if x % 2 == 0 else -x for x in range(5)])
- What is printed? python words = ["banana", "kiwi", "apple", "fig"] print(sorted(words, key=len)) print(sorted(words, key=lambda w: w[-1]))
- What is printed? python s = "Hello World" print(s.swapcase()) print(s.title()) print(" a b ".split())
- What is printed? python print("a-b--c".split("-")) print("a-b--c".split("-", 1))
- What is printed? python d = {"b": 2, "a": 1, "c": 3} print(sorted(d.items())) print(dict(sorted(d.items())))
- What is printed? python from functools import lrucache calls = [] @lrucache(maxsize=None) def f(n): calls.append(n) return n n f(2); f(3)…