Java OCA OCP Practice Question 2168

Question

What design pattern or principle is used in this class?.

public class Main { 
   int height; 
   public Main(int height) { 
      this.height = height; 
   } 
   public int getHeight() { 
      return height; 
   } 
} 
  • A. Encapsulation
  • B. Immutability
  • C. Singleton
  • D. Both A and B
  • E. None of the above


E.

Note

Option C is clearly incorrect because the class has a public constructor so cannot be a singleton.

Options A, B, and D are also incorrect because the height instance variable is not private.

This means other classes in the same package can read and change the value.

Therefore, Option E is correct.




PreviousNext

Related