ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
s = 'racecar level noon python'
print([w for w in s.split() if w == w[::-1]], len([w for w in s.split() if w == w[::-1]]))
  1. A. ['racecar', 'level', 'noon'] 3
  2. B. ['racecar', 'level', 'noon', 'python'] 4
  3. C. ['racecar', 'noon'] 2
  4. D. ['racecar', 'level', 'noon'] 4

Correct answer: A. ['racecar', 'level', 'noon'] 3

A word is a palindrome if it equals its reverse, and 'python' is not one. The comprehension is evaluated twice, but the output counts the same three words both times.

More Python Programming questions

Exams that ask this topic