Output of Java Programs | Set 34 (Collections)
1. What is the Output of following Java Program?
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 element in sequential order and then we can access element in List using index. List provides the ability to access its elements by using its index. But in set, map elements are not accessed by using index.
2. What is the output of following Java Program?
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 9 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 following Java Program?
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. Compilation Error cannot give Collections.sort() after Iterator.
B. apple banana
C. banana apple
Answer: B. apple banana
Explanation: Collections.sort() sort element and Iterator is an object used to traverse through a Collection. Iterator is an interface available in Collection framework in java.util.package. It is used to traverse elements one by one.
4. What is the Output of following Java Program?
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 return the runtime class of an object. That class object is the object that is locked by static synchronized method of represented class. Here both are in ArrayList Class so answer is true.
5. What is the Output of following Java Program?
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 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.
Recommended Posts:
- Output of Java programs | Set 13 (Collections)
- Output of Java Programs | Set 33 (Collections)
- Output of Java Programs | Set 50
- Output of Java Programs | Set 30
- Output of Java Programs | Set 37 (If-else)
- Output of Java Programs | Set 12
- Output of Java programs | Set 29
- Output of Java Programs | Set 49
- Output of Java Programs | Set 31
- Output of Java Programs | Set 32
- Output of Java Programs | Set 36 (do-while loop)
- Output of Java Programs | Set 14 (Constructors)
- Output of Java Programs | Set 47 (Arrays)
- Output of Java Programs | Set 42 (Arrays)
- Output of Java Programs | Set 38 (Arrays)



