CS & IT
ENGINEERING
Java With OOPS
Java Collections
Discussion Notes
DPP- 01 By- Aditya sir
Topics to be covered
Topic DPP Questions Discussion
Hashset
Stack Que
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test1 {
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<>(Arrays.asList(5, 2, 9, 1, 5, 6));
Collections.sort(a);
HashSet<Integer> s = new HashSet<>(a);
System.out.println(a.get(3) + s.size()); Ans 1
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test2 {
public static void main(String[] args) {
LinkedList<Integer> l = new LinkedList<>(Arrays.asList(10, 20, 30, 40, 50));
Collections.reverse(l);
PriorityQueue<Integer> pq = new PriorityQueue<>(l);
ArrayList<Integer> al = new ArrayList<>();
while(!pq.isEmpty()){
al.add(pq.poll());
}
Ans 30
System.out.println(al.get(2));
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test3 {
public static void main(String[] args) { actus 2
int[] arr = {3, 1, 4, 1, 5, 9, 2, 6};
HashMap<Integer,Integer> hm = new HashMap<>();
for(int i : arr){
hm.put(i, hm.getOrDefault(i,0)+1);
}
ArrayList<Integer> al = new ArrayList<>(hm.values());
Collections.sort(al);
System.out.println(al.get(al.size()-1));
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
aus 16
public class Test4 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(2, 3, 5, 7, 11, 13));
Queue<Integer> q = new LinkedList<>(list);
Stack<Integer> st = new Stack<>();
while(!q.isEmpty()){
st.push(q.poll());
}
Collections.sort(list);
System.out.println(st.pop() + list.get(1));
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*; ans alephan
public class Test5 {
public static void main(String[] args) {
LinkedList<String> ll = new LinkedList<>(Arrays.asList("delta", "alpha",
"charlie", "bravo"));
Collections.sort(ll);
HashSet<String> hs = new HashSet<>(ll);
PriorityQueue<String> pq = new PriorityQueue<>(ll);
System.out.println(pq.poll() + hs.size());
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test6 { ans 1
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<>(Arrays.asList(8, 3, 8, 2, 5, 3));
a.remove(new Integer(8));
Collections.reverse(a);
HashMap<Integer,Integer> m = new HashMap<>();
for(int i : a) {
m.put(i, m.getOrDefault(i,0)+1);
}
System.out.println(m.get(a.get(2)));
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test7 {
public static void main(String[] args) {
Stack<Integer> st = new Stack<>();
int[] arr = {4, 7, 10, 15, 20};
for(int n : arr){ and
if(n % 2 == 0)
st.push(n);
}
PriorityQueue<Integer> pq = new PriorityQueue<>();
while(!st.isEmpty()){
pq.offer(st.pop());
}
System.out.println(pq.poll());
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
an 20
public class Test8 {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<>(Arrays.asList(100, 80, 60, 40, 20));
Collections.sort(al, Collections.reverseOrder());
PriorityQueue<Integer> pq = new PriorityQueue<>(al);
System.out.println(pq.poll());
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test9 { Ans 4
public static void main(String[] args) {
int[] arr = {7, 5, 7, 3, 3, 5, 2};
HashMap<Integer,Integer> hm = new HashMap<>();
for(int i : arr)
hm.put(i, hm.getOrDefault(i,0)+1);
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
for(int v : hm.values())
pq.offer(v);
System.out.println(pq.poll() + pq.poll());
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test10 {
air 30
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(9, 8, 7, 6, 5));
Collections.sort(list);
HashSet<Integer> set = new HashSet<>(list);
System.out.println(list.get(1) * set.size());
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test11 {
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<>(Arrays.asList(11, 22, 33, 44));
HashMap<Integer,Integer> hm = new HashMap<>();
for(int i : a)
hm.put(i, i / 11);
Queue<Integer> q = new LinkedList<>(a);
Stack<Integer> st = new Stack<>();
while(!q.isEmpty()){
int temp = q.poll();
if(hm.get(temp) > 2)
as 4
st.push(hm.get(temp));
}
System.out.println(st.pop());
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test12 {
public static void main(String[] args) {
LinkedList<String> ll = new LinkedList<>(Arrays.asList("gamma", "alpha",
"epsilon", "beta"));
Collections.sort(ll);
PriorityQueue<Character> pq = new PriorityQueue<>();
for(String s : ll) {
pq.offer(s.charAt(s.length()-1));
} aus 92
HashSet<Character> hs = new HashSet<>(pq);
System.out.println(pq.poll() + "" + hs.size());
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test13 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(5, 10, 15, 20, 25));
HashMap<Integer,Integer> map = new HashMap<>();
for(int i : list)
map.put(i, i + 5);
Queue<Integer> q = new LinkedList<>(list);
Stack<Integer> st = new Stack<>();
while(!q.isEmpty()){ ans 10
int x = q.poll();
if(map.get(x) % 10 == 0)
st.push(map.get(x));
}
System.out.println(st.pop() - st.peek());
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test14 { ans 9
public static void main(String[] args) {
LinkedList<Integer> l = new LinkedList<>(Arrays.asList(12, 5, 7, 5, 9, 3));
ArrayList<Integer> al = new ArrayList<>(l);
Collections.sort(al);
l.removeFirst();
HashMap<Integer,Integer> hm = new HashMap<>();
for(int i : l)
hm.put(i, hm.getOrDefault(i, 0) + 1);
System.out.println(al.get(3) + hm.getOrDefault(5, 0));
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
ans
41
public class Test15 {
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
pq.offer(2);
pq.offer(4);
pq.offer(6);
pq.offer(8);
System.out.println(pq.poll() * pq.poll());
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*; 2
aus
public class Test16 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 1, 4, 5, 4};
HashMap<Integer,Integer> freq = new HashMap<>();
for(int i : arr)
freq.put(i, freq.getOrDefault(i, 0) + 1);
HashSet<Integer> hs = new HashSet<>();
for(int i : arr)
hs.add(i * i);
ArrayList<Integer> al = new ArrayList<>(hs);
Collections.sort(al);
System.out.println(al.get(1) - freq.get(2));
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test17 {
aus 17
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<>(Arrays.asList(3, 6, 9, 12, 15));
LinkedList<Integer> ll = new LinkedList<>(Arrays.asList(2, 4, 6, 8, 10));
al.addAll(ll);
Collections.sort(al);
PriorityQueue<Integer> pq = new PriorityQueue<>(al);
System.out.println(pq.poll() + al.get(al.size()-1));
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test18 { ans
public static void main(String[] args) {
HashMap<String,Integer> hm = new HashMap<>();
921
hm.put("x", 10);
hm.put("y", 20);
hm.put("z", 30);
ArrayList<String> keys = new ArrayList<>(hm.keySet());
Collections.sort(keys);
LinkedList<Integer> ll = new LinkedList<>(hm.values());
Collections.sort(ll);
System.out.println(keys.get(1) + " " + ll.get(1));
}
}
[NAT]
#Q. What is the output of the given code below?
import java.util.*;
public class Test19 {
public static void main(String[] args) {
LinkedList<Integer> l = new LinkedList<>(Arrays.asList(50, 40, 30, 20, 10));
Stack<Integer> s = new Stack<>();
while(!l.isEmpty()){
s.push(l.removeLast());
}
while(!s.isEmpty()){
Ans 50
if(s.peek() % 15 == 0)
s.pop();
else break;
}
System.out.println(s.peek());
}
}
[NAT]
#Q. What is the output of the given code below?
aus 14
import java.util.*;
public class Test20 {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<>(Arrays.asList(7, 14, 21, 28, 35));
Collections.sort(al, (a, b) -> b - a);
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
pq.addAll(al);
int res = al.get(2) - pq.poll();
System.out.println(res);
}
}
THANK - YOU