Encapsulation in Java Exercise I


Ques 1 Encapsulation


What is encapsulation in Java?
  a) A method of creating objects from classes
  b) A technique for hiding an object's internal state and requiring controlled access through methods
  c) A way to perform mathematical operations on objects
  d) A process of inheritance in object-oriented programming


b is the correct option




Ques 2 Encapsulation


What is the main benefit of encapsulation?
  a) To make methods and variables available to any class
  b) To restrict access to certain methods and variables for better control and security
  c) To make code execution faster
  d) To create a new instance of a class


b is the correct option




Ques 3 Encapsulation


Which Java access modifier is used to make a class or member accessible only within the same package?
  a) private
  b) protected
  c) package-private (default)
  d) public


c is the correct option




Ques 4 Encapsulation


In Java, which keyword is used to declare a private instance variable?
  a) var
  b) int
  c) private
  d) final


c is the correct option