Q.- Given:-
public 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);
}
}
}
What is the result?
A) Richard
Donald
B) A NullPointerException is thrown at runtime.
C) null
Richard
Donald
D) An ArrayIndexOutOfBoundsException is thrown at runtime.
E) Compilation fails.
⧪ please give your answer in a comment section.
public 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);
}
}
}
What is the result?
A) Richard
Donald
B) A NullPointerException is thrown at runtime.
C) null
Richard
Donald
D) An ArrayIndexOutOfBoundsException is thrown at runtime.
E) Compilation fails.
⧪ please give your answer in a comment section.
4 Comments
E
ReplyDeleteB
ReplyDeleteA
ReplyDeleteB Student[0] is null so "main" java.lang.NullPointerException: Cannot read field "name" because "s" is null
ReplyDelete