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

This set of Object Oriented Programming using C++ Problems focuses on “Types of Member Functions”.

1. How many types of member functions are possible in general?
a) 2
b) 3
c) 4
d) 5
View Answer

Answer: d
Explanation: There are basically 5 types of member functions possible. The types include simple, static, const, inline, and friend member functions. Any of these types can be used in a program as per requirements.

2. Simple member functions are ______________________
a) Ones defined simply without any type
b) Ones defined with keyword simple
c) Ones that are implicitly provided
d) Ones which are defined in all the classes
View Answer

Answer: a
Explanation: When there is no type defined for any function and just a simple syntax is used with the return type, function name and parameter list then those are known as simple member functions. This is a general definition of simple members.

3. What are static member functions?
a) Functions which use only static data member but can’t be accessed directly
b) Functions which uses static and other data members
c) Functions which can be accessed outside the class with the data members
d) Functions using only static data and can be accessed directly in main() function
View Answer

Answer: d
Explanation: The static member functions can be accessed directly in the main function. There is no restriction on direct use. We can call them with use of objects also. But the restriction is that the static member functions can only use the static data members of the class.
advertisement
advertisement

4. How can static member function can be accessed directly in main() function?
a) Dot operator
b) Colon
c) Scope resolution operator
d) Arrow operator
View Answer

Answer: c
Explanation: The static member functions can be accessed directly in the main() function. The only restriction is that those must use only static data members of the class. These functions are property of class rather than each object.

5. Correct syntax to access the static member functions from the main() function is ______________
a) classObject::functionName();
b) className::functionName();
c) className:classObject:functionName();
d) className.classObject:functionName();
View Answer

Answer: b
Explanation: The syntax in option b must be followed in order to call the static functions directly from the main() function. That is a predefined syntax. Scope resolution helps to spot the correct function in the correct class.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. What are const member functions?
a) Functions in which none of the data members can be changed in a program
b) Functions in which only static members can be changed
c) Functions which treat all the data members as constant and doesn’t allow changes
d) Functions which can change only the static members
View Answer

Answer: c
Explanation: The const member functions are intended to keep the value of all the data members of a class same and doesn’t allow any changes on them. The data members are treated as constant data and any modification inside the const function is restricted.

7. Which among the following best describes the inline member functions?
a) Functions defined inside the class only
b) Functions with keyword inline only
c) Functions defined outside the class
d) Functions defined inside the class or with the keyword inline
View Answer

Answer: d
Explanation: The functions which are defined with the keyword inline or are defined inside the class are treated to be inline functions. Definitions inside the class are implicitly made inline if none of the complex statements are used in the definition.
advertisement

8. What are friend member functions (C++)?
a) Member function which can access all the members of a class
b) Member function which can modify any data of a class
c) Member function which doesn’t have access to private members
d) Non-member functions which have access to all the members (including private) of a class
View Answer

Answer: d
Explanation: A non-member function of a class which can access even the private data of a class is a friend function. It is an exception on access to private members outside the class. It is sometimes considered as a member functions since it has all the access that a member function in general have.

9. What is the syntax of a const member function?
a) void fun() const {}
b) void fun() constant {}
c) void const fun() {}
d) const void fun(){}
View Answer

Answer: a
Explanation: The general syntax to be followed in order to declare a const function in a class is as in option a. The syntax may vary in different programming languages.
advertisement

10. Which keyword is used to make a nonmember function as friend function of a class?
a) friendly
b) new
c) friend
d) connect
View Answer

Answer: c
Explanation: The keyword friend is provided in programming languages to use it whenever a functions is to be made friend of one class or other. The keyword indicates that the function is capable of new functionalities like accessing private members.

11. Member functions _____________________
a) Must be defined inside class body
b) Can be defined inside class body or outside
c) Must be defined outside the class body
d) Can be defined in another class
View Answer

Answer: c
Explanation: The functions definitions can be given inside or outside the body of class. If defined inside, general syntax is used. If defined outside then the class name followed by scope resolution operator and then function name must be given for the definition.

12. All type of member functions can’t be used inside a single class.
a) True
b) False
View Answer

Answer: b
Explanation: There is no restriction on the use of type of member functions inside a single class. Any type any number of times can be defined inside a class. The member functions can be used as required.

13. Which among the following is true?
a) Member functions can never be private
b) Member functions can never be protected
c) Member functions can never be public
d) Member functions can be defined in any access specifier
View Answer

Answer: d
Explanation: The member functions can be defined inside any specifier. There is no restriction. The programmer can apply restrictions on its use by specifying the access specifier with the functions.

14. Which keyword is used to define the static member functions?
a) static
b) stop
c) open
d) state
View Answer

Answer: a
Explanation: The static keyword is used to declare any static member function in a class. The static members become common to each object of the class being created. They share the same values.

15. Which keyword is used to define the inline member function?
a) no keyword required
b) inline
c) inlined
d) line
View Answer

Answer: b
Explanation: The inline keyword is used to defined the inline member functions in a class. The functions are implicitly made inline if defined inside the class body, but only if they doesn’t have any complex statement inside. All functions defined outside the class body must be mentioned with an explicit inline keyword.

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming using C++ Problems, 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.