Test Series - oops

Test Number 22/78

Q: 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
Solution: 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.
Q: How many types of member functions are generally there in C++?
A. 2
B. 3
C. 4
D. 5
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.

You Have Score    /10