ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
s = "abcdef"
s2 = list(s)
s2[1:3] = ["X", "Y", "Z"]
print("".join(s2))
  1. A. aXYZdef
  2. B. aXYdef
  3. C. aXYZcdef
  4. D. abXYZdef

Correct answer: A. aXYZdef

Slice assignment to a list can change its length: it removes the two elements at indices 1 and 2 ('b', 'c') and splices in three new elements ('X', 'Y', 'Z') in their place, giving a['X','Y','Z']def joined as aXYZdef.

More Python Programming questions

Exams that ask this topic