🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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; }

  1. A.Constructors can't take parameters
  2. B.It assigns the parameter to itself; the field stays null
  3. C.It should return a Student
  4. 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 →