Answer is E:
class Student{
String name;
public Student(String name) {
this.name = name;
}
}
public class Test {
public static void main(String[] args) {
Student[] students = new Student[3];
students[1] = new Student("Richard");
students[2] = new Student("Donald");
for (Student s : students){
System.out.println("" + s.name);
}
}
}
The first index of the array are instantiated as 'null' because Student is an object. c
Calling the variable of a null object results NullPointerException.
The answer is the letter E, If you try to System.out.print(array[0]) the result will be: null
but in this case the for:each loop try to access the first element result in a NullException
If you try to System.out.print(array[0]) the result will be: null
but in this case the for:each loop try to access the first element result in a NullException
Answer is agreed. But not sure why its throwing an null pointer exception. As we know
if any method called on null reference it will throw null pointer exception but case here not sure.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
Tarik2190
Highly Voted 1 year, 11 months agoakbiyik
Most Recent 1 month, 2 weeks agoiSnover
3 months, 1 week agoAyla
2 years, 2 months agomassigirello
2 years, 4 months agoHarid
2 years, 4 months agoSamAru
2 years, 6 months agonswati2019
2 years, 6 months agov323rs
2 years, 12 months agoDJava
3 years, 7 months agopawankalyan
3 years, 7 months ago