Java Constructor Overloading
Identify, using its signature, the correct constructor being called.
Consider the following class:
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 when the following code executes?
Student s1 = new Student("Alice", 16);
Student s2 = new Student("Bob", 17, 3.5);
System.out.println(s1.getName() + " " + s2.getName());
A
Alice 16 Bob 17
B
null null
C
Student Student
D
Alice Bob
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
APFIVE