250+ TOP MCQs on Member Functions and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Member Functions”.

1. Which among the following best describes member functions?
a) Functions which are defined within the class
b) Functions belonging a class
c) Functions in public access of a class
d) Functions which are private to class
Answer: b
Clarification: We can’t say that only functions that are defined inside class are member functions. There can be some inherited functions. Though they doesn’t belong to the class but are property of the objects once inheritance is used. So the nearest definition is functions belonging to a class.

2. How many types of member functions are generally there in C++?
a) 2
b) 3
c) 4
d) 5
Answer: d
Clarification: There are 5 types of member functions that are generally provided in C++. Namely, simple, static, const, inline and friend member functions. Member functions are specific to classes.

3. How can a static member function be called in the main function?
a) Using dot operator
b) Using arrow operator
c) Using dot or arrow operator
d) Using dot, arrow or using scope resolution operator with class name
Answer: d
Clarification: The member functions can be called using only the dot operator or the arrow operator. But the static members can be called using directly the class name followed by the scope resolution operator and static member function name. This is useful when you don’t have any object to call the member.

4. What are inline member functions?
a) Member functions which can be called without object
b) Member functions whose definition is expanded in place of its call
c) Member functions whose definition is faster than simple function
d) Member function which is defined in single line
Answer: b
Clarification: The member functions whose definition is expanded at the call, and no jump to function and return happened, are termed as inline functions. This is used to make the program faster and more efficient.

5. What happens if non static members are used in static member function?
a) Compile time error
b) Runtime error
c) Executes fine
d) Executes if that member function is not used
Answer: a
Clarification: There must be specific memory space allocated for the data members before the static member functions uses them. But the space is not reserved if object is not declared. Hence only if static members are not used, it leads to compile time error.

6. Static member functions _____________
a) Contains “this” pointer for data members
b) Contains “this” pointer if used for member functions
c) Doesn’t contain “this” pointer
d) Doesn’t contain “this” pointer if member functions are referred
Answer: c
Clarification: The static member functions doesn’t contain “this” pointer. Static member functions can’t be defined as const or volatile also. These are restrictions on static member functions.

7. How to access members of the class inside a member function?
a) Using this pointer only
b) Using dot operator
c) Using arrow operator
d) Used directly or with this pointer
Answer: d
Clarification: The members of a class can be used directly inside a member function. We can use this pointer when there is a conflict between data members of class and arguments/local function variable names.

8. For overloading “( )”, “[ ]” or “->” operators, a class __________
a) Must use static member functions
b) Must use non-static member functions
c) Must be non-static member and should not be friend of class
d) Must use static member function or a friend member function
Answer: c
Clarification: For overloading those operators for a class, the class must use non-static member function so that doesn’t remain common to all the objects, and each object can use it independently. The friend functions is also restricted so as to keep the security of data.

9. If a virtual member function is defined ___________
a) It should not contain any body and defined by subclasses
b) It must contain body and overridden by subclasses
c) It must contain body and be overloaded
d) It must not contain any body and should not be derived
Answer: a
Clarification: The virtual functions are defined using virtual keyword. These are made in order to make all the classes to define them as the class gets inherited. Increases code understanding.

10. Member functions of a generic class are _____________
a) Not generic
b) Automatically generic
c) To be made generic explicitly
d) Given default type as double
Answer: b
Clarification: When generic type is used in a class, the functions are automatically generic. This is so because the functions would use the same type as defined to make the class generic. The functions will get to know the type of data as soon as the generic class is used. It’s inbuilt feature.

11. Member function of a class can ____________
a) Access all the members of the class
b) Access only Public members of the class
c) Access only the private members of the class
d) Access subclass members
Answer: a
Clarification: The member functions has access to all the members of the class. Whenever data members of a class, which might be private, have to be modified, we make use of these member functions. This is more secure way to manipulate data.

12. Which among the following is proper syntax for class given below?

class A
	int a,b;
	public : void disp();

a) void disp::A(){ }
b) void A::disp(){ }
c) void A:disp() { cout<d) void disp:A(){ cout<Answer: b
Clarification: The syntax in option void A::disp(){ } is correct. We use scope resolution to represent the member function of a class and to write its definition. It is not necessary for a function to have anything in its definition.

13. A member function can _______________ of the same class.
a) Call other member functions
b) Call only private member functions
c) Call only static member functions
d) Call only const member functions
Answer: a
Clarification: We can call one function inside another function to access some data of class. A public member function can be used to call a private member function which directly manipulates the private data of class.

14. Which member function doesn’t require any return type?
a) Static
b) Constructor
c) Const
d) Constructor and destructor
Answer: d
Clarification: All the member functions work same as normal functions with syntax. But the constructor and destructor are also considered as member functions of a class, and they never have any data type.

15. Which among the following is not possible for member function?
a) Access protected members of parent class
b) Definition without return type
c) Access public members of subclass
d) Access static members of class
Answer: c
Clarification: A member function of a class can only have the access to the members of its own class and parent classes if inheritance used. Otherwise a member function can never access the members of a subclass. Accessing static members of a class is possible by normal and static member functions.

Leave a Reply

Your email address will not be published. Required fields are marked *