Output of Java Programs | Set 33 (Collections)
Prerequisite: Java – Collections
1. What is the output of following Java Program?
A. Compilation Error
B. 4 7 1
C. 1 4 7
Answer : B. 4 7 1
Explanation : List in java stores its elements in Sequential manner it maintains insertion order. List provides the ability of accessing elements using index.Collections are in the package util so we are importing java.util.ArrayList.
2. What is the output of following Java Program?
B. Compilation Error at line 8
C. Runtime Error
Answer: A. Element1
Explanation : LinkedList has a getFirst() method . It returns an elements at Zero index. LinkedList also maintains its insertion order and provides easy accessing of elements.
3. What is the output of following Java Program?
Answer : B.IndexOutOfBoundException
Explanation : There is no element present in that index ‘0’ so it is IndexOutOfBoundException. In java, if we access the elements out of the index it provides ArrayIndexOutOfBoundException in array. In Collection. it provide IndexOutOfBoundException.
4. What is the Output of following Java Program?
B. Compilation Error
C. Runtime Error
Answer: B. Compilation Error
Explanation: ArrayList doesn’t have method getFirst(). So it is compilation error.getmethod() is available only LinkedList. Therefore, it provide compilation error in this program.
5. What is the Output of following Java Program?
Answer: C. NoSuchElementException
Explanation: There is no element in LinkedList so it return NoSuchElementException. NoSuchElementException is a RuntimeException thrown when there is no more element in it. NoSuchElementException extends RuntimeException.
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 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.