Output of Java Programs | Set 55 (Java Collections Framework)

Pre-requisites: Java Collection Framework.

1. What is the Output of following Java Program?

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
  
class Demo {
    public static void main(String[] args)
    {
        ArrayList<Integer> arr = new ArrayList<Integer>();
        arr.add(11);
        arr.add(2);
        arr.add(3);
        arr.add(5);
        arr.add(7);
        arr.remove(new Integer(7));
        arr.remove(2);
        for (int i = 0; i < arr.size(); i++)
            System.out.print(arr.get(i) + " ");
    }
}

chevron_right


A. Compilation error.
B. 11 3 5
C. 11 2 5

Answer: C.

Explanation: ArrayList.remove() method takes either the index or the Integer class object. Here it takes the index and removes the elements at that index. So when we use new Integer(7) it deletes the value 7 from ArrayList.

2. What is the Output of following Java Program?



filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
  
class Demo {
    public static void main(String[] args)
    {
        Deque<Integer> dq = new LinkedList<Integer>();
        dq.offerFirst(1);
        dq.offerFirst(2);
        dq.offerFirst(3);
        dq.offerLast(4);
        Queue<Integer> q = new LinkedList<Integer>();
        Iterator it = dq.descendingIterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
    }
}

chevron_right


A. 1 2 3 4
B. 4 1 2 3
C. 4 3 2 1

Answer: B.

Explanation: Deque is a doubly ended queue in which data can be inserted as well as deleted from both ends. When we use descendingIterator(), we actually take the output in the reverse order of the stored data.

3. What is the Output of following Java Program?

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
  
class Demo {
    public static void main(String[] args)
    {
        Set<Integer> h1
            = new TreeSet<Integer>(
                Collections.reverseOrder());
        h1.addAll(
            Arrays.asList(
                new Integer[] { 1, 2, 3,
                                3, 5, 6,
                                7, 1, 4 }));
        for (int h : h1)
            System.out.print(h + " ");
    }
}

chevron_right


A. 1 2 3 3 5 6 7 1 4
B. 3 2 1 7 6 4 5
C. 7 6 5 4 3 2 1
D. 1 2 3 4 5 6 7

Answer: C.

Explanation: Set doesn’t store duplicate values. TreeSet stores data in sorted order i.e. Ascending order, but here we have used Collections.reverseOrder() to make it store data in the descending order explicitly.

4. What is the Output of following Java Program?

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
  
class Demo {
    public static void main(String[] args)
    {
        Set<Integer> h1 = new LinkedHashSet<Integer>();
        Stack<Integer> s1 = new Stack<Integer>();
        for (int i = 0; i < 6; i++)
            h1.add(i);
        for (int h : h1) {
            s1.push(h);
        }
        while (!s1.isEmpty()) {
            System.out.print(s1.pop() + " ");
        }
    }
}

chevron_right


A. 3 4 1 2 5 0
B. 5 4 3 2 1 0
C. 1 2 3 4 5 0
D. 3 1 2 5 0 4

Answer: B.

Explanation: LinkedHashSet doesn’t store any duplicate values and it stores the data in the original order of insertion. Also, stack is a LIFO Data structure. Therefore the most recently inserted value in Stack, gets popped out.



5. What is the Output of following Java Program?

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
  
class Demo {
    public static void main(String[] args)
    {
        LinkedHashMap<String, Integer> hm
            = new LinkedHashMap<String, Integer>();
        hm.put("John", 1);
        hm.put("Ron", 2);
        hm.put("George", 3);
        hm.put("Ray", 4);
        for (Map.Entry<String, Integer> e : hm.entrySet())
            System.out.println(
                e.getKey() + " "
                + e.getValue());
    }
}

chevron_right


A.

   Ray 4
   George 3
   John 1
   Ron 2

B.

   John 1
   Ron 2
   George 3
   Ray 4

C.

   Ray 4
   George 3
   Ron 2 
   John 1 
