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

Binary Search Quiz · Question 2 of 8

Why is mid = lo + (hi - lo) / 2 preferred over (lo + hi) / 2 in Java?

Why is mid = lo + (hi - lo) / 2 preferred over (lo + hi) / 2 in Java?

  1. A.It is faster
  2. B.lo + hi can overflow a 32-bit int
  3. C.It rounds up instead of down
  4. D.It works on unsorted arrays
Answer & explanation

Correct answer: lo + hi can overflow a 32-bit int

When lo and hi are both large, their sum can exceed 2³¹ − 1. The difference form never overflows.

Can you get the rest right without peeking?

Take the timed Binary Search Quiz quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →