1. Pick the other name of operator function.
a) function overloading
b) operator overloading
c) member overloading
d) None of the mentioned

b) operator overloading

2. How to declare operator function?
a) operator operator sign
b) operator
c) operator sign
d) None of the mentioned

a) operator operator sign

3. Which of the following statements is NOT valid about operator overloading?
a) Only existing operators can be overloaded.
b) Overloaded operator must have at least one operand of its class type.
c) The overloaded operators follow the syntax rules of the original operator.
d) None of the mentioned

d) None of the mentioned

4. Operator overloading is:
a) making c++ operator works with objects
b) giving new meaning to existing operator
c) making new operator
d) both a & b

d) both a & b

5. The output of this program?

#include <iostream>
using namespace std;
ostream & operator<<(ostream & i, int n)
{
return i;
}
int main()
{
cout << 5 << endl;
cin.get();
return 0;
}
a) 5
b) 6
c) error
d) runtime error

c) error

6. Output of this program?

#include <iostream>
using namespace std;
class myclass
{
public:
int i;
myclass *operator->()
{return this;}
};
int main()
{
myclass ob;
ob->i = 10;
cout << ob.i << " " << ob->i;
return 0;
}
a) 10 10
b) 11 11
c) error
d) runtime error

a) 10 10

7. Output of this program?

#include <iostream>
using namespace std;
class Integer
{
int i;
public:
Integer(int ii) : i(ii) {}
const Integer
operator+(const Integer& rv) const
{
cout << "operator+" << endl;
return Integer(i + rv.i);
}
Integer&
operator+=(const Integer& rv)
{
cout << "operator+=" << endl;
i += rv.i;
return *this;
}
};
int main()
{
int i = 1, j = 2, k = 3;
k += i + j;
Integer ii(1), jj(2), kk(3);
kk += ii + jj;
}
a) operator+
operator+=
b) operator+=
operator+
c) operator+
operator+
d) None of the mentioned

a) operator+
operator+=

8. Output of Program?

#include <iostream>
using namespace std;
class sample
{
public:
int x, y;
sample() {};
sample(int, int);
sample operator + (sample);
};
sample::sample (int a, int b)
{
x = a;
y = b;
}
sample sample::operator+ (sample param)
{
sample temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main ()
{
sample a (4,1);
sample b (3,2);
sample c;
c = a + b;
cout << c.x << "," << c.y;
return 0;
}
a) 5, 5
b) 7, 3
c) 3, 7
d) None of the mentioned

b) 7, 3

9. Which of the following operators can't be overloaded?
a) ::
b) +
c) -
d) []

a) ::

10. Pick out the correct syntax of operator conversion.
a) operator float()const
b) operator float()
c) operator const
d) None of the mentioned

a) operator float()const

Download Interview PDF

11. How types are there in user defined conversion?
a) 1
b) 2
c) 3
d) 4

b) 2

12. What is the output of this program?

#include <iostream>
using namespace std;
int main()
{
double a = 21.09399;
float b = 10.20;
int c ;
c = (int) a;
cout << c ;
c = (int) b;
cout << c ;
return 0;
}
a) 2110
b) 1210
c) 21
d) None of the mentioned

a) 2110

13. What is the output of this program?

#include <iostream>
#include <string>
using namespace std;
class test
{
public:
operator string ()
{
return "Converted";
}
};
int main()
{
test t;
string s = t;
cout << s << endl;
return 0;
}
a) converted
b) error
c) run time error
d) None of the mentioned

a) converted

14. What is the output of this program?

#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)
{}
double mag()
{
return getMag();
}
operator double ()
{
return getMag();
}
private:
double getMag()
{
return sqrt(real * real + imag * imag);
}
};
int main()
{
Complex com(3.0, 4.0);
cout << com.mag();
cout << com;
return 0
}
a) 5 5
b) 4 5
c) 6 6
d) None of the mentioned

a) 5 5

15. program Output:

#include <iostream>
using namespace std;
class sample
{
public:
sample(int i) : m_i(i) { }
public:
int operator()(int i = 0) const
{
return m_i + i;
}
operator int () const
{
return m_i;
}
private:
int m_i;
friend int g(const sample&);
};
int f(char c)
{
return c;
}
int main()
{
sample f(2);
cout << f(2);
return 0;
}
a) 3
b) 4
c) 5
d) None of the mentioned

b) 4

16. What is the output of this program?

#include <iostream>
using namespace std;
class sample1
{
float i, j;
};
class sample2
{
int x, y;
public:
sample2 (int a, int b)
{
x = a;
y = b;
}
int result()
{
return x + y;
}
};
int main ()
{
sample1 d;
sample2 * padd;
padd = (sample2*) &d;
cout < result();
return 0;
}
a) 20
b) runtime error
c) random number
d) c or b

d) c or b

17. How many parameters does a conversion operator may take?
a) 0
b) 1
c) 2
d) as many as possible

a) 0

18. Why we use the "dynamic_cast" type conversion?
a) result of the type conversion is a valid
b) to be used in low memory
c) result of the type conversion is a invalid
d) None of the mentioned

a) result of the type conversion is a valid

19. What is the return type of the conversion operator?
a) void
b) int
c) float
d) no return type

d) no return type

20. Can you please explain the difference between overloaded functions and overridden functions?

Overloading is a static or compile-time binding and Overriding is dynamic or run-time binding.

Redefining a function in a derived class is called function overriding

A derived class can override a base-class member function by supplying a new version of that function with the same signature (if the signature were different, this would be function overloading rather than function overriding).

21. Explain overloading unary operator?

Unary operators are those which operate on a single variable. Overloading unary operator means extending the operator's original functionality to operate upon object of the class. The declaration of a overloaded unary operator function precedes the word operator.

