Output of Java Programs | Set 34 (Collections)

09 Apr, 2021
1. What is the Output of the following Java Program? 
Java
import java.util.LinkedList; class Demo {public void show()    {        LinkedList<Integer> list = new LinkedList<Integer>();        list.add(1);        list.add(4);        list.add(7);        list.add(5);        for (int i = 0; i < list.size(); i++) {            System.out.print(list.get(i) + " ");        }    }} public class Main {public static void main(String[] args)    {        Demo demo = new Demo();        demo.show();    }}
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.Collections;import java.util.LinkedList;import java.util.List; class Demo {public void show()    {        List<Integer> list = new LinkedList<Integer>();        list.add(1);        list.add(4);        list.add(7);        list.add(5);        Collections.sort(list); // line 8        System.out.println(list);    }} 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.ConcurrentModificationExceptionB. 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.
This article is contributed by RanjaniRavi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
aktmishra143
Java-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.