1.

What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Arraylist { 3. Public Static Void Main(string Args[]) { 4. Arraylist Obj = New Arraylist(); 5. Obj.add("a"); 6. Obj.add("b"); 7. Obj.add("c"); 8. Obj.add(1, "d"); 9. System.out.println(obj); 10. } 11. }

Answer»

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]

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]



Discussion

No Comment Found

Related InterviewSolutions