A method public static int mystery(int n) contains the following algorithm: initialize a counter to 0, repeatedly divide n by 2 (ignoring remainders) while n is greater than 0, increment the counter each time, and return the counter value. What does this algorithm compute, and what will mystery(12) return?
- Computes the number of factors of 2 in n; returns 2 because 12 = 4 × 3 and 4 = 2²
- Computes the floor of log₂(n) plus 1; returns 4 because it counts division steps until reaching 0
- Computes the number of binary digits needed to represent n; returns 4 because 12 in binary is 1100 (correct answer)
- Computes the largest power of 2 less than n; returns 3 because 2³ = 8 is the largest power of 2 less than 12
Explanation: The algorithm repeatedly divides by 2 until reaching 0, counting steps. For n=12: 12→6→3→1→0 takes 4 steps. This counts binary digits because each division by 2 removes one binary digit. 12 = 1100₂ has 4 digits. Choice A confuses this with factoring. Choice B is close but describes it as log₂(n)+1 rather than binary digit count. Choice D misidentifies what's being computed entirely.