Open In App

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

Last Updated : 04 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisites: Java Collection Framework.

1. What is the Output of following Java Program?




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) + " ");
    }
}


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?




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() + " ");
        }
    }
}


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?




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 + " ");
    }
}


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?




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() + " ");
        }
    }
}


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?




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());
    }
}


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?




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());
    }
}


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?




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());
    }
}


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?




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 + " ");
    }
}


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?




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() + " ");
    }
}


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?




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() + " ");
    }
}


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.



Similar Reads

Data Aggregation in Java using Collections Framework
Let's look at the problem before understanding what data aggregation is. You are given daily consumption of different types of candies by a candy-lover. The input is given as follows. Input Table: [caption width="800"]Data Before Aggregation[/caption] Desired Output Table: [caption width="800"]Data After Aggregation[/caption] Above is the aggregate
6 min read
Output of C programs | Set 66 (Accessing Memory Locations)
Q1. Is the output of this code True or False? #include &lt;stdio.h&gt; int main(void) { int b = 20; int* y = &amp;b; char n = 'A'; char* z = &amp;n; y[0] = z[0]; printf((*y == *z) ? &quot;True&quot; : &quot;False&quot;); } A. True B. False C. Program would crash D. Compilation error Answer: A. True Explanation: The binary form of 20 is 10100 which
7 min read
Output of C programs | Set 64 (Pointers)
Prerequisite : Pointers in C Question 1 : What will be the output of following program? C/C++ Code #include &amp;quot;stdio.h&amp;quot; int main() { char a[] = { 'A', 'B', 'C', 'D' }; char* ppp = &amp;amp;a[0]; *ppp++; // Line 1 printf(&amp;quot;%c %c &amp;quot;, *++ppp, --*ppp); // Line 2 } OPTIONS: a)C B b)B A c)B C d)C A OUTPUT: (d) C A Explanat
3 min read
Output of C programs | Set 43
1. What is the output of following program? #include &lt;stdio.h&gt; int main() { int a = 1, b = 2, c = 3; c = a == b; printf(&quot;%d&quot;, c); return 0; } Choose the correct answer: (A) 0 (B) 1 (C) 2 (D) 3 Answer : (A) Explanation : "==" is relational operator which returns only two values, either 0 or 1. 0: If a == b is false 1: If a == b is tr
2 min read
Output of C programs | Set 48
1. What is the output of following program? #include &lt;stdio.h&gt; #define square(x) (x * x) int main() { int x, y = 1; x = square(y + 1); printf(&quot;%d\n&quot;, x); return 0; } Options: (A) Error (B) 4 (C) 3 (D) Garbage value Answer : (C) Explanation : The macro function square(x)(x*x) calculates the square of the given number. Initially int y
3 min read
Output of C programs | Set 55
1. What will be the output of the following program? #include &lt;stdio.h&gt; int main() { int a = 03489; printf(&quot;%d&quot;, a); return (0); } Options: 1. 1865 2. runtime error 3. Syntax error 4. 0 The answer is option(3). Explanation: Any integral value prefix with 0 acts as octal number but the allowed digits is 0 to 7. But here 8 and 9 is th
3 min read
How to Compare two Collections in Java?
Java Collection provides an architecture to store and manipulate the group of objects. Here we will see how to compare Elements in a Collection in Java. Steps: Take both inputs with help of asList() function.Sort them using Collections.sort() method.Compare them using equals() function.Print output. (true means both are equal and false means both a
2 min read
Collections unmodifiableCollection() method in Java with Examples
The unmodifiableCollection() method of java.util.Collections class is used to return an unmodifiable view of the specified collection. This method allows modules to provide users with "read-only" access to internal collections. Query operations on the returned collection "read through" to the specified collection, and attempts to modify the returne
2 min read
Collections unmodifiableSortedMap() method in Java with Examples
The unmodifiableSortedMap() method of java.util.Collections class is used to return an unmodifiable view of the specified sorted map. This method allows modules to provide users with "read-only" access to internal sorted maps. Query operations on the returned sorted map "read through" to the specified sorted map. Attempts to modify the returned sor
2 min read
Collections unmodifiableSet() method in Java with Examples
The unmodifiableSet() method of java.util.Collections class is used to return an unmodifiable view of the specified set. This method allows modules to provide users with "read-only" access to internal sets. Query operations on the returned set "read through" to the specified set, and attempts to modify the returned set, whether direct or via its it
2 min read