← 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?
- A.Setters are not allowed
- B.It doesn't check the value, so invalid ages (like -5) can still be set
- C.age must be public
- 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 →