Menu
Question Index
...

What will be the output of the program?

import java.util.*;
class Test{
    public static void main(String... arg){
        Queue myList = new ArrayList();
        int [] val ={1,2,3,4,5};
        for(int i = 0; i< val.length; i++){
            myList.add(new Integer(val[i]));
        }
        System.out.println("Before traversal through an iterator:"+ myList);
        Iterator iterator = myList.iterator();
        while(iterator.hasNext()){
            Integer element = (Integer)iterator.next();
            int value = element.intValue();
            if(value>4)
            iterator.remove();
        }
        System.out.print("After traversal through an iterator:"+myList);
    }
}


Before traversal through an iterator:[1,2,3,4,5]
After traversal through an iterator:[1,2,3,4]
Before traversal through an iterator:[1,2,3]
After traversal through an iterator:[1,2,3,4]
The program generates compile-time error.
Before traversal through an iterator:[1,2,3,4]
After traversal through an iterator:[1,2,3,4]
The program compiles successfully, but it gives run-time error.

Doubts

Problems

Topic: Java Iterator

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App