ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
print(repr('abc'[5:]), repr('abc'[-10:2]), [1, 2][3:0:-1])- A. '' 'abc' [2, 1]
- B. 'abc' 'ab' []
- C. '' 'ab' [1, 2]
- 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
- What is printed? python s = 'banana' print(s.find('an', 2), s.rfind('an'), s.find('x'), s.index('na', 3))
- What is printed? python print('aaaa'.count('aa'), 'aaaa'.replace('aa', 'b'), len('aaaaa'.split('aa')))
- What is printed? python x = 3.14159 print(f"{x:08.3f}|{x:>8.2e}|{42:#x}|{'ab'!r:>6}")
- What is printed? python print(f"{1234567.891:,.2f}", f"{255:b}", f"{-5:+d}")
- What is printed? python print('-'.join(map(str, [1, 2])) 2, ('ab' 3).count('ba'))
- What is printed? python print('a,,b,'.split(','), ' a b '.split(' '))