Test Series - oops

Test Number 17/78

Q: How many types of access specifiers are provided in OOP (C++)?
A. 1
B. 2
C. 3
D. 4
Solution: 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.
Q: 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
Solution: All the classes can use any of the specifiers as needed. There is no restriction on how many of them can be used together.
Q: Which among the following can restrict class members to get inherited?
A. Private
B. Protected
C. Public
D. All three
Solution: 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.
Q: 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
Solution: 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.
Q: Which specifier allows a programmer to make the private members which can be inherited?
A. Private
B. Default
C. Protected
D. Protected and default
Solution: 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.
Q: 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
Solution: The default members can be inherited. Provided that they are in same package. It works in a little different way from private access specifier.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.

You Have Score    /10