C++ Programming Multiple Choice Question - Functions

This section focuses on the "Functions" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.

1. Which of the following function / types of function cannot have default parameters?

A. Member function of class
B. Main()
C. Member function of structure
D. Both B and C

View Answer


2. Correct way to declare pure virtual function in a C++ class is

A. Virtual void foo() =0 ;
B. Void virtual foo()= { 0 }
C. Virtual void foo() {} = 0;
D. None of the above

View Answer


3. What is the scope of the variable declared in the user defined function?

A. Whole program
B. Only inside the {} block
C. The main function
D. None of the above

View Answer


4. Which of the following in Object Oriented Programming is supported by Function overloading and default arguments features of C++.

A. Inheritance
B. Polymorphism
C. Encapsulation
D. None of these

View Answer


5. Predict the output:

float x= 3.1496;
cout << setprecision(2) << x;

A. 3.14
B. 3.15
C. 3
D. 3.1

View Answer


6. Which of the following statement is correct?

A. Only one parameter of a function can be a default parameter.
B. Minimum one parameter of a function must be a default parameter.
C. All the parameters of a function can be default parameters.
D. No parameter of a function can be default.

View Answer


7. Which of the following function declaration using default arguments is incorrect?

A. int foo(int x, int y =5, int z=10)
B. int foo(int x=5, int y =10, int z)
C. int foo(int x=5, int y, int z=10)
D. All are correct

View Answer


8. How are many minimum numbers of functions need to be presented in c++?

A. 0
B. 1
C. 2
D. 3

View Answer


9. Inline functions may not work ______ . i) If function contain static variables. ii) If function contain global and register variables. iii) If function returning value consists looping construct(i.e. for, while). iv) If inline functions are recursive. v) If function contains const value.

A. Only i,iv & v
B. Only ii,iii & v
C. Only i,iii & iv
D. All of the above

View Answer


10. Unary scope resolution operator is denoted by

A. ! !
B. % %
C. :
D. : :

View Answer


11. What is the output of this program?

#include <iostream>
using namespace std;
void find()
void find()
{
    cout<<"course";
}
int main()
{
    find();
    return 0;
} 

A. course
B. coursecourse
C. compile time error
D. none of the mentioned

View Answer


12. What is the output of this program?

        
Note:Includes all required header files

    using namespace std;
    void fun(int p, int q)
    {
        p = 20;
        q = 10;
    }
    int main()
    {
        int p = 10;
        fun(p, p);
        cout << p;
        return 0;
    }    

A. 10
B. 20
C. compile time error
D. none of the mentioned

View Answer


13. What is the output of this program?

       
Note:Includes all required header files 
          
using namespace std;
    void copy (int& a, int& b, int& c)
    {
        a *= 2;
        b *= 2;
        c *= 2;
    }
    int main ()
    {
        int x = 2, y = 5, z = 7;
        copy (x, y, z);
        cout << "x =" << x << ", y =" << y << ", z =" << z;
        return 0;
    }       

A. x =3, y =7, z =10
B. x =3, y =6, z =5
C. x =4, y =10, z =14
D. None of the above

View Answer


14. What is the output of this program?

        
Note:Includes all required header files

using namespace std;
 
void fun(int &p)
    {
        p = 30;
    }
    int main()
    {
         int p = 5;
         fun(p);
         cout << "New value of p is " << p;
         return 0;
    }
       

A. New value of p is 5
B. New value of p is 30
C. New value of p is 15
D. None of the above

View Answer


15. Which of the following is true about the following program

Note:Includes all required header files
using namespace std;
   long factorial (long p)
    {
        if (p > 1)
            return (p * factorial (p + 1));
        else
            return (1);
    }
    int main ()
    {
        long q = 3;
        cout << q << "! = " << factorial ( q );
        return 0;
    }

A. 6
B. 24
C. segmentation fault
D. compile time error

View Answer


16. What will be the output of this program?

        
Note:Includes all required header files
using namespace std;
    void square (int *p)
    {
  *p = (*p + 1) * (*p);
    }
    int main ( )
    {
  int q = 5;
        square(&q);
        cout << q; 
        return 0;
    } 

A. 25
B. compile time error
C. 36
D. 30

View Answer


17. What will be the output of this program?

Note:Includes all required header files
 using namespace std;
     int max(int p, int q )
    {
        return ( p > q ? p : q );
    }
    int main()
    {
        int x = 25;
        int y = 50;
        cout << max(x, y );
        return 0;
    }  

A. 25
B. 50
C. either 25 or 50
D. none of the mentioned

View Answer


18. What will be the output of the following program?

        
Note:Includes all required header files
using namespace std;
    int gcd (int a, int b)
    {
        int temp;
        while (b != 0) 
        {
            temp = a % b;
            a = b;
            b = temp;
        }
        return(a);
    }
    int main ()
    {
        int x = 7, y = 13;
        cout << gcd(x, y);
        return(0);
    } 

A. 7
B. 13
C. 91
D. 1

View Answer


19. What will be the output of the following program?

#include <iostream>

