C++ Function Overloading and Default Arguments

    Last Updated :
    Discuss
    Comments

    Question 1

    Output? CPP
    #include<iostream>
    using namespace std;
    
    int fun(int x = 0, int y = 0, int z)
    {  return (x + y + z); }
    
    int main()
    {
       cout << fun(10);
       return 0;
    }
    
    • 10
    • 0
    • 20
    • Compiler Error

    Question 2

    What is the output of this C++ program?

    #include 
    using namespace std;
    void square (int *x)
    {
    *x = (*x)++ * (*x);
    }
    void square (int *x, int *y)
    {
    *x = (*x) * --(*y);
    }
    int main ( )
    {
    int number = 30;
    square(&number, &number);
    cout << number;
    return 0;
    }
    • 910

    • 920

    • 870

    • 900

    Question 3

    Which of the following in Object Oriented Programming is supported by Function overloading and default arguments features of C++.
    • Inheritance
    • Polymorphism
    • Encapsulation
    • None of the above

    Question 4

    Which of the following overloaded functions are NOT allowed in C++? 1) Function declarations that differ only in the return type
        int fun(int x, int y);
             void fun(int x, int y); 
    2) Functions that differ only by static keyword in return type
        int fun(int x, int y);
             static int fun(int x, int y); 
    3)Parameter declarations that differ only in a pointer * versus an array []
    int fun(int *ptr, int n);
    int fun(int ptr[], int n); 
    4) Two parameter declarations that differ only in their default arguments
    int fun( int x, int y); 
    int fun( int x, int y = 10); 
    • All of the above
    • All except 2)
    • All except 1)
    • All except 2 and 4

    Question 5

    Output of following program? CPP
    #include <iostream>
    using namespace std;
    
    int fun(int=0, int = 0);
    
    int main()
    {
      cout << fun(5);
      return 0;
    }
    
    int fun(int x, int y) { return (x+y); }
    
    • Compiler Error
    • 5
    • 0
    • 10

    Question 6

    Predict the output of following C++ program? C
    include<iostream>
    using namespace std;
     
    class Test
    {
    protected:
        int x;
    public:
        Test (int i):x(i) { }
        void fun() const  { cout << "fun() const " << endl; }
        void fun()        {  cout << "fun() " << endl;     }
    };
     
    int main()
    {
        Test t1 (10);
        const Test t2 (20);
        t1.fun();
        t2.fun();
        return 0;
    }
    
    • Compiler Error
    • fun()
      fun() const

    • fun() const
      fun() const

    • fun()
      fun()

    There are 6 questions to complete.

    Take a part in the ongoing discussion