ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
s = 'a'
for i in range(3):
    s = s + s[::-1] + str(i)
print(s, len(s))
  1. A. aa00aa1aa00aa2 14
  2. B. aa00aa11aa00aa2 14
  3. C. aa00aa11aa00aa2 15
  4. D. aa00aa11aa00aa 15

Correct answer: C. aa00aa11aa00aa2 15

Each pass makes the new string the old one, its reverse, and a digit, so the length goes 1, 3, 7, 15. Tracking the length this way avoids simulating the string.

More Python Programming questions

Exams that ask this topic