250+ TOP MCQs on Access Specifiers and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Access Specifiers”.

1. How many types of access specifiers are provided in OOP (C++)?
a) 1
b) 2
c) 3
d) 4
Answer: c
Clarification: Only 3 types of access specifiers are available. Namely, private, protected and public. All these three can be used according to the need of security of members.

2. Which among the following can be used together in a single class?
a) Only private
b) Private and Protected together
c) Private and Public together
d) All three together
Answer: d
Clarification: All the classes can use any of the specifiers as needed. There is no restriction on how many of them can be used together.

3. Which among the following can restrict class members to get inherited?
a) Private
b) Protected
c) Public
d) All three
Answer: a
Clarification: Private members of a class can’t be inherited. These members can only be accessible from members of its own class only. It is used to secure the data.

4. Which access specifier is used when no access specifier is used with a member of class (java)?
a) Private
b) Default
c) Protected
d) Public
Answer: b
Clarification: Default access is used if the programmer doesn’t specify the specifier. This acts in a similar way as that of private. But since nothing is specified we call it to default access.

5. Which specifier allows a programmer to make the private members which can be inherited?
a) Private
b) Default
c) Protected
d) Protected and default
Answer: c
Clarification: Protected access is used to make the members private. But those members can be inherited. This gives both security and code reuse capability to a program.

6. Which among the following is false?
a) Private members can be accessed using friend functions
b) Member functions can be made private
c) Default members can’t be inherited
d) Public members are accessible from other classes also
Answer: c
Clarification: The default members can be inherited. Provided that they are in same package. It works in a little different way from private access specifier.

7. If a class has all the private members, which specifier will be used for its implicit constructor?
a) Private
b) Public
c) Protected
d) Default
Answer: b
Clarification: The implicit constructor will always be public. Otherwise the class wouldn’t be able to have instances. In turn, no objects will be created and the class can only be used for inheritance.

8. If class A has add() function with protected access, and few other members in public. Then class B inherits class A privately. Will the user will not be able to call _________ from the object of class B.
a) Any function of class A
b) The add() function of class A
c) Any member of class A
d) Private, protected and public members of class A
Answer: d
Clarification: Class B object will not be able to call any of the private, protected and public members of class A. It is not only about the function add(), but all the members of class A will become private members of class B.

9. Which access specifier should be used in a class where the instances can’t be created?
a) Private default constructor
b) All private constructors
c) Only default constructor to be public
d) Only default constructor to be protected
Answer: b
Clarification: All the constructors must be made private. This will restrict the instance of class to be made anywhere in the program. Since the constructors are private, no instance will be able to call them and hence won’t be allocated with any memory space.

10. On which specifier’s data, does the size of a class’s object depend?
a) All the data members are added
b) Only private members are added
c) Only public members are added
d) Only default data members are added
Answer: a
Clarification: All the data members are counted to calculate the size of an object of a class. The data member access specifier doesn’t play any role here. Hence all the data size will be added.

11. If class B inherits class A privately. And class B has a friend function. Will the friend function be able to access the private member of class A?
a) Yes, because friend function can access all the members
b) Yes, because friend function is of class B
c) No, because friend function can only access private members of friend class
d) No, because friend function can access private member of class A also
Answer: c
Clarification: The friend function of class B will not be able to access private members of class A. Since B is inheriting class A privately, the members will become private in class B. But private members of class A won’t be inherited at all. Hence it won’t be accessible.

12. If an abstract class has all the private members, then _________
a) No class will be able to implement members of abstract class
b) Only single inheritance class can implement its members
c) Only other enclosing classes will be able to implement those members
d) No class will be able to access those members but can implement.
Answer: a
Clarification: The classes which inherit the abstract class, won’t be able to implement the members of abstract class. The private members will not be inherited. This will restrict the subclasses to implement those members.

13. Which access specifier should be used so that all the parent class members can be inherited and accessed from outside the class?
a) Private
b) Default or public
c) Protected or private
d) Public
Answer: d
Clarification: All the members must be of public access. So that the members can be inherited easily. Also, the members will be available from outside the class.

14. Which access specifier is usually used for data members of a class?
a) Private
b) Default
c) Protected
d) Public
Answer: a
Clarification: All the data members should be made private to ensure the highest security of data. In special cases we can use public or protected access, but it is advised to keep the data members private always.

15. Which specifier should be used for member functions of a class?
a) Private
b) Default
c) Protected
d) Public
Answer: d
Clarification: It is always advised that the member functions should be kept public so that those functions can be used from out of the class. This is usually done to ensure that the features provided by the class can be used at its maximum.

Leave a Reply

Your email address will not be published. Required fields are marked *