Java Constructor Overloading
Consider the following class definition.
public class Student {
private String name;
private int age;
private double gpa;
public Student(String n, int a) {
name = n;
age = a;
gpa = 0.0;
}
public Student(String n, int a, double g) {
name = n;
age = a;
gpa = g;
}
public String getName() {
return name;
}
}
What is printed as a result of executing the following code segment?
Student s1 = new Student("Alice", 16);
Student s2 = new Student("Bob", 17, 3.5);
System.out.println(s1.getName() + " " + s2.getName());
A
Alice Bob
B
null null
C
Student Student
D
Alice 16 Bob 17
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | kaisuki | 1 | 1 | 0m 00s | 100 |
| #2 | zgj07310417 | 1 | 1 | 3m 08s | -88 |
| #3 | lsj08030922 | 1 | 1 | 39m 12s | -2,252 |
Items per page:
10
1 – 3 of 3
