← 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?
- A.It is faster
- B.lo + hi can overflow a 32-bit int
- C.It rounds up instead of down
- 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 →