← Quiz: Java Classes and Objects: all 10 questions
Quiz: Java Classes and Objects · Question 3 of 10
Why is this constructor buggy? Student(String name) { name = name; }
Why is this constructor buggy? Student(String name) { name = name; }
- A.Constructors can't take parameters
- B.It assigns the parameter to itself; the field stays null
- C.It should return a Student
- D.Strings can't be assigned
Answer & explanation
Correct answer: It assigns the parameter to itself; the field stays null
Without this, both names refer to the parameter. Use this.name = name.
Can you get the rest right without peeking?
Take the timed Quiz: Java Classes and Objects quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →