Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) 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
Answer: c
Clarification: 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
Answer: d
Clarification: 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
Answer: c
Clarification: 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)
Answer: b
Clarification: 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:
b) public(
c) public void
d) public::
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: d
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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);
b) object.functionName(parameters);
c) object.functionName void (parameters)
d) object.void functionName();
Answer: b
Clarification: 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
Answer: b
Clarification: 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.
a) True
b) False
