Java OCA OCP Practice Question 1468

Question

What change is needed to make MyClass well encapsulated?

import java.util.*; 

public class MyClass { 
   private int number = new Random().nextInt(10); 
   public boolean guess(int candidate) { 
      return number == candidate; 
   } 
} 
  • A. Change number to use a public access modifier.
  • B. Declare a private constructor.
  • C. Remove the guess method.
  • D. None. It is already well encapsulated.


D.

Note

This class is a good example of encapsulation.

It has a private instance variable and is accessed by a public method.

No changes are needed to encapsulate it, and Option D is correct.




PreviousNext

Related