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()); }}
Question
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()); }}
Solution
The output of the code would be "11 3".
Here's the step by step explanation:
-
A Queue of Integer type is created using LinkedList. The Queue interface is implemented by LinkedList class.
-
A for loop is used to add elements to the queue. The loop runs from i=1 to i<5, so four elements are added to the queue: 11, 12, 13, and 14.
-
The poll() method is used to remove and return the head of the queue. Since queues follow the FIFO (First In First Out) principle, the first element added (11) is removed and printed.
-
The size() method is used to return the number of elements in the queue. Since one element has been removed from the original four, the size of the queue is now 3.
So, the output is "11 3".
Similar Questions
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>(); 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.*;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
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()); } }}
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.