C++ Function Overloading and Default Arguments

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


Question 1-Explanation: 
Both of the features allow one function name to work for different parameter.
Question 2
Output?
#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;
}
Cross
10
Cross
0
Cross
20
Tick
Compiler Error


Question 2-Explanation: 
All default arguments must be the rightmost arguments. The following program works fine and produces 10 as output.
#include <iostream>
using namespace std;

int fun(int x, int y = 0, int z = 0)
{  return (x + y + z); }

int main()
{
   cout << fun(10);
   return 0;
}
Question 3
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); 
Tick
All of the above
Cross
All except 2)
Cross
All except 1)
Cross
All except 2 and 4


Question 3-Explanation: 
Question 4
Predict the output of following C++ program?
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;
}
Cross
Compiler Error
Tick
fun()
fun() const
Cross
fun() const
fun() const
Cross
fun()
fun()


Question 4-Explanation: 
The two methods ‘void fun() const’ and ‘void fun()’ have same signature except that one is const and other is not. Also, if we take a closer look at the output, we observe that, ‘const void fun()’ is called on const object and ‘void fun()’ is called on non-const object. C++ allows member methods to be overloaded on the basis of const type. Overloading on the basis of const type can be useful when a function return reference or pointer. We can make one function const, that returns a const reference or const pointer, other non-const function, that returns non-const reference or pointer. See following for more details. Function overloading and const keyword
Question 5
Output of following program?
#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); }
Cross
Compiler Error
Tick
5
Cross
0
Cross
10


Question 5-Explanation: 
The statement "int fun(int=0, int=0)" is declaration of a function that takes two arguments with default values as 0 and 0. The last statement is definition of fun(). When we make a call fun(5), x gets the value 5 and y gets 0. So the returned value is 5.
Question 6
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;
}
Cross
910
Cross
920
Tick
870
Cross
900


There are 6 questions to complete.

  • Last Updated : 27 Sep, 2023

Similar Reads