ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
s = "abcdef"
print(s[5:1:-1], s[-1:-7:-1] == s[::-1], s[:-7:-1], s[2::-1])
  1. A. fedc True fedcba cba
  2. B. fedc False fedcba cba
  3. C. edcb True fedcba cba
  4. D. fedc True fedcb cb

Correct answer: A. fedc True fedcba cba

s[5:1:-1] takes indices 5 to 2: 'fedc'. -7 is before the start, so s[-1:-7:-1] and s[:-7:-1] both run to the beginning, equal to s[::-1]. s[2::-1] goes from index 2 back to 0: 'cba'.

More Python Programming questions

Exams that ask this topic