Function and Method - Object oriented programming (MCQ) questions

Here, you can read Function and Method multiple choice questions and answers with explanation.

1)   Invoking methods characteristics
- Published on 19 Oct 15

a. methods can be invoked in any order and calling method should be in same class as worker method
b. Worker method and calling method can be invoked in same way
c. Any method can call any method
Answer  Explanation 

ANSWER: methods can be invoked in any order and calling method should be in same class as worker method

Explanation:
Methods can be invoked in any order. Methods do not need to be completed in the order in which they are listed in the class where they are declared. The way you invoke a worker class is different based on whether they are present in same class or different class there is no limit in number of method calls that a calling method can make. The calling method and worker can be in same class or different class. There are privately defined methods and so cannot be called by method in any other class.


2)   Function templates are considered equivalent when
- Published on 19 Oct 15

a. Declared in same scope
b. Having same name
c. Having identical return type and parameter list
d. All of the above
Answer  Explanation 

ANSWER: All of the above

Explanation:
Function overloading performs similar operations on different data types. Thus if similar operations are to be executed on all data types, function templates gives an easy solution for the same. Function template is defined with keyword 'template' followed by a parameter list. Each parameter has typename and can be of built-in or user defined type. Also, the number of parameters must not exceed eight. All the given options in the question above are hence true.


3)   Function Templates can have
- Published on 19 Oct 15

a. Explicit instantiation definition with template argument for all parameters
b. explicit instantiation of declaration with template argument for all parameters
c. Both a and b
d. None
Answer  Explanation 

ANSWER: Both a and b

Explanation:
A function template cannot be defined as a type or a function in itself. There is no such coding available that contains only template definitions generated from a source file. A template must have real instance i.e. template arguments must be determined such that the compiler generates an actual function. Here both first and second options are correct. An explicit formation of instance definition forces instantiation of member functions. For instantiating a function template all template arguments must be known.


4)   Give output of the following:

#include <iostream>
            using namespace std;

class exam
            {     private:
                  int x, y, z;
               public:
               int testcase( )

             {
                x=50; y=20; z=30;
                 }
               friend int add( exam e);
             };

            int add( exam e)
             {

                   return int( e.x+ e.y+ e.z);
                    }
              int main( )
           {
  exam a;
  a.testcase( );
              cout << add(a);
              return 0;

               }

- Published on 19 Oct 15

a. 0
b. compile time error
c. 100
d. none
Answer  Explanation 

ANSWER: compile time error

Explanation:
This code shows usage of a friend function and how a friend function has access to private members of a class. Generally a function defined outside the class cannot access the private and protected members of the class but by declaring a function as friend function the class gives it permission to access its private and protected data.


1