What will be the output of the following Java program? [code] import java.util.*; class Output { public static

Trần Vũ

New member
What will be the output of the following Java program?
Code:
    import java.util.*;
    class Output 
    {
        public static void main(String args[]) 
        {
            ArrayList obj = new ArrayList();
            obj.add("A");
            obj.ensureCapacity(3);
            System.out.println(obj.size());
        }
    }
A. 1
B. 2
C. 3
D. 4
 
Answer: a
Explanation: Although obj.ensureCapacity(3); has manually increased the capacity of obj to 3 but the value is stored only at index 0, therefore obj.size() returns the total number of elements stored in the obj i:e 1, it has nothing to do with ensureCapacity().
Output:
Code:
$ javac Output.java
$ java Output
1
 

Tìm kiếm

Back
Top