This set of Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) focuses on “Access Specifiers”.
1. How many types of access specifiers are provided in OOP (C++)?
Explanation: 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
Explanation: 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?
d) All three
Explanation: 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)?
Explanation: Default access is used if the programmer doesn’t specify the specifier. This acts in similar way as that of private. But since nothing is specified we call it default access.
5. Which specifier allows a programmer to make the private members which can be inherited?
d) Protected and default
Explanation: 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
Explanation: 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?
Explanation: 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 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
Explanation: 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
Explanation: 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
Explanation: 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
Explanation: 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.
Explanation: 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?
b) Default or public
c) Protected or private
Explanation: 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?
Explanation: 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?
Explanation: 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.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs), here is complete set of 1000+ Multiple Choice Questions and Answers.