LearnFrenzy
Sign in Take a free test

Encapsulation · Exercise 1 of 4

Encapsulation Encapsulation MCQ

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twenty multiple-choice questions on encapsulation in Java: data hiding, getters and setters, validation, invariants, defensive copies, read-only properties and immutability. Pick an option to check your answer instantly.

Question 11 Medium

For which field type is a defensive copy unnecessary?

A int[]
B String (immutable)
C ArrayList<String>
D java.util.Date
Question page & discussion
Question 12 Medium

What is a class invariant?

A A final variable
B A condition that must be true for every object after construction and after every public method
C A static field
D An unchanging method name
Question page & discussion
Question 13 Medium

Why is generating a public setter for every private field poor encapsulation?

A It is slower
B It lets callers change any value at any time, so the fields are effectively public
C Setters cannot be public
D Fields must be final
Question page & discussion
Question 14 Medium

Which code follows "tell, don't ask"?

A acc.setBalance(acc.getBalance() - 100)
B acc.withdraw(100)
C if (acc.getBalance() > 100) acc.balance -= 100
D Account.balance -= 100
Question page & discussion
Question 15 Medium

Why is a public setStatus(String) method on an Order risky?

A Strings are slow
B Callers could jump to any status (e.g. CREATED to DELIVERED), skipping business rules
C It cannot be overridden
D It must be static
Question page & discussion
Question 16 Medium

A class stores price as double and later switches to paise in a long. With proper encapsulation, what changes for callers?

A Every caller must be rewritten
B Nothing, if the public methods keep the same signatures and behaviour
C Callers must recompile with a flag
D Callers must use reflection
Question page & discussion
Question 17 Medium

Which is a good example of a write-only property?

A A user's name
B A password that is stored as a hash and never returned
C An order total
D A product price
Question page & discussion
Question 18 Medium

Why might a constructor call the class's own (final or private) validating setters?

A Constructors cannot assign fields
B To reuse the same validation, so objects are valid from the start
C For speed
D To make the fields public
Question page & discussion
Question 19 Easy

Do simple getters and setters make Java programs noticeably slower?

A Yes, every call is expensive
B No – the JIT compiler inlines small methods
C Only in loops
D Only for static fields
Question page & discussion
Question 20 Medium

What does record Point(int x, int y) {} generate?

A Public mutable fields
B Private final fields, a constructor, accessors x() and y(), equals, hashCode and toString
C Getters and setters
D Only a constructor
Question page & discussion
Up next · Exercise 2Predict the Output20 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links