← 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?
- A.long arithmetic is slower than int arithmetic
- B.Each += unboxes sum, adds, and boxes a new Long object
- C.The loop runs twice
- 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 →