Answer: B.

Explanation: LinkedHashMap stores the data in the order of insertion.

6. What is the Output of following Java Program?

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
  
class Demo {
    public static void main(String[] args)
    {
        HashMap<String, Integer> hm
            = new HashMap<String, Integer>();
        hm.put("Karan", 1);
        hm.put("Deepak", 2);
        hm.put("Aman", 3);
        hm.put("Ayan", 4);
        TreeMap<String, Integer> hm1
            = new TreeMap<String, Integer>();
        hm1.putAll(hm);
        for (Map.Entry<String, Integer>
                 entry : hm1.entrySet())
            System.out.println(
                entry.getKey()
                + " " + entry.getValue());
    }
}

chevron_right


A.

Karan 1
Ayan 4
Deepak 2
Aman 3

B.

Karan 1
Deepak 2
Aman 3
Ayan 4



C.

Karan 1
Deepak 2
Ayan 4
Aman 3

D.

Aman 3
Ayan 4
Deepak 2
Karan 1

Answer: D.

Explanation: TreeMap stores data in the sorted order of key values.

7. What is the Output of following Java Program?

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
  
class Demo {
    public static void main(String[] args)
    {
        Stack<Integer> s1 = new Stack<Integer>();
        Queue<Integer> q = new LinkedList<Integer>();
        for (int i = 0; i < 5; i++)
            s1.push(i);
        while (s1.isEmpty())
            q.add(s1.pop());
        System.out.print(q.peek());
    }
}

chevron_right


A. 4
B. 0
C. Compilation Error
D. null

Answer: D.

Explanation: Since, no value is placed inside the queue, the condition of the while loop is false.

8. What is the Output of following Java Program?

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
  
class Demo {
    public static void main(String[] args)
    {
        HashSet<Integer> h1 = new HashSet<Integer>();
        h1.addAll(
            Arrays.asList(
                new Integer[] { 1, 2, 2,
                                3, 6, 4,
                                4, 0, 7, 5 }));
        h1.remove(1);
        h1.remove(2);
        h1.remove(4);
        for (int h : h1)
            System.out.print(h + " ");
    }
}

chevron_right


A. 1 3 4 4 0 7 5
B. 2 3 6 4 0 7 5
C. 0 3 5 6 7
D. 5 6 3 2 0 7 4



Answer: C.

Explanation: HashSet doesn’t contain any duplicate values. So 2 and 4 are stored only once. When the remove function is called, it removes the value that has been passed as parameter to the function.

9. What is the Output of following Java Program?

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
  
class Demo {
    public static void main(String[] args)
    {
        TreeSet<Integer> ts
            = new TreeSet<Integer>();
        ts.add(101);
        ts.add(76);
        ts.add(89);
        ts.add(7);
        System.out.print(ts.first() + " ");
        ts.remove(7);
        System.out.print(ts.last() + " ");
        System.out.print(ts.first() + " ");
    }
}

chevron_right


A. 101 89 101
B. 7 76 101
C. 7 101 76

Answer: C.

Explanation: TreeSet stores the values in Sorted order(ascending). Also, TreeSet.first() returns the smallest value and TreeSet.last() returns the largest value present in the set.

10. What is the Output of following Java Program?

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
  
class Demo {
    public static void main(String[] args)
    {
        Deque<Integer> dq
            = new LinkedList<Integer>();
        Stack<Integer> s1
            = new Stack<Integer>();
        dq.addFirst(10);
        dq.addFirst(20);
        dq.addLast(30);
        dq.addFirst(40);
        while (!dq.isEmpty())
            s1.push(dq.poll());
        while (!s1.isEmpty())
            System.out.print(s1.pop() + " ");
    }
}

chevron_right


A. 30 10 20 40
B. 40 30 20 10
C. Compilation Error.
D. 40 20 10 30

Answer: C.

Explanation: Since Deque is an interface, it can be instantiated with the help of LinkedList class.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.