Which of the access modifiers a top level class may have?

  1. protected

  2. private

  3. public

  4. friendly


Correct Option: C
Explanation:

To answer this question, the user needs to know the concept of access modifiers in object-oriented programming. Access modifiers are keywords that determine the accessibility of classes, fields, methods, and other members of a class from other classes and packages. A top-level class is a class that is not a nested class or an inner class.

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

A. protected: A protected access modifier can be applied to class members but not to top-level classes. Protected members can be accessed from within the class itself, its subclasses, and classes in the same package.

B. private: A private access modifier can be applied to class members but not to top-level classes. Private members can only be accessed from within the class itself.

C. public: A public access modifier can be applied to top-level classes, as well as class members. Public members can be accessed from anywhere in the program.

D. friendly: There is no such access modifier as "friendly". It may be a typo or a misunderstanding of the default access modifier, which is applied when no access modifier is specified. The default access modifier allows access to members from within the same package.

Therefore, the correct answer is:

The Answer is: C

Find more quizzes: