My Report (&Account)

Java Collections Test – 1


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. What will be the output of the following Java program?

 
    import java.util.*;
    class Collection_iterators 
    {
        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() + " ");
        }
    }

2. Which of these iterators can be used only with List?

3. Which of these return type of hasNext() method of an iterator?

4. Which of these exceptions is thrown by remover() method?

5. Which of these is a method of ListIterator used to obtain index of previous element?

6. Which of these methods is used to obtain an iterator to the start of collection?

7. Which of these methods can be used to move to next element in a collection?

8. What will be the output of the following Java program?

 
    import java.util.*;
    class Collection_iterators 
    {
        public static void main(String args[]) 
        {
            ListIterator a = list.listIterator();
                if(a.previousIndex()! = -1)
                    while(a.hasNext())
	                System.out.print(a.next() + " ");
                else
                   System.out.print("EMPTY");
        }
    }

9. What will be the output of the following Java program?

 
    import java.util.*;
    class Collection_iterators 
    {
        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);
	    Collections.shuffle(list);
            i.next();
            i.remove();
            while(i.hasNext())
	        System.out.print(i.next() + " ");
        }
    }

10. What will be the output of the following Java program?

 
    import java.util.*;
    class Collection_iterators 
    {
        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);
	    Collections.sort(list);
            while(i.hasNext())
	        System.out.print(i.next() + " ");
        }
    }

 

Start practicing “1000 MCQs on Java”, and once you are ready, you can take tests on all topics by attempting our “Java Test Series”.

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.