This set of Object Oriented Programming using C++ Multiple Choice Questions & Answers focuses on “Private Member Functions”.
1. Which is private member functions access scope?
a) Member functions which can only be used within the class
b) Member functions which can used outside the class
c) Member functions which are accessible in derived class
d) Member functions which can’t be accessed inside the class
Explanation: The member functions can be accessed inside the class only if they are private. The access is scope is limited to ensure the security of the private members and their usage.
2. Which among the following is true?
a) The private members can’t be accessed by public members of the class
b) The private members can be accessed by public members of the class
c) The private members can be accessed only by the private members of the class
d) The private members can’t be accessed by the protected members of the class
Explanation: The private members are accessible within the class. There is no restriction on use of private members by public or protected members. All the members can access the private member functions of the class.
3. Which member can never be accessed by inherited classes?
a) Private member function
b) Public member function
c) Protected member function
d) All can be accessed
Explanation: The private member functions can never be accessed in the derived classes. The access specifiers is of maximum security that allows only the members of self class to access the private member functions.
4. Which syntax among the following shows that a member is private in a class?
a) private: functionName(parameters)
c) private functionName(parameters)
Explanation: The function declaration must contain private keyword follower by the return type and function name. Private keyword is followed by normal function declaration.
5. If private member functions are to be declared in C++ then _____________
a) private: <all private members>
b) private <member name>
c) private(private member list)
d) private :- <private members>
Explanation: The private members doesn’t have to have the keyword with each private member. We only have to specify the keyword private followed by single colon and then private member’s are listed.
6. In java, which rule must be followed?
a) Keyword private preceding list of private member’s
b) Keyword private with a colon before list of private member’s
c) Keyword private with arrow before each private member
d) Keyword private preceding each private member
Explanation: The private keyword must be mentioned before each private member. Unlike the rule in C++ to specify private once and list all other private member’s, in java all member declarations must be preceded by the keyword private.
7. How many private member functions are allowed in a class?
a) Only 1
b) Only 7
c) Only 255
d) As many as required
Explanation: There are no conditions applied on the number of private member functions that can be declared in a class. Though the system may restrict use of too many functions depending on memory.
8. How to access a private member function of a class?
a) Using object of class
b) Using object pointer
c) Using address of member function
d) Using class address
Explanation: Even the private member functions can be called outside the class. This is possible if address of the function is known. We can use the address to call the function outside the class.
9. Private member functions ____________
a) Can’t be called from enclosing class
b) Can be accessed from enclosing class
c) Can be accessed only if nested class is private
d) Can be accessed only if nested class is public
Explanation: The nested class members can’t be accessed in the enclosed class even though other members can be accessed. This is to ensure the class members security and not to go against the rules of private members.
10. Which function among the following can’t be accessed outside the class in java in same package?
a) public void show()
b) void show()
c) protected show()
d) static void show()
Explanation: The protected members are available within the class. And are also available in derived classes. But these members are treated as private members for outside the class and inheritance structure. Hence can’t be accessed.
11. If private members are to be called outside the class, which is a good alternative?
a) Call a public member function which calls private function
b) Call a private member function which calls private function
c) Call a protected member function which calls private function
d) Not possible
Explanation: The private member functions can be accessed within the class. A public member function can be called which in turn calls the private member function. This maintains the security and adheres to the rules of private members.
12. A private function of a derived class can be accessed by the parent class.
Explanation: If private functions get accessed even by the parent class that will violate the rules of private members. If the functions can be accessed then the derived class security is hindered.
13. Which error will be produced if private members are accessed?
a) Can’t access private message
b) Code unreachable
c) Core dumped
d) Bad code
Explanation: The private members access from outside the class produce an error. The error states that the code at some line can’t access the private members. And denies the access terminating the program.
14. Can main() function be made private?
a) Yes, always
b) Yes, if program doesn’t contain any classes
c) No, because main function is user defined
d) No, never
Explanation: The reason given in option “No, because main function is user defined” is wrong. The proper reason that the main function should not be private is that it should be accessible in whole program. This makes the program flexible.
15. If a function in java is declared private then it __________________
a) Can’t access the standard output
b) Can access the standard output
c) Can’t access any output stream
d) Can access only the output streams
Explanation: The private members can access any standard input or output. There is no restriction on access to any input or output stream. And since standard input can also be used hence only accessing the output stream is not true.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming 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!