ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
print(repr('abc'[5:]), repr('abc'[-10:2]), [1, 2][3:0:-1])
  1. A. '' 'abc' [2, 1]
  2. B. 'abc' 'ab' []
  3. C. '' 'ab' [1, 2]
  4. D. '' 'ab' [2]

Correct answer: D. '' 'ab' [2]

Out-of-range slice bounds are clamped, never an error. 'abc'[5:] is empty, and -10 clamps to 0 in 'abc'[-10:2]. For [1, 2][3:0:-1], the start clamps to index 1, so only index 1 is taken, which gives [2].

More Python Programming questions

Exams that ask this topic