Instructions

  • 1. Your final score will reflect your grasp of the concepts—approach each question with precision.
  • 2. Thoroughly review each solution before proceeding to ensure full understanding.
  • 3. Final results will be available after submission to provide insights into areas for further improvement.
  • 4. Maintain academic integrity—plagiarism undermines learning and professional growth.
  • 5. Once submitted, responses are final, so ensure you’re confident in your answers.
  • 6. These challenges are designed to test practical knowledge; apply your skills as you would in real-world scenarios.

All Problems

Question

Action

1. What is encapsulation in Java?

View

2. Which access modifier allows a variable or method to be accessed within the same class only?

View

3. Which of the following ensures encapsulation in Java?

View

4. Consider the following code. Which statement is correct?

View

5. Which access modifier allows a method to be accessed from any class in the program?

View

6. Which of the following is true about the protected access modifier?

View

7. Consider the following code snippet. What will happen if you try to access data from another class in a different package? java Copy code

View

8. What is the purpose of getter and setter methods in Java?

View

9. Which access modifier should you use for maximum encapsulation?

View

10. Which of the following is NOT an advantage of encapsulation?

View

1. What is encapsulation in Java?

Hiding data and restricting access to methods and variables.
Inheriting the properties of a class.
Creating multiple methods with the same name.
None of the above.

2. Which access modifier allows a variable or method to be accessed within the same class only?

protected
private
public
default

3. Which of the following ensures encapsulation in Java?

Declaring variables as private.
Providing public getter and setter methods.
Using access modifiers to control visibility.
All of the above.

4. Consider the following code. Which statement is correct?

<p>class Person {<br>&nbsp; &nbsp; private String name;<br><br>&nbsp; &nbsp; public String getName() {<br>&nbsp; &nbsp; &nbsp; &nbsp; return name;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; public void setName(String name) {<br>&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;<br>&nbsp; &nbsp; }<br>}<br><br></p>

The variable name can be accessed directly outside the class.
The variable name is encapsulated and can only be accessed through getter and setter methods.
The class does not follow the principle of encapsulation.
The variable name must be declared as protected for encapsulation.

5. Which access modifier allows a method to be accessed from any class in the program?

private
protected
public
default

6. Which of the following is true about the protected access modifier?

Members with protected access can only be accessed in the same package.
Members with protected access can be accessed in the same package and by subclasses.
Members with protected access can be accessed by any class.
Members with protected access cannot be inherited.

7. Consider the following code snippet. What will happen if you try to access data from another class in a different package? java Copy code

<p>class Example {<br>&nbsp; &nbsp; protected int data = 10;<br>}<br><br></p>

Compilation error.
The variable data can be accessed directly if a subclass exists in another package.
The variable data can never be accessed outside its package.
protected members are treated as private in different packages

8. What is the purpose of getter and setter methods in Java?

To provide controlled access to private variables.
To override parent class methods.
To allow default visibility for variables.
To achieve polymorphism.

9. Which access modifier should you use for maximum encapsulation?

public
private
protected
default

10. Which of the following is NOT an advantage of encapsulation?

It enhances code readability.
It protects data from unauthorized access.
It makes the code less reusable.
It helps to achieve a modular structure.