0%
0 / 15 answered
Constructors Practice Test
•15 QuestionsQuestion
1 / 15
Q1
In the Student code below, what value will the field id have after executing new Student("Maya")?
public class Student {
private String name;
private int id;
// Public constructor: sets both name and id
public Student(String name, int id) {
this.name = name;
this.id = id;
}
// Public constructor: sets name and defaults id to 0
public Student(String name) {
this(name, 0); // Chain to the two-parameter constructor
}
// Private constructor: internal helper for an unnamed student
private Student(int id) {
this("Unknown", id); // Chain to the two-parameter constructor
}
}
In the Student code below, what value will the field id have after executing new Student("Maya")?
public class Student {
private String name;
private int id;
// Public constructor: sets both name and id
public Student(String name, int id) {
this.name = name;
this.id = id;
}
// Public constructor: sets name and defaults id to 0
public Student(String name) {
this(name, 0); // Chain to the two-parameter constructor
}
// Private constructor: internal helper for an unnamed student
private Student(int id) {
this("Unknown", id); // Chain to the two-parameter constructor
}
}