What will be the output of the following Java program? [code] import java.util.*; class Collection_Algos { public

Julipkq

New member
What will be the output of the following Java program?
Code:
    import java.util.*;
    class Collection_Algos 
    {
        public static void main(String args[]) 
        {
            LinkedList list = new LinkedList();
            list.add(new Integer(2));
            list.add(new Integer(8));
            list.add(new Integer(5));
            list.add(new Integer(1));
            Iterator i = list.iterator();
            Collections.reverse(list);
	    while(i.hasNext())
	        System.out.print(i.next() + " ");
        }
    }
A. 2 8 5 1
B. 1 5 8 2
C. 2
D. 2 1 8 5
 
Answer: b
Explanation: Collections.reverse(list) reverses the given list, the list was 2->8->5->1 after reversing it became 1->5->8->2.
Output:
Code:
$ javac Collection_Algos.java
$ java Collection_Algos
1 5 8 2
This section of our 1000+ Java MCQs focuses on collection framework of Java Programming Language.
 

Tìm kiếm

Back
Top