Java MCQ Output based Questions - Collections

In this article, we will cover some Java MCQ output based questions which will help extend your Java knowledge. Usually In Interviews nowadays, interviewer are more focused on negative questions rather than conceptual questions, and If you go through below questions, It will surely give you an add on and add value to your experience.

Java MCQ Output based Questions

Collections in Java

What is the output of below 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 


Explanation - Priority queue always outputs the minimum element from the queue when remove() method is called, no matter what the sequence of input is.


What is the output of below 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) 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’.


What is the output of below 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


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.


What is the output of below program?

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]


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.


What is the output for below 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


Explanation - An iterator made for iterating over Integer cannot be used to iterate over String data type.


What is the output for below program?

import java.util.LinkedList;

 

class Demo {

public void show()

    {

        LinkedList<Integer> list = new LinkedList<Integer>();

        list.add(1);

        list.add(4);

        list.add(7);

        list.add(5);

        for (int i = 0; i < list.size(); i++) {

            System.out.print(list.get(i) + " ");

        }

    }

} public class Main {

public static void main(String[] args)

    {

        Demo demo = new Demo();

        demo.show();

    }

}

A. Compilation Error 

B. 1 4 7 5 

C. 1 4 5 7


Explanation - List stores elements in sequential order and then we can access an element in List using an index. The List provides the ability to access its elements by using its index. But in the set, map elements are not accessed by using an index.


What is the output of below 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


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.


What is thr output of below 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


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.


What is the output of below 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


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.


What is thr output of below 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


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.



Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Coforge Interview Questions | Automation Testing profile

Testing in CI/CD