Question 14
Back to Exercise
What is the output of the following Java program?
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
int capacity = 10, size = 0;
List<Integer> grows = new ArrayList<>();
for (int i = 0; i < 50; i++) {
if (size == capacity) {
capacity = capacity + (capacity >> 1);
grows.add(capacity);
}
size++;
}
System.out.println(grows + " final=" + capacity);
}
}
- A [20, 40, 80] final=80
- B [15, 22, 33, 49, 73] final=73 Correct
- C [15, 22, 33, 49] final=49
- D [15, 23, 35, 53] final=53
Correct Answer: Option B
capacity >> 1 is integer division by 2: 10 → 15 → 22 → 33 → 49, and the 50th add needs one more growth to 73.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!