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.MercedesAudiBMWc.AudiMercedesBMWd.MercedesBMWAudi
Question
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.MercedesAudiBMWc.AudiMercedesBMWd.MercedesBMWAudi
Solution
The correct answer is:
a. BMW Audi Mercedes
This is because the descendingIterator() method of LinkedList class in Java is used to return an iterator that iterates the elements in reverse order. So it starts from the last element (BMW) to the first one (Mercedes).
Similar Questions
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.AudiMercedesBMWb.BMWAudiMercedesc.MercedesAudiBMWd.MercedesBMWAudi
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()); }}
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. 3b. 4c. nulld. 6
Both array and LinkedList in Java have indexes that allow elements to be accessed by their position within the collection. When you should choose to use LinkedList rather than array?AWhen you frequently perform addition and removal of data at any point of the collection.BWhen you frequently search for a value from the collection.CWhen you frequently update the existing values in the collection.DWhen you want print all values in the collection.
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.