ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
a = "hello"
try:
    a[0] = "j"
except TypeError:
    a = "j" + a[1:]
b = a.replace("l", "X", 1)
print(a, b, len(a.replace("", "-")))
  1. A. jello jeXXo 11
  2. B. hello heXlo 11
  3. C. jello jeXlo 11
  4. D. jello jeXlo 9

Correct answer: C. jello jeXlo 11

Strings are immutable, so item assignment fails and a new string is built. replace's third argument limits it to one replacement. Replacing '' inserts '-' at all len+1 = 6 gaps, giving 5 + 6 = 11 characters.

More Python Programming questions

Exams that ask this topic