For example, consider class 3D which has data members x, y and z and overloaded increment operators:

class 3D
{
int x, y, z;
public:
3D (int a=0, int b=0, int c=0)
{
x = a;
y = b;
z = c;
}
3D operator ++() //unary operator ++ overloaded
{
x = x + 1;
y = y + 1;
z = z + 1;
return *this; //this pointer which points to the caller object
}
3D operator ++(int) //use of dummy argument for post increment
operator
{
3D t = *this;
x = x + 1;
y = y + 1;
z = z + 1;
return t; //return the original object
}
3D show()
{
cout<<”The elements are:n”
cout<<”x:”<<this->x<<”, y:<this->y <<”, z:”<<this->z;
}
};
int main()
{
3D pt1(2,4,5), pt2(7,1,3);
cout<<”Point one's dimensions before increment are:”<< pt1.show();
++pt1; //The overloaded operator function ++() will return object's this pointer
cout<<”Point one's dimensions after increment are:”<< pt1.show();
cout<<”Point two's dimensions before increment are:”<< pt2.show();
pt2++; //The overloaded operator function ++() will return object's this pointer
cout<<”Point two's dimensions after increment are:”<< pt2.show();
return 0;
}

The o/p would be:
Point one's dimensions before increment are:
x:2, y:4, z:5
Point one's dimensions after increment are:
x:3, y:5, z:6
Point two's dimensions before increment are:
x:7, y:1, z:3
Point two's dimensions after increment are:
x:7, y:1, z:3

Please note in case of post increment, the operator function increments the value; but returns the original value since it is post increment.

22. Can you please explain function overloading?

A feature in C++ that enables several functions of the same name can be defined with different types of parameters or different number of parameters. This feature is called function overloading. The appropriate function will be identified by the compiler by examining the number or the types of parameters / arguments in the overloaded function. Function overloading reduces the investment of different function names and used to perform similar functionality by more than one function.

23. Can you please explain operator overloading?

A feature in C++ that enables the redefinition of operators. This feature operates on user defined objects. All overloaded operators provides syntactic sugar for function calls that are equivalent. Without adding to / changing the fundamental language changes, operator overloading provides a pleasant façade.

24. What is overloading template?

A template function overloads itself as needed. But we can explicitly overload it too. Overloading a function template means having different sets of function templates which differ in their parameter list. Consider following example:

#include <iostream>

template <class X> void func(X a)
{
// Function code;
cout <<”Inside f(X a) n”;
}

template <class X, class Y> void func(X a, Y b) //overloading function template func()
{
// Function code;
cout <<”Inside f(X a, Y b) n”;
}

int main()
{
func(10); // calls func(X a)
func(10, 20); // calls func(X a, Y b)
return 0;
}

25. Explain function overloading?

Function overloading is the process of using the same name for two or more functions. The secret to overloading is that each redefinition of the function must use either different types of parameters or a different number of parameters. It is only through these differences that the compiler knows which function to call in any situation.

Consider following program which overloads MyFunction() by using different types of parameters:

#include <iostream>
Using namespace std;
int MyFunction(int i);
double MyFunction(double d);
int main()
{
cout <<MyFunction(10)<<”n”; //calls MyFunction(int i);
cout <<MyFunction(5.4)<<”n”; //calls MyFunction(double d);
return 0;
}
int MyFunction(int i)
{
return i*i;
}

double MyFunction(double d)
{
return d*d;
}

Now the following program overloads MyFunction using different no of parameters

#include <iostream>
Using namespace std;
int MyFunction(int i);
int MyFunction(int i, int j);
int main()
{
cout <<MyFunction(10)<<”n”; //calls MyFunction(int i);
cout <<MyFunction(1, 2)<<”n”; //calls MyFunction(int i, int j);
return 0;
}

int MyFunction(int i)
{
return i;
}

double MyFunction(int i, int j)
{
return i*j;
}

Please note, the key point about function overloading is that the functions must differ in regard to the types and/or number of parameters. Two functions differing only in their return types can not be overloaded.

Download Interview PDF

26. Tell me what is operator overloading in C++?

C++ provides ability to overload most operators so that they perform special operations relative to classes. For example, a class String can overload the + operator to concatenate two strings.

When an operator is overloaded, none of its original meanings are lost. Instead, the type of objects it can be applied to is expanded. By overloading the operators, we can use objects of classes in expressions in just the same way we use C++'s built-in data types.

Operators are overloaded by creating operator functions. An operator function defines the operations that the overloaded operator will perform on the objects of the class. An operator function is created using the keyword operator.

For example: Consider class String having data members char *s, int size, appropriate function members and overloaded binary operator + to concatenate two string objects.

class String
{
char *s;
int sz;
public:
String(int n)
{
size = n;
s = new char [n];
}
void accept()
{
cout <<”n Enter String:”;
cin.getline(s, sz);
}
void display()
{
cout<<”The string is: ”<<s;
}
String operator +(String s2) // ‘+' operator overloaded
{
String ts(sz + s2.sz);
for(int i = 0; s[i]!='';i++)
ts.s[i] = s[i];
for(int j = 0; s2.s[j]!=''; i++, j++)
ts.s[i] = s2.s[j];
ts.s[i] = ‘';
return ts;
}
};

int main()
{
String s1, s2, s3;
s1.accept();
s2.accept();
s3 = s1 + s2; //call to the overloaded ‘+' operator
s3.display()
}

If you pass strings “Hello” and “World” for s1 and s2 respectively, it will concatenate both into s3 and display output as “HelloWorld”
Operator overloading can also be achieved using friend functions of a class.