This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Public Member Functions”.
1. What are public member functions?
a) Functions accessible outside the class but not in derived class
b) Functions accessible outside the class directly
c) Functions accessible everywhere using object of class
d) Functions that can’t be accessed outside the class
Explanation: The most suitable definition would be that public member functions are accessible everywhere using object of the class. If derived classes are using those, derived class object can be used to call those functions.
2. Which among the following is true for public member functions?
a) Public member functions doesn’t have a return type
b) Public member functions doesn’t have any security
c) Public member functions are declared outside the class
d) Public member functions can be called using object of class
Explanation: The public member functions can be called using object of the class. The members can’t be declared outside the class as those would become non-member functions of the class. The functions have security as those can be accessed using the class object only.
3. Which type of member functions get inherited in the same specifier in which the inheritance is done? (If private inheritance is used, those become private and if public used, those become public)
a) Private member functions
b) Protected member functions
c) Public member functions
d) All member functions
Explanation: The public member functions gets into the same specifier in which the inheritance is done. If protected members are involved in public inheritance, still those remain protected in the derived class but public members become public on public inheritance and protected in protected inheritance.
4. Which syntax among the following is correct for public member functions?
a) public::void functionName(parameters)
b) public void functionName(parameters)
c) public(void functionName(parameters))
d) public:-void functionName(Parameters)
Explanation: The public member functions declaration must be mentioned with the keyword public. The syntax given is used in java. Keyword public is followed by the usual function declaration.
5. Which syntax is applicable to declare public member functions in C++?
a) public: <function declaration>
b) public(<function declaration>)
c) public void <function declaration>
d) public::<function declaration>
Explanation: The syntax in C++ must contain the public keyword followed by a colon. Thereafter, all the public members can be declared. But in few other language, public have to be mentioned explicitly with each member.
6. In java, which rule among the following is applicable?
a) Keyword public can’t be preceded with all the public members
b) Keyword public must be preceded with all the public members
c) Keyword public must be post mentioned the function declaration
d) Keyword public is not mandatory
Explanation: The public members in java must be preceded with the keyword public. It must be mentioned with each public member, unlike C++ where we mention it only once. In java, each member must have explicit declaration of specifier type.
7. How many public members are allowed in a class?
a) Only 1
b) At most 7
c) Exactly 3
d) As many as required
Explanation: The number of public members that can be defined in a class doesn’t have any limit. Though the programmer should not use too many functions, instead should use another class for more specific functions to reduce the readability complexity.
8. Which is not a proper way to access public members of a class?
a) Using object pointer with arrow operator
b) Using object of class in main function
c) Using object of class with arrow operator
d) Using object anywhere in the program
Explanation: The public members can be accessed anywhere in the program using the object of the class. And if object pointer is used, then arrow operator is used to access class members. If normal object is used with arrow operator, an error will be generated.
9. Which call is correct for public members of a nested class?
a) Can be called from object of enclosing class
b) Can be called within enclosing class only with direct names
c) Direct names should be used for the nested classes
d) Only with help of nested class object pointer
Explanation: The closest definition is that any public member function of the nested class can be accessed with the help of enclosing class object. The nested class object pointer can be used only within the enclosing class. It’s not mandatory to use the members of nested class only within the enclosing class.
10. Which public function call among the following is correct outside the class, if return type is void (C++)?
a) object.void functionName(parameters);
c) object.functionName void (parameters)
d) object.void functionName();
Explanation: The condition given says that there is no return type hence we can call the function directly. The object name should be mentioned with a dot operator to access its class members. Then the function name with parameters, if required, can be given.
11. If public members are to be restricted from getting inherited from the subclass of the class containing that function, which alternative is best?
a) Make the function private
b) Use private inheritance
c) Use public inheritance
d) Use protected inheritance
Explanation: If private inheritance is used then the class containing the function will be able to use the function with rules of whichever specifier is used. Then the derived class makes those function the private members of itself. This restricts the public members of parent class from further inheritance.
12. A derived class object can access the public members of the base class.
Explanation: The public members of the base class can be accessed from the derived class object only if public inheritance is used. If protected or private inheritance is used then those members become public/protected in derived class and hence won’t be able to be called from object of the derived class.
13. If a class have a public member function and is called directly in the main function then ___________________________
a) Undeclared function error will be produced
b) Out of memory error is given
c) Program gives warning only
d) Program shut down the computer
Explanation: If the function is called directly without using any object then the compiler doesn’t gets to know that the function have to be called from a specific class. And if there are no global or in-scope function with same name then the compiler produces an error stating that the called function is undeclared.
14. The function main() must always be public.
Explanation: The main() function must always be public. This is because the whole function and the operating system that is out of the package have to access the main function throughout the program execution. Hence the main() function should be public so as to be available everywhere in the program.
15. All the public member functions ___________________
a) Can’t access the private members of a class
b) Can’t access the protected members of a class
c) Can access only public members of a class
d) Can access all the member of its class
Explanation: The public member function can access any private, protected and public member of its class. Not only public member function, any member function of a class can access each and every other member declared inside the class. Hence are flexible to program.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!