🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Encapsulation and Abstraction: all 10 questions

Quiz: Encapsulation and Abstraction · Question 5 of 10

Why is a setter like setAge(int a) { age = a; } weak encapsulation?

Why is a setter like setAge(int a) { age = a; } weak encapsulation?

  1. A.Setters are not allowed
  2. B.It doesn't check the value, so invalid ages (like -5) can still be set
  3. C.age must be public
  4. D.It should return a value
Answer & explanation

Correct answer: It doesn't check the value, so invalid ages (like -5) can still be set

Real encapsulation enforces rules inside the method.

Can you get the rest right without peeking?

Take the timed Quiz: Encapsulation and Abstraction quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →