Knowee
Questions
Features
Study Tools

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()); } }}

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()); } }}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution 1

The output of the program will be:

2 Dog Rabbit

Here's the step by step explanation:

  1. A LinkedList of Strings is created and three elements ("Cat", "Dog", "Rabbit") are added to it.

  2. The remove() method is called without any arguments, which removes the first element of the list. So, "Cat" is removed.

  3. The size() method is called, which returns the number of elements in the list. Since "Cat" was removed, only "Dog" and "Rabbit" remain, so the size is 2. This is printed to the console.

  4. A descendingIterator is created. This is an iterator that will iterate over the LinkedList in reverse order.

  5. A while loop is used to iterate over the LinkedList. Inside the loop, the next() method is called on the iterator, which returns the next element in the iteration. This element is then printed to the console.

  6. Since the iterator is a descendingIterator, the elements are printed in reverse order. So, "Dog" is printed first, then "Rabbit".

This problem has been solved

Solution 2

The output of the program will be:

2 Dog Rabbit

Here's the step by step explanation:

  1. A LinkedList of Strings is created and three elements ("Cat", "Dog", "Rabbit") are added to it.

  2. The remove() method is called without any arguments, which removes the first element of the list. So, "Cat" is removed.

  3. The size() method is called which returns the number of elements in the list. After the removal of "Cat", there are 2 elements left, so it prints "2".

  4. A descendingIterator is created. This iterator iterates over the elements in reverse order.

  5. A while loop is used to print all the elements of the LinkedList using the descendingIterator. So, it prints "Dog" and "Rabbit", in that order.

This problem has been solved

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()); }}

1/3

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.