Output of Java Programs | Set 33 (Collections)

18 Sep, 2017
Prerequisite: Java – Collections
1. What is the output of following Java Program?
import java.util.ArrayList;class Demo {public void show()    {        ArrayList<Integer> list = new ArrayList<Integer>();        list.add(4);        list.add(7);        list.add(1);        for (int number : list) {            System.out.print(number + " ");        }    }} public class Main {public static void main(String[] args)    {        Demo demo = new Demo();        demo.show();    }}
A. Compilation ErrorB. 4 7 1C. 1 4 7D. None
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?
import java.util.LinkedList;  class Demo {public void show()    {        LinkedList<String> list = new LinkedList<String>();        list.add("Element1"); // line 6        list.add("Element2");        System.out.print(list.getFirst()); // line 8    }} public class Main {public static void main(String[] args)    {        Demo demo = new Demo();        demo.show();    }}
A. Element1B. Compilation Error at line 8C. 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?
import java.util.ArrayList;class Demo {public void show()    {        ArrayList<String> list = new ArrayList<String>();        System.out.print(list.get(0));    }} public class Main {public static void main(String[] args)    {        Demo demo = new Demo();        demo.show();    }}
A. ArrayIndexOutOfBoundExceptionB. IndexOutOfBoundExceptionC. null
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?
import java.util.ArrayList;  class Demo {public void show()    {        ArrayList<String> list = new ArrayList<String>();        list.add("GeeksForGeeks_one"); // line 6        list.add("GeeksForGeeks_two");        System.out.print(list.getFirst()); // line 8    }} public class Main {public static void main(String[] args)    {        Demo demo = new Demo();        demo.show();    }}
A. GeeksForGeeks_oneB. Compilation ErrorC. 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?
import java.util.LinkedList;  class Demo {public void show()    {        LinkedList<String> list = new LinkedList<String>();          System.out.print(list.getFirst());    }} public class Main {public static void main(String[] args)    {        Demo demo = new Demo();        demo.show();    }}
A. nullB. IndexOutOfBoundExceptionC. NoSuchElementException
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 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.
Java-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Output of C++ programs | Set 50
Runtime Errors
C++ Programming Multiple Choice Questions
Different ways to copy a string in C/C++
Output of C Programs | Set 2
Output of C++ Program | Set 1
Output of Python Program | Set 1
Output of C++ programs | Set 47 (Pointers)
Output of C programs | Set 59 (Loops and Control Statements)
Output of C programs | Set 35 (Loops)