Question
import java.util.*;
class Arraylist {
public static void main(String args[]) {
ArrayList obj = new ArrayList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.add(1, "D");
System.out.println(obj);
}
}
What is the output of this program?
import java.util.*;
class Arraylist {
public static void main(String args[]) {
ArrayList obj = new ArrayList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.add(1, "D");
System.out.println(obj);
}
}
Answer: Option B
Was this answer helpful ?
obj is an object of class ArrayList hence it is an dynamic array which can increase and
decrease its size. obj.add("X") adds to the array element X and obj.add(1,"X") adds
element x at index position 1 in the list, Hence obj.add(1,"D") stores D at index position
1 of obj and shifts the previous value stored at that position by 1.
Output:
$ javac Arraylist.java
$ java Arraylist
[A, D, B, C]
Was this answer helpful ?
More Questions on This Topic :
Question 9.
Which of these methods must be made static?
....
Submit Solution