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

What will be the output of the following Java program?
Code:
    import java.util.*;
    class Arraylist 
    {
        public static void main(String args[])
        {
            ArrayList obj1 = new ArrayList();
            ArrayList obj2 = new ArrayList();
            obj1.add("A");
            obj1.add("B");
            obj2.add("A");
            obj2.add(1, "B");
            System.out.println(obj1.equals(obj2));
        }
    }
A. 0
B. 1
C. true
D. false
 
Answer: c
Explanation: obj1 and obj2 are an object of class ArrayList hence it is a 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, Both the objects obj1 and obj2 contain same elements i:e A & B thus obj1.equals(obj2) method returns true.
Output:
Code:
$ javac Arraylist.java
$ java Arraylist
true
 

Tìm kiếm

Back
Top