4/16/25, 6:33 PM GATE_Computer Science & Information Technology_DPP 2
GATE
Computer Science & Information Technology
JAVA with OOPS
DPP: 1
Java Collections
Q1 What is the output of the given code below? [Link](i, [Link](i, 0) + 1);
import [Link].*; }
public class Test1 {
public static void main(String[] args) { ArrayList al = new ArrayList<>([Link]());
ArrayList<Integer> a = new ArrayList<> [Link](al);
([Link](5, 2, 9, 1, 5, 6));
[Link](a); [Link]([Link]([Link]() - 1));
HashSet<Integer> s = new HashSet<>(a); }
[Link]([Link](3) + [Link]()); }
}
Q4 What is the output of the given code below?
}
import [Link].*;
Q2 What is the output of the given code below? public class Test4 {
import [Link].*; public static void main(String[] args) {
public class Test2 { ArrayList list = new ArrayList<>
public static void main(String[] args) { ([Link](2, 3, 5, 7, 11, 13));
LinkedList<Integer> l = new LinkedList<> Queue q = new LinkedList<>(list);
([Link](10, 20, 30, 40, 50)); Stack st = new Stack<>();
[Link](l);
PriorityQueue<Integer> pq = new while (![Link]()) {
PriorityQueue<>(l); [Link]([Link]());
ArrayList<Integer> al = new ArrayList<>(); }
while (![Link]()) {
[Link]([Link]()); [Link](list);
} [Link]([Link]() + [Link](1));
[Link]([Link](2)); }
} }
}
Q5 What is the output of the given code below?
Q3 What is the output of the given code below? import [Link].*;
import [Link].*; public class Test5 {
public class Test3 { public static void main(String[] args) {
public static void main(String[] args) { LinkedList ll = new LinkedList<>
int[] arr = {3, 1, 4, 1, 5, 9, 2, 6}; ([Link]("delta", "alpha", "charlie", "bravo"));
HashMap<Integer, Integer> hm = new [Link](ll);
HashMap<>(); HashSet hs = new HashSet<>(ll);
PriorityQueue pq = new PriorityQueue<>(ll);
for (int i : arr) {
Android App | iOS App | PW Website
[Link] 1/8
4/16/25, 6:33 PM GATE_Computer Science & Information Technology_DPP 2
GATE
[Link]([Link]() + [Link]()); [Link](al,
} [Link]());
} PriorityQueue pq = new PriorityQueue<>(al);
[Link]([Link]());
Q6 What is the output of the given code below?
}
import [Link].*;
}
public class Test6 {
public static void main(String[] args) { Q9 What is the output of the given code below?
ArrayList a = new ArrayList<>([Link](8, import [Link].*;
3, 8, 2, 5, 3)); public class Test9 {
[Link](new Integer(8)); public static void main(String[] args) {
[Link](a); int[] arr = {7, 5, 7, 3, 3, 5, 2};
HashMap<Integer, Integer> m = new HashMap<Integer, Integer> hm = new
HashMap<>(); HashMap<>();
for (int i : a) {
[Link](i, [Link](i, 0) + 1); for (int i : arr)
} [Link](i, [Link](i, 0) + 1);
[Link]([Link]([Link](2)));
} PriorityQueue pq = new PriorityQueue<>
} ([Link]());
Q7 What is the output of the given code below?
for (int v : [Link]())
import [Link].*;
[Link](v);
public class Test7 {
public static void main(String[] args) {
[Link]([Link]() + [Link]());
Stack st = new Stack<>();
}
int[] arr = {4, 7, 10, 15, 20};
}
for (int n : arr) {
if (n % 2 == 0) Q10 What is the output of the given code below?
[Link](n); import [Link].*;
} public class Test10 {
PriorityQueue pq = new PriorityQueue<>(); public static void main(String[] args) {
while (![Link]()) { LinkedList list = new LinkedList<>
[Link]([Link]()); ([Link](9, 8, 7, 6, 5));
} [Link](list);
[Link]([Link]()); HashSet set = new HashSet<>(list);
} [Link]([Link](1) * [Link]());
} }
}
Q8 What is the output of the given code below?
import [Link].*; Q11 What is the output of the given code below?
public class Test8 { import [Link].*;
public static void main(String[] args) { public class Test11 {
ArrayList al = new ArrayList<> public static void main(String[] args) {
([Link](100, 80, 60, 40, 20)); ArrayList a = new ArrayList<>([Link](11,
Android App | iOS App | PW Website
[Link] 2/8
4/16/25, 6:33 PM GATE_Computer Science & Information Technology_DPP 2
GATE
22, 33, 44)); ArrayList list = new ArrayList<>
HashMap<Integer, Integer> hm = new ([Link](5, 10, 15, 20, 25));
HashMap<>(); HashMap<Integer, Integer> map = new
HashMap<>();
for (int i : a)
[Link](i, i / 11); for (int i : list)
[Link](i, i + 5);
Queue q = new LinkedList<>(a);
Stack st = new Stack<>(); Queue q = new LinkedList<>(list);
Stack st = new Stack<>();
while (![Link]()) {
int temp = [Link](); while (![Link]()) {
if ([Link](temp) > 2) int x = [Link]();
[Link]([Link](temp)); if ([Link](x) % 10 == 0)
} [Link]([Link](x));
}
[Link]([Link]());
} [Link]([Link]() - [Link]());
} }
}
Q12 What is the output of the given code below?
import [Link].*; Q14 What is the output of the given code below?
public class Test12 { import [Link].*;
public static void main(String[] args) { public class Test14 {
LinkedList ll = new LinkedList<> public static void main(String[] args) {
([Link]("gamma", "alpha", "epsilon", "beta")); LinkedList l = new LinkedList<>
[Link](ll); ([Link](12, 5, 7, 5, 9, 3));
ArrayList al = new ArrayList<>(l);
PriorityQueue pq = new PriorityQueue<>(); [Link](al);
[Link]();
for (String s : ll) {
[Link]([Link]([Link]() - 1)); HashMap<Integer, Integer> hm = new
} HashMap<>();
for (int i : l)
HashSet hs = new HashSet<>(pq); [Link](i, [Link](i, 0) + 1);
[Link]([Link]() + "" + [Link]()); [Link]([Link](3) +
} [Link](5, 0));
} }
}
Q13 What is the output of the given code below?
import [Link].*; Q15 What is the output of the given code below?
public class Test13 { import [Link].*;
public static void main(String[] args) {
Android App | iOS App | PW Website
[Link] 3/8
4/16/25, 6:33 PM GATE_Computer Science & Information Technology_DPP 2
GATE
public class Test15 { [Link](ll);
public static void main(String[] args) { [Link](al);
PriorityQueue pq = new PriorityQueue<>((a,
b) -> b - a); PriorityQueue pq = new PriorityQueue<>(al);
[Link](2);
[Link](4); [Link]([Link]() + [Link]([Link]() -
[Link](6); 1));
[Link](8); }
}
[Link]([Link]() * [Link]());
Q18 What is the output of the given code below?
}
import [Link].*;
}
public class Test18 {
Q16 What is the output of the given code below? public static void main(String[] args) {
import [Link].*; HashMap<String, Integer> hm = new
public class Test16 { HashMap<>();
public static void main(String[] args) { [Link]("x", 10);
int[] arr = {1, 2, 3, 2, 1, 4, 5, 4}; [Link]("y", 20);
HashMap<Integer, Integer> freq = new [Link]("z", 30);
HashMap<>();
ArrayList keys = new ArrayList<>
for (int i : arr) ([Link]());
[Link](i, [Link](i, 0) + 1); [Link](keys);
HashSet hs = new HashSet<>(); LinkedList ll = new LinkedList<>([Link]());
for (int i : arr) [Link](ll);
[Link](i * i);
[Link]([Link](1) + " " + [Link](1));
ArrayList al = new ArrayList<>(hs); }
[Link](al); }
Q19 What is the output of the given code below?
[Link]([Link](1) - [Link](2));
import [Link].*;
}
public class Test19 {
}
public static void main(String[] args) {
Q17 What is the output of the given code below? LinkedList l = new LinkedList<>
import [Link].*; ([Link](50, 40, 30, 20, 10));
public class Test17 { Stack s = new Stack<>();
public static void main(String[] args) {
ArrayList al = new ArrayList<>([Link](3, while (![Link]()) {
6, 9, 12, 15)); [Link]([Link]());
LinkedList ll = new LinkedList<> }
([Link](2, 4, 6, 8, 10));
while (![Link]()) {
Android App | iOS App | PW Website
[Link] 4/8
4/16/25, 6:33 PM GATE_Computer Science & Information Technology_DPP 2
GATE
if ([Link]() % 15 == 0) ArrayList al = new ArrayList<>([Link](7,
[Link](); 14, 21, 28, 35));
else [Link](al, (a, b) -> b - a);
break;
} PriorityQueue pq = new PriorityQueue<>
([Link]());
[Link]([Link]()); [Link](al);
}
} int res = [Link](2) - [Link]();
Q20 What is the output of the given code below?
[Link](res);
import [Link].*;
}
public class Test20 {
}
public static void main(String[] args) {
Android App | iOS App | PW Website
[Link] 5/8
4/16/25, 6:33 PM GATE_Computer Science & Information Technology_DPP 2
GATE
Answer Key
Q1 10 Q11 4
Q2 30 Q12 a2
Q3 2 Q13 10
Q4 16 Q14 9
Q5 alpha4 Q15 48
Q6 1 Q16 2
Q7 4 Q17 17
Q8 20 Q18 y 20
Q9 4 Q19 50
Q10 30 Q20 -14
Android App | iOS App | PW Website
[Link] 6/8
4/16/25, 6:33 PM GATE_Computer Science & Information Technology_DPP 2
GATE
Hints & Solutions
Q1 Text Solution: Popping from the stack reverses their order;
The sorted list becomes [1, 2, 5, 5, 6, 9] so [Link](3) inserting them into the PriorityQueue
returns 5. reorganizes them in ascending order, so poll
The HashSet contains the unique values [1, 2, 5, 6, returns 4.
9] (size 5).
Q8 Text Solution:
Their sum is 5 + 5 = 10.
Even though the ArrayList is sorted in
Q2 Text Solution: descending order, constructing the
Reversing the LinkedList does not affect the PriorityQueue reorders the elements in natural
PriorityQueue, which reorders the elements in ascending order.
natural (ascending) order: [10, 20, 30, 40, 50]. Thus, the smallest element, 20, is returned when
Polling these into an ArrayList results in the third polled.
element (index 2) being 30.
Q9 Text Solution:
Q3 Text Solution: The frequency counts are: 7 → 2, 5 → 2, 3 → 2,
The frequency map shows that 1 occurs twice and 2 → 1.
while every other number occurs once. In the max-heap, the top two frequencies are 2
Sorting the frequency values gives [1, 1, 1, 1, 1, 2] and 2, and their sum is 4.
so the highest frequency is 2, which is the
Q10 Text Solution:
output.
After sorting, the list is [5, 6, 7, 8, 9] so [Link](1)
Q4 Text Solution: returns 6 and the HashSet has 5 unique
The queue is emptied into the stack in the order elements.
of the list, so the stack’s top becomes 13. Multiplying these gives 6 × 5 = 30.
The sorted list remains [2, 3, 5, 7, 11, 13], where
Q11 Text Solution:
[Link](1) is 3. Their sum is 13 + 3 = 16.
Mapping divides each element by 11, giving {11→1,
Q5 Text Solution: 22→2, 33→3, 44→4}.
Sorting the LinkedList gives [alpha, bravo, charlie, Only 33 and 44 satisfy the condition (>2) and are
delta]. pushed onto the stack in order; [Link]() returns
The PriorityQueue also uses natural string order 4.
so poll returns "alpha".
Q12 Text Solution:
The HashSet size is 4, and concatenating yields
After sorting, the list becomes [alpha, beta,
"alpha4".
epsilon, gamma].
Q6 Text Solution: The last characters of each are 'a', 'a', 'n', and 'a'.
After removing the first 8, the list becomes [3, 8, The PriorityQueue returns 'a' and the HashSet
2, 5, 3]. has 2 unique characters ('a' and 'n'), producing
Reversing it yields [3, 5, 2, 8, 3]. "a2".
The frequency of the element at index 2 (which is
Q13 Text Solution:
2) is 1, so the output is 1.
Mapping results in: 5→10, 10→15, 15→20, 20→25,
Q7 Text Solution: 25→30.
Even numbers from the array (4, 10, 20) are Only 10, 20, and 30 are divisible by 10; they are
pushed onto the stack (in that order). pushed in order, so the top two are 30 and 20.
Android App | iOS App | PW Website
[Link] 7/8
4/16/25, 6:33 PM GATE_Computer Science & Information Technology_DPP 2
GATE
Their difference is 30 - 20 = 10. The smallest element is 2 (from [Link]()) and the
Q14 Text Solution: largest is 15, summing to 17.
Initially, l is [12, 5, 7, 5, 9, 3] and al (sorted) Q18 Text Solution:
becomes [3, 5, 5, 7, 9, 12]. The sorted keys become [x, y, z] so [Link](1) is
Removing the first element of l yields [5, 7, 5, 9, 3] "y".
where 5 appears twice. [Link](3) returns 7, and 7 + The values [10, 20, 30] sorted remain the same,
2 equals 9. making [Link](1) equal to 20.
The output is the concatenation "y 20".
Q15 Text Solution:
Using a custom comparator, the PriorityQueue Q19 Text Solution:
acts as a max-heap. Removing from the end pushes elements into the
Polling returns the top two elements 8 and 6; stack in the order [10, 20, 30, 40, 50].
their product is 8 * 6 = 48. The loop checks if the top is divisible by 15; 50 %
15 is not 0 so the loop breaks immediately,
Q16 Text Solution:
leaving 50 at the top.
Frequencies: 1→2, 2→2, 3→1, 4→2, 5→1.
The unique squares are {1, 4, 9, 16, 25} which Q20 Text Solution:
sorted give [1, 4, 9, 16, 25]; [Link](1) is 4 and Sorting in descending order gives [35, 28, 21, 14,
[Link](2) is 2, so 4 - 2 = 2. 7] so [Link](2) is 21.
Q17 Text Solution: The PriorityQueue (maxheap) returns 35 when
After merging and sorting, the combined list is [2, polled.
3, 4, 6, 6, 8, 9, 10, 12, 15]. The difference 21 - 35 equals -14.
Android App | iOS App | PW Website
[Link] 8/8