Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Output of Java Programs | Set 33 (Collections)

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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 Error
B. 4 7 1
C. 1 4 7
D. 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. Element1
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?




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. ArrayIndexOutOfBoundException
B. IndexOutOfBoundException
C. 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_one
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?




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. null
B. IndexOutOfBoundException
C. 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.


My Personal Notes arrow_drop_up
Last Updated : 18 Sep, 2017
Like Article
Save Article
Similar Reads