🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Wrapper Classes and Autoboxing: all 10 questions

Quiz: Wrapper Classes and Autoboxing · Question 9 of 10

Why is Long sum = 0L; for (long i = 0; i < n; i++) sum += i; slow?

Why is Long sum = 0L; for (long i = 0; i < n; i++) sum += i; slow?

  1. A.long arithmetic is slower than int arithmetic
  2. B.Each += unboxes sum, adds, and boxes a new Long object
  3. C.The loop runs twice
  4. D.Long cannot hold large sums
Answer & explanation

Correct answer: Each += unboxes sum, adds, and boxes a new Long object

Boxing in a loop creates a new object on almost every round. Use the primitive long for the sum.

Can you get the rest right without peeking?

Take the timed Quiz: Wrapper Classes and Autoboxing quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →