🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Java Collections (List and Set): all 10 questions

Quiz: Java Collections (List and Set) · Question 9 of 10

What does it cost to insert an element at index 0 of an ArrayList with n elements?

What does it cost to insert an element at index 0 of an ArrayList with n elements?

  1. A.O(1)
  2. B.O(log n)
  3. C.O(n), because every element must shift one place right
  4. D.O(n log n)
Answer & explanation

Correct answer: O(n), because every element must shift one place right

An ArrayList is a dynamic array, so inserting at the front moves all the existing elements.

Can you get the rest right without peeking?

Take the timed Quiz: Java Collections (List and Set) quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →