🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Exceptions and Files: all 15 questions

Quiz: Exceptions and Files · Question 4 of 15

What does this print? (code)

What does this print?

try:
    x = [1, 2][5]
except IndexError:
    print("bad index")
else:
    print("ok")
  1. A.bad index
  2. B.ok
  3. C.bad index and ok
  4. D.Nothing
Answer & explanation

Correct answer: bad index

Index 5 is out of range, so the except block runs and else is skipped.

Can you get the rest right without peeking?

Take the timed Quiz: Exceptions and Files quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →