Test Series - oops

Test Number 38/78

Q: Which is the correct syntax of inheritance?
A. class derived_classname : base_classname{ /*define class body*/ };
B. class base_classname : derived_classname{ /*define class body*/ };
C. class derived_classname : access base_classname{ /*define class body*/ };
D. class base_classname :access derived_classname{ /*define class body*/ };
Solution: Firstly, keyword class should come, followed by the derived class name. Colon is must followed by access in which base class has to be derived, followed by the base class name. And finally the body of class. Semicolon after the body is also must.
Q: Which type of inheritance leads to diamond problem?
A. Single level
B. Multi-level
C. Multiple
D. Hierarchical
Solution: When 2 or more classes inherit the same class using multiple inheritance and then one more class inherits those two base classes, we get a diamond like structure. Here, ambiguity arises when same function gets derived into 2 base classes and finally to 3rd level class because same name functions are being inherited.
Q: Which access type data gets derived as private member in derived class?
A. Private
B. Public
C. Protected
D. Protected and Private
Solution: It is a rule, that when a derived class inherits the base class in private access mode, all the members of base class gets derived as private members of the derived class.
Q: If a base class is inherited in protected access mode then which among the following is true?
A. Public and Protected members of base class becomes protected members of derived class
B. Only protected members become protected members of derived class
C. Private, Protected and Public all members of base, become private of derived class
D. Only private members of base, become private of derived class
Solution: As the programming language rules apply, all the public and protected members of base class becomes protected members of derived class in protected access mode. It can’t be changed because it would hinder the security of data and may add vulnerability in the program.
Q: Members which are not intended to be inherited are declared as ________________
A. Public members
B. Protected members
C. Private members
D. Private or Protected members
Solution: Private access specifier is the most secure access mode. It doesn’t allow members to be inherited. Even Private inheritance can only inherit protected and public members.
Q: While inheriting a class, if no access mode is specified, then which among the following is true? (in C++)
A. It gets inherited publicly by default
B. It gets inherited protected by default
C. It gets inherited privately by default
D. It is not possible
Solution: If the access mode is not specified during inheritance, the class is inherited privately by default. This is to ensure the security of data and to maintain OOP features. Hence it is not mandatory to specify the access mode if we want the class to be inherited privately.
Q: If a derived class object is created, which constructor is called first?
A. Base class constructor
B. Derived class constructor
C. Depends on how we call the object
D. Not possible
Solution: First the base class constructor is invoked. When we create a derived class object, the system tries to invoke its constructor but the class is derived so first the base class must be initialized, hence in turn the base class constructor is invoked before the derived class constructor.
Q: The private members of the base class are visible in derived class but are not accessible directly.
A. True
B. False
C. 1
D. 0
Solution: Consider that a variable is private in base class and the derived class uses public inheritance to inherit that class. Now if we also have a global variable of same name as that of base class private variable, neither the global variable nor the base class private variable will be accessible from derived class. This is because we can’t have 2 variables with same name in same local scope. Hence the private members are accessible but not directly.
Q: How can you make the private members inheritable?
A. By making their visibility mode as public only
B. By making their visibility mode as protected only
C. By making their visibility mode as private in derived class
D. It can be done both by making the visibility mode public or protected
Solution: It is not mandatory that you have to make the visibility mode either public or protected. You can do either of those. That will give you permission to inherit the private members of base class.
Q: How many basic types of inheritance are provided as OOP feature?
A. 4
B. 3
C. 2
D. 1
Solution: There are basically 4 types of inheritance provided in OOP, namely, single level, multilevel, multiple and hierarchical inheritance. We can add one more type as Hybrid inheritance but that is actually the combination any types of inheritance from the 4 basic ones.

You Have Score    /10