Output of Java programs | Set 13 (Collections)
Prerequisite – Collections in Java
1) What is the output of the following program?
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
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?
a) Geeks For Geeks GeeksforGeeks
b) Geeks For GeeksforGeeks
c) For Geeks GeeksforGeeks
d) For GeeksforGeeks Geeks
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?
a) For Geeks GFG GeeksforGeeks
b) For GeeksforGeeks GFG
c) For GFG for
d) For GFG GeeksforGeeks
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?
a) [Geeks, For, Geeks, GeeksforGeeks]
b) [GeeksforGeeks, Geeks, For]
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?
a) Geeks for Geeks GeeksforGeeks
b) GeeksforGeeks Geeks for Geeks
c) Runtime Error
d) Compilation Error
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 email@example.com. 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.