Open In App

Output of Java Programs | Set 34 (Collections)

Improve
Improve
Like Article
Like
Save
Share
Report

1. What is the Output of the following Java Program? 

Java





A. Compilation Error 
B. 1 4 7 5 
C. 1 4 5 7 
 

Answer: B. 1 4 7 5

Explanation: List stores elements in sequential order and then we can access an element in List using an index. The List provides the ability to access its elements by using its index. But in the set, map elements are not accessed by using an index.

2. What is the output of the following Java Program? 
 

Java




import java.util.ArrayList;
 
class Demo {
public void show()
    {
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<Integer> list1 = new ArrayList<Integer>();
        boolean check = (list.getClass() == list1.getClass());
        System.out.println(check);
    }
} public class Main {
public static void main(String[] args)
    {
        Demo demo = new Demo();
        demo.show();
    }
}


A. Compilation Error at line 9 
B. [1, 4, 5, 7] 
C. [1, 4, 7, 5] 
 

Answer: B. [1, 4, 5, 7]

Explanation: Collections.sort() sort the list in ascending order. Collections class provides static methods for sorting the elements in collections. If Collection elements are of set type elements are inserted in sorted order no need to sort.

3. What is the output of the following Java Program? 
 

Java




import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
 
class Demo {
public void show()
    {
        ArrayList<String> list = new ArrayList<String>();
        list.add("banana");
        list.add("apple");
        Iterator itr = list.iterator();
 
        Collections.sort(list);
        while (itr.hasNext()) {
            System.out.print(itr.next() + " ");
        }
    }
} public class Main {
public static void main(String[] args)
    {
        Demo demo = new Demo();
        demo.show();
    }
}


A. Exception in thread “main” java.util.ConcurrentModificationException
B. apple banana 
C. banana apple 
 

Answer: A. Exception in thread "main" java.util.ConcurrentModificationException

Explanation: Collections.sort() sort element and Iterator is an object used to traverse through a Collection. Iterator is an interface available in the Collection framework in java.util.package. It is used to traverse elements one by one.

4. What is the Output of the following Java Program? 
 

Java




import java.util.ArrayList;
 
class Demo {
public void show()
    {
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<Integer> list1 = new ArrayList<Integer>();
        boolean check = (list.getClass() == list1.getClass());
        System.out.println(check);
    }
} public class Main {
public static void main(String[] args)
    {
        Demo demo = new Demo();
        demo.show();
    }
}


A. true 
B. false 

Answer: A. true

Explanation: getclass() method returns the runtime class of an object. That class object is the object that is locked by the static synchronized method of represented class. Here both are in ArrayList Class so the answer is true.

5. What is the Output of the following Java Program? 
 

Java




import java.util.LinkedList;
 
class Demo {
public void show()
    {
        LinkedList<String> list = new LinkedList<String>();
 
        System.out.println(list.getClass());
    }
} public class Main {
public static void main(String[] args)
    {
        Demo demo = new Demo();
        demo.show();
    }
}


A. class java.util.LinkedList 
B. String 
C. Compiler Error 

Answer: A. class java.util.LinkedList

Explanation: getclass() method returns the runtime class of an object. That class object is the object that is locked by the static synchronized method of represented class. Here LinkedList is the runtime class so the answer is java.util.LinkedList.

 



Last Updated : 09 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads