Encapsulation makes it easier to:

  1. reuse and modify existing modules of code.

  2. write and read code by sharing method names.

  3. hide and protect data from external code.

  4. Both a and b.

  5. All of the above.


Correct Option: C
Explanation:

To answer this question, the user needs to know the definition of encapsulation. Encapsulation is a concept in object-oriented programming that refers to the bundling of data and methods that operate on that data within a single unit, thus protecting the data from external access or modification.

Now, let's go through each option and explain why it is right or wrong:

A. reuse and modify existing modules of code: This option is partially correct. Encapsulation can make it easier to reuse and modify existing modules of code because it allows for code to be written in a modular and reusable way. However, this option only addresses the part of encapsulation that relates to code reuse and does not address its main purpose, which is to protect data from external access or modification.

B. write and read code by sharing method names: This option is incorrect. Encapsulation does not have anything to do with sharing method names. While encapsulation does bundle data and methods together, it is not done for the purpose of sharing method names.

C. hide and protect data from external code: This option is correct. Encapsulation is primarily used to hide and protect data from external code. By bundling data and methods together, encapsulation ensures that the data can only be accessed and modified through the methods provided by the encapsulated unit. This helps to prevent unintended modifications to data and increases the security of the code.

D. Both a and b: This option is incorrect. Option A is partially correct, but option B is incorrect. Thus, option D cannot be the correct answer.

E. All of the above: This option is incorrect. Option A is partially correct, but option B is incorrect. Option C is correct, but option A only addresses part of the purpose of encapsulation. Thus, option E cannot be the correct answer.

Therefore, the correct answer is:

The Answer is: C. hide and protect data from external code.

Find more quizzes: