What is the output of the following code?import java.util.*;public class Main { public static void main(String[] args) { Queue queue = new LinkedList<>(); queue.add(1); queue.add(2); queue.add(3); System.out.println(queue.poll()); System.out.println(queue.poll()); System.out.println(queue.poll()); }}a)3 1 2b)3 2 1c)1 3 2d)1 2 3
Question
What is the output of the following code?import java.util.*;public class Main { public static void main(String[] args) { Queue queue = new LinkedList<>(); queue.add(1); queue.add(2); queue.add(3); System.out.println(queue.poll()); System.out.println(queue.poll()); System.out.println(queue.poll()); }}a)3 1 2b)3 2 1c)1 3 2d)1 2 3
Solution
The output of the code will be:
1 2 3
So, the correct answer is d) 1 2 3
Here's the step by step explanation:
-
A Queue is created using LinkedList in Java. Queue follows the FIFO (First In First Out) rule.
-
The numbers 1, 2, and 3 are added to the queue in that order.
-
The poll() method is used to remove and return the head of the queue. It returns null if the queue is empty.
-
So, when the first poll() is called, it removes and returns '1', which is the head of the queue.
-
The second poll() call removes and returns '2'.
-
The third poll() call removes and returns '3'.
-
Therefore, the output of the code is 1, 2, 3.
Similar Questions
What would be the output of the following code?import java.util.LinkedList;import java.util.Queue;class Test { public static void main (String[] args) { Queue<Integer> q=new LinkedList<Integer>(); for(int i=1;i<5;i++) { q.add(10+i); } System.out.print(q.poll()+" "+q.size()); }}
What is the output of the following program:import java.util.*;class Queue1 { public static void main(String args[]) { PriorityQueue<String> queue = new PriorityQueue<String>(); queue.add("Amit"); queue.add("Vijay"); queue.add("Karan"); System.out.println("Printing queue elements:"); Iterator itr = queue.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } queue.remove(); System.out.println("Printing queue elements:"); Iterator<String> itr2 = queue.iterator(); while (itr2.hasNext()) { System.out.println(itr2.next()); } }}
What is the output of the following program:import java.util.*;public class linked_list { public static void main(String args[]) { LinkedList<String> ll = new LinkedList<String>(); ll.add("Mercedes"); ll.add("Audi"); ll.add("BMW"); Iterator i = ll.descendingIterator(); while (i.hasNext()) { System.out.println(i.next()); } }}Select one:a.BMWAudiMercedesb.AudiMercedesBMWc.MercedesBMWAudid.MercedesAudiBMW
What is the output of the following program:import java.util.*;public class linked_list_size { public static void main(String args[]) { LinkedList<String> ll = new LinkedList<String>(); ll.add("Cat"); ll.add("Dog"); ll.add("Rabbit"); System.out.println(ll.size()); }}Select one:a. 6b. 3c. nulld. 4
What is the output of the following program:import java.util.*;class Queue1 { public static void main(String args[]) { PriorityQueue<String> queue = new PriorityQueue<String>(); System.out.println(queue.element()); }}Select one:a. None of the choicesb. Program will throw an exceptionc. No outputd. n
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.