Menu
Topics Index
...
`

Delete Failed Students


If you need explanation Read this topic

If you need Answer Take test on this topic

Delete Failed Students
What will be the output of the following program?
import java.util.ArrayList;

public class DeleteFailedStudents {
    public static void main(String[] args) {
        ArrayList students = new ArrayList();
        students.add(new Student("Raja", 23));
        students.add(new Student("Rajesh", 87));
        students.add(new Student("Rajani", 50));
        students.add(new Student("Rajanna", 35));
        students.add(new Student("Rajanikanth", 90));
        deleteFailedStudents(students);
        System.out.println(students);
    }
    public static void deleteFailedStudents(ArrayList<Student> students) {
        for (Student student : students) {
            if (student.marks < 35) {
                students.remove(student);
            }
        }
    }
}
class Student {
    String name; int marks;
    public Student(String name, int marks) { this.name = name; this.marks = marks; }
    public String toString() { return name + "-" + marks; }
}

A. [Rajesh-87, Rajani-50, Rajanna-35, Rajanikanth-90]
B. [Rajesh-87, Rajani-50, Rajanikanth-90]
C. [Rajesh-87, Rajani-50, Rajanna-35]
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java ArrayList

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is E..here we create the ArrayList which can store heterogeneous elements and growble in the
nature...and this can store  null values also and duplicates are allowed

Execution:
here we are travelling through the AL using for-each loop and here we will receive the objects into
the student variable and we are checking the marks in the Student object...if the marks are <35 than
we are removing the that Object from the Al..here ConcurrentModificationException is raise Bcz here we
are iterating through the for-each loop so we cant remove() the objects from the Al using for-each
loop..if we want remove elements we must use the iterator in order to remove the objects from the AL

Posted by Uday Kumar    2014-12-23 12:35:56


ans e. run time error. how to iterate through Java Collection class ArrayList. Also used best practice to avoid possible Out Of Memory (OOM) situation. fundamentals of Java Iterator and possible Introduced in the Java JDK 1.2 release, the java.util.Iterator interface allows the iteration of Collection classes. Each Iterator provides a next() and hasNext() method, and may optionally support a remove() method. Iterators are created by the corresponding container class, typically by a method named iterator(). It is an improvement over Enumeration interface.coming to this program we are not used iterator class to remove array elements. here we just use for loop iteration to remove element. We cannot add or remove elements to the underlying collection when we are using for loop iterator. so here concurrentModificationException get thrown

Posted by Maheshwari Natarajan    2014-12-24 01:23:07


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-24 04:03:54


© meritcampus 2019

All Rights Reserved.

Open In App