using namespace std;
void lfc(int p)
{
    cout << p;
}
void lfc(double  q)
{
    cout << q;
}
int main(void)
{
    lfc(5);
    lfc(555.263);
    return 0;
}

A. 5555.263
B. 555.2635
C. 555.263
D. None of the mentioned

View Answer


20. What will be the output of the following program?

#include <iostream>

using namespace std;
int lfc (int a, int b)
{
    return (a * b);
}
float lfc (float a, float b)
{
    return (a / b);
}
int main()
{
    int x = 5, y = 2;
    float n = 5.0, m = 2.0;
    cout << lfc(x, y) <<"	";
    cout << lfc (n, m);
    return 0;
} 

A. 10.0 5.0
B. 5.0 2.5
C. 10.0 5
D. 10 2.5

View Answer


21. Which of the following function declaration is/are incorrect?

A. int Sum(int a, int b = 2, int c = 3);
B. int Sum(int a = 5, int b);
C. int Sum(int a = 0, int b, int c = 3);
D. Both B and C are incorrect.

View Answer


22. Which of the following functions are provided by compiler by default if we don't write in a C++ class?

A. Copy constructor
B. Assignment
C. Constructor
D. All the above

View Answer


23. How many can max number of arguments present in function in the c99 compiler?

A. 99
B. 90
C. 102
D. 127

View Answer


24. Which of the followings is/are not false about friend function ? i) It can be called / invoked with class object. ii) It has objects as arguments. iii) It can have built-in types as arguments. iv) It must declared only in public part of a class. v) It does not have this pointer as an argument.

A. Only ii,iv
B. Only i,ii,v
C. Only ii,iii,v
D. All of the above

View Answer


25. The maths function acos (x) stands for

A. Inverse Cosine of x
B. Inverse Sine of x
C. Inverse Tangent of x
D. Floor of x

View Answer


26. Which of the following statement is correct?

A. Two functions having same number of argument, order and type of argument can be overloaded if both functions do not have any default argument.
B. Overloaded function must have default arguments.
C. Overloaded function must have default arguments starting from the left of argument list.
D. A function can be overloaded more than once.

View Answer


27. Constant function in C++ can be declared as

A. Void display()
B. Void display() const
C. Const void display()
D. Void const display()

View Answer


28. Where does the execution of the program starts?

A. User-defined function
B. Main function
C. Void function
D. None of the above

View Answer


29. A friend function does not have 'this' pointer associated with it.

A. TRUE
B. FALSE
C. May Be
D. Can't Say

View Answer


30. A programmer can create custom header files that must be end with

A. .h extension
B. .l extension
C. .ios extension
D. .a extension

View Answer


31. What will be the output of the following program?

        
void abc(int x=0, int y, int z=0)
{
  cout << x  << y << z;
}

A. abc () ;
B. abc (h) ;
C. abc (h,h) ;
D. Both A and B

View Answer


32. What will be the output of the following program?

int a =1 , b=2;
        
a=chg(b) ;
  cout << a << b ;

  If the function chg is coded as
  int chg(int x)
    {
x = 10;

return ();

} 
then  

A. It results in compile-time error
B. It results in run time error
C. It prints 1 1 2
D. It prints 1 1 1

View Answer


33. What will be the output of the following program?

int a =1 , b=2;
        
a=chg(b) ;
  cout << a << b ;

  If the function chg is coded as
  int chg(int &x)
    {
x = 10;

return ();

} 
then    

A. It results in compile-time error
B. It results in run time error
C. It prints 1 1 2
D. It prints 1 1 10

View Answer


34. What will be the output of the following program?

int a =1 , b=2;
        
a=chg(b) ;
  cout << a << b ;

  If the function chg is coded as
  int chg(const int &x)
    {
x = 10;

return ();

} 
then    
  

A. It results in compile-time error
B. It results in run time error
C. It prints 1 1 2
D. It prints 1 1 10

View Answer


35. Choose the correct statements regarding inline functions.

A. It speeds up execution
B. It slows down execution
C. It increases the code size
D. Both A and C

View Answer


36. The compiler identifies a virtual function to be pure

A. by the presence of the keyword pure
B. by its location in the program
C. if it is equated to 0
D. None of the above

View Answer


37. If many functions-have the same name, which of the following information, if present, will be used by the compiler to invoke the correct function to be used?

A. The operator : :
B. The return value of the function
C. Function signature
D. Both A & C

View Answer


38. Which of the following parameter passing mechanism(s) is/are supported by C++, but not by C?

A. Pass by value
B. Pass by reference
C. Pass by value-result
D. All of the above

View Answer


39. If a piece of code can be implemented as a macro or as an inline function, which of the following factors favour implementation as an inline function?

A. Interacting with other components (like variables in an expression), in the correct way ..
B. Flexibility to manipulate as a pointer
C. Source code size
D. Both A and B

View Answer


40. Assume that the random number generating function - rand( ), returns an integer between 0 and 10000 (both inclusive). If you want to simulate the throwing of a die using this random function, use the expression

A. rand ( ) % 6
B. rand ( ) % 6 + 1
C. rand ( ) % 5 + 1
D. None of the above

View Answer





Also check :


Discussion



* You must be logged in to add comment.