Object Oriented Programming using C++ Questions and Answers – Private Member Functions

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
View Answer

Answer: a
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
View Answer

Answer: b
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
View Answer

Answer: a
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.
advertisement
advertisement

4. Which syntax among the following shows that a member is private in a class?
a) private: functionName(parameters)
b) private(functionName(parameters))
c) private functionName(parameters)
d) private::functionName(parameters)
View Answer

Answer: c
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>
View Answer

Answer: a
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.
Note: Join free Sanfoundry classes at Telegram or Youtube

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
View Answer

Answer: d
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
View Answer

Answer: d
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.
advertisement

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
View Answer

Answer: c
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
View Answer

Answer: a
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.
advertisement

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()
View Answer

Answer: c
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
View Answer

Answer: a
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.
a) True
b) False
View Answer

Answer: b
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
View Answer

Answer: a
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
View Answer

Answer: d
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
View Answer

Answer: b
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.