🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Sorting Basics: all 13 questions

Quiz: Sorting Basics · Question 1 of 13

What does this print? (code)

What does this print?

a = [4, 3, 2, 1]
for i in range(1, len(a)):
    key, k = a[i], i - 1
    while k >= 0 and a[k] > key:
        a[k + 1] = a[k]
        k -= 1
    a[k + 1] = key
    if i == 2:
        print(a)
  1. A.[3, 4, 2, 1]
  2. B.[2, 3, 4, 1]
  3. C.[1, 2, 3, 4]
  4. D.[2, 3, 1, 4]
Answer & explanation

Correct answer: [2, 3, 4, 1]

After i = 2 the first three items (4, 3, 2) are sorted: [2, 3, 4], and 1 has not been touched yet.

Can you get the rest right without peeking?

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

Start the quiz →