Test Series - oops

Test Number 18/78

Q: 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
Solution: 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.
Q: 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.
Solution: 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.
Q: 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
Solution: 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.
Q: Which access specifier is usually used for data members of a class?
A. Private
B. Default
C. Protected
D. Public
Solution: 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.
Q: Which specifier should be used for member functions of a class?
A. Private
B. Default
C. Protected
D. Public
Solution: 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.
Q: If a function has to be called only by using other member functions of the class, what should be the access specifier used for that function?
A. Private
B. Protected
C. Public
D. Default
Solution: The function should be made private. In this way, the function will be available to be called only from the class member functions. Hence the function will be secure from the outside world.
Q: Which among the following is correct to call a private member from outside the class?
A. object.memberfunction( parameters );
B. object->memberfunction( parameters );
C. object->memberfunction( parameteres); or object.memberfunction( parameters );
D. Not possible
Solution: The private member function will not be accessible from outside the class. Hence any syntax will not work to access the private members. If you have the address of the member, may be you can access those members, but that is a totally different case and concept.
Q: If private members have to be accessed directly from outside the class but the access specifier must not be changed, what should be done?
A. Specifier must be changed
B. Friend function should be used
C. Other public members should be used
D. It is not possible
Solution: For calling the function directly, we can’t use another function because that will be indirect call. Using friend function, we can access the private members directly.
Q: Which access specifier is/are most secure during inheritance?
A. Private
B. Default
C. Protected
D. Private and default
Solution: The private members are most secure in inheritance. The default members can still be in inherited in special cases, but the private members can’t be accessed in any case.
Q: Choose the correct option for the code given below.
class A{ static int c=0; public: A(){ c++; } };
A. Constructor will make c=1 for each object created
B. Constructor will make c=0 for each object created
C. Constructor will keep number of objects created
D. Constructor will just initialize c=0 then increment by 1
Solution: The constructor is using a static member to keep the count of the number of objects created. This is done because the variable c is static and hence the value will be common for all the objects created.

You Have Score    /10