Prerequisite – Collections in Java
1) What is the output of the following program?
import java.util.*; public class priorityQueue { public static void main(String[] args) { PriorityQueue<Integer> queue = new PriorityQueue<>(); queue.add(11); queue.add(10); queue.add(22); queue.add(5); queue.add(12); queue.add(2); while (queue.isEmpty() == false) System.out.printf("%d ", queue.remove()); System.out.println("\n"); } } |
a) 11 10 22 5 12 2
b) 2 12 5 22 10 11
c) 2 5 10 11 12 22
d) 22 12 11 10 5 2
Ans. (c)
Explanation: Priority queue always outputs the minimum element from the queue when remove() method is called, no matter what the sequence of input is.
2) What is the output of the following program?
import java.util.*; public class Treeset { public static void main(String[] args) { TreeSet<String> treeSet = new TreeSet<>(); treeSet.add("Geeks"); treeSet.add("for"); treeSet.add("Geeks"); treeSet.add("GeeksforGeeks"); for (String temp : treeSet) System.out.printf(temp + " "); System.out.println("\n"); } } |
a) Geeks for Geeks GeeksforGeeks
b) Geeks for GeeksforGeeks
c) Geeks GeeksforGeeks for
d) for GeeksforGeeks Geeks
Ans. (c)
Explanation: A TreeSet sorts the data in ascending order that is inserted in it. Therefore, the output string contains all the strings arranged in ascending order. A TreeSet does not contain any duplicate element as it is a set. So in the output, there is just a single occurrence of string ‘Geeks’.
3) What is the output of the following program?
import java.util.*; public class linkedList { public static void main(String[] args) { List<String> list1 = new LinkedList<>(); list1.add("Geeks"); list1.add("for"); list1.add("Geeks"); list1.add("GFG"); list1.add("GeeksforGeeks"); List<String> list2 = new LinkedList<>(); list2.add("Geeks"); list1.removeAll(list2); for (String temp : list1) System.out.printf(temp + " "); System.out.println(); } } |
a) for Geeks GFG GeeksforGeeks
b) for GeeksforGeeks GFG
c) for GFG for
d) for GFG GeeksforGeeks
Ans. (d)
Explanation: list1.removeAll(list2) function deletes all the occurrence of string in list2 from list1. Here, the string ‘Geeks’ appears in list2, so all the nodes of linked list in list1 that contains ‘Geeks’ as its data is removed from list1.
4) Which of the given choices is a possible output ?
import java.util.*; public class hashSet { public static void main(String[] args) { HashSet<String> hashSet = new HashSet<>(); hashSet.add("Geeks"); hashSet.add("for"); hashSet.add("Geeks"); hashSet.add("GeeksforGeeks"); System.out.println(hashSet); } } |
a) [Geeks, for, Geeks, GeeksforGeeks]
b) [GeeksforGeeks, Geeks, for]
Ans. (b)
Explanation: A HashSet is a set and as a set doesn’t contain any duplicate element therefore, the string ‘Geeks’ appears only once in the output.
5) What is the output of the following program?
import java.util.*; public class stack { public static void main(String[] args) { List<String> list = new LinkedList<>(); list.add("Geeks"); list.add("for"); list.add("Geeks"); list.add("GeeksforGeeks"); Iterator<Integer> iter = list.iterator(); while (iter.hasNext()) System.out.printf(iter.next() + " "); System.out.println(); } } |
a) Geeks for Geeks GeeksforGeeks
b) GeeksforGeeks Geeks for Geeks
c) Runtime Error
d) Compilation Error
Ans. (d)
Explanation: An iterator made for iterating over Integer cannot be used to iterate over String data type. Corrected program : https://ide.geeksforgeeks.org/DgeN0P
This article is contributed by Mayank Kumar. 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.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Output of Java Programs | Set 34 (Collections)
- Output of Java Programs | Set 33 (Collections)
- Difference between Traditional Collections and Concurrent Collections in java
- Output of Java Programs | Set 12
- Output of Java programs | Set 10 (Garbage Collection)
- Output of Java Programs | Set 14 (Constructors)
- Output of Java Programs | Set 21 (Type Conversions)
- Output of Java programs | Set 24 (Final Modifier)
- Output of Java programs | Set 29
- Output of Java Programs | Set 30
- Output of Java Programs | Set 31
- Output of Java Programs | Set 32
- Output of Java Programs | Set 35 (Decision Making)
- Output of Java Programs | Set 36 (do-while loop)
- Output of Java Programs | Set 37 (If-else)
- Output of Java Programs | Set 38 (Arrays)
- Output of Java Programs | Set 39 (throw keyword)
- Output of Java Programs | Set 40 (for loop)
- Output of Java Programs | Set 41 (try-catch)
- Output of Java Programs | Set 45 (static and instance variables)

