ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print([x for x in range(5) if x % 2], [x for x in 'a1b2' if x.isdigit()], {x % 3 for x in range(6)})
  1. A. [1, 3] ['1', '2'] {0, 1, 2}
  2. B. [0, 2, 4] ['1', '2'] {0, 1, 2}
  3. C. [1, 3] ['a', 'b'] {0, 1, 2}
  4. D. [1, 3] ['1', '2'] {3, 4, 5}

Correct answer: A. [1, 3] ['1', '2'] {0, 1, 2}

The odd numbers below 5 are 1 and 3. isdigit keeps the digit characters, and the set of remainders mod 3 is {0, 1, 2}. Integers hash to themselves, so the set prints in that order.

More Python Programming questions

Exams that ask this topic