What is the output of the following program:import java.util.*;public class LinkedList2 { public static void main(String args[]) { LinkedList<String> ll = new LinkedList<String>(); ll.add("Cat"); ll.add("Dog"); ll.add("Rabbit"); ll.remove(); System.out.println(ll.size()); Iterator i = ll.descendingIterator(); while (i.hasNext()) { System.out.println(i.next()); } }}Select one:a.2DogRabbitb.2DogCatc.2CatRabbitd.2CatDog
Question
What is the output of the following program:import java.util.*;public class LinkedList2 { public static void main(String args[]) { LinkedList<String> ll = new LinkedList<String>(); ll.add("Cat"); ll.add("Dog"); ll.add("Rabbit"); ll.remove(); System.out.println(ll.size()); Iterator i = ll.descendingIterator(); while (i.hasNext()) { System.out.println(i.next()); } }}Select one:a.2DogRabbitb.2DogCatc.2CatRabbitd.2CatDog
Solution
The output of the program will be:
2 Dog Rabbit
So, the correct answer is:
a. 2 Dog Rabbit
Here's the step by step explanation:
- A LinkedList of Strings is created and "Cat", "Dog", and "Rabbit" are added to it in that order.
- The remove() method is called without any arguments, which removes the first element of the list. So, "Cat" is removed.
- The size of the LinkedList is printed, which is now 2 because one element has been removed.
- A descendingIterator is created, which will iterate over the LinkedList in reverse order.
- The remaining elements of the LinkedList are printed in reverse order, which are "Dog" and "Rabbit".
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 code?import java.util.*;public class Main { public static void main(String[] args) { List list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); Iterator iterator = list.iterator(); while (iterator.hasNext()) { Integer value = iterator.next(); if (value == 2) { iterator.remove(); } } System.out.println(list); }}a)[2, 3]b)[3, 2, 1]c)[1, 2, 3]d)[1, 3]
What is the output of the following program:import java.util.ArrayList;class Main { public static void main(String[] args){ ArrayList<String> animals = new ArrayList<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); animals.remove(2); System.out.println("ArrayList: " + animals); }}Select one:a.ArrayList: [Dog]b.ArrayList: [Dog, Cat]c.ArrayList: [Dog, Cat, Horse]d.ArrayList: [Horse]
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
What would be the output of the following code?import java.util.LinkedList;import java.util.Queue;import java.util.Stack;class Test { public static void main (String[] args) { Queue<Integer> q=new LinkedList<Integer>(); Stack<Integer> s=new Stack<Integer>(); q.add(5); q.add(10); s.push(10); s.push(20); System.out.print(q.peek()+s.pop()); }}
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.