Test Series - oops

Test Number 19/78

Q: Which among the following is true?
A. Private member functions can’t be overloaded
B. Private member functions can be overridden
C. Private member functions can’t be overloaded with a public member
D. Private member function can’t be overridden
Solution: The private member functions can be overloaded but they can’t be overridden. This is because, overriding means a function with same name in derived class, gets more priority when called from object of derived class. Here, the member function is private so there is no way that it can be overridden.
Q: Which among the following is correct?
A. Private specifier must be used before public specifier
B. Private specifier must be used before protected specifier
C. Private specifier must be used first
D. Private specifier can be used anywhere in class
Solution: The private specifier can be used anywhere in the class as required. It is not a rule to mention the private members first and then others. It is just followed to write first for better readability.
Q: Which among the following best describes the protected specifier?
A. Members are most secure and can’t be used outside class
B. Members are secure but can be used outside the class
C. Members are secure as private, but can be inherited
D. Members are secure like private, but can’t be inherited
Solution: The members which are made protected, are most secure if inheritance is not used. But, this facility is provided to keep those members private and with that, they can be inherited by other classes. This is done to make the code more flexible.
Q: If a constructor is defined in protected access, then?
A. It’s instance can be created inside the subclasses
B. It’s instance can be created anywhere in the program
C. It’s instance can be created inside the subclasses and main() function
D. It’s instance can be created inside the parent class only
Solution: The instances will be allowed to be created only inside the sub classes. This is because the protected members will be inherited and hence the constructor too. This will allow the subclasses to call the constructor whenever an object is created.
Q: If the protected members are to be made accessible only to the nearest subclass and no further subclasses, which access specifier should be used in inheritance?
A. The sub class should inherit the parent class privately
B. The sub class should inherit the parent class as protected
C. The sub class should inherit the parent class as public
D. The sub class can use any access modifier
Solution: The sub class should use private inheritance. This will allow only the nearest sub classes to inherit the protected members and then those members will become private. Hence further inheritance of those members will not be possible.
Q: Protected members differ from default members as _______
A. Protected members can be accessed outside package using inheritance, but default can’t
B. Default members can be accessed outside package using inheritance, but protected can’t
C. Protected members are allowed for inheritance but Default members are not allowed
D. Both are same
Solution: The protected members are allowed in the same package but can also be accessed in other packages using inheritance. But the default members can never be accessible in other packages.
Q: If all the members are defined in protected specifier then? (Constructors not considered)
A. Instance of class can’t be created
B. Instance of class can be created anywhere
C. Instance of class can be created only in subclasses
D. Instance of class can be created only in main() function
Solution: The instances can be created anywhere in the program. This is because the constructors are not considered among the members defined in protected mode. Hence the default implicit constructor will be used whenever an object is created.
Q: If protected inheritance is used then _____
A. Public members become public in subclass
B. Protected members become public in subclass
C. Protected members become protected in subclass
D. Protected and Public members become protected in subclass
Solution: The protected and public members of the parent class will become the protected members in subclass. This is predefined rule of inheritance. The reason behind is to maintain the level of security in subclass too.
Q: Which among the following can use protected access specifier?
A. Members which may be used in other packages
B. Members which have to be secure and should be used by other packages/subclass
C. Members which have to be accessed from anywhere in the program
D. Members which have to be as secure as private but can be used by main() function
Solution: The members which have to be secure and might get used in other packages or subclasses can use protected access. This also allows the members to be safe from accidental modification.
Q: If a class have default constructor defined in private access, and one parameter constructor in protected mode, how will it be possible to create instance of object?
A. Define a constructor in public access with different signature
B. Directly create the object in the subclass
C. Directly create the object in main() function
D. Not possible
Solution: If a new constructor is defined in public access. That will be available to the whole program. Only restriction will be of the way to use it.

You Have Score    /10