Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ Operator Overloading Aptitude Questions and Answers
C++ Operator Overloading Aptitude: This section contains C++ Operator Overloading Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on March 01, 2021
1) There are the following statements that are given below, which of them are correct about operator overloading in C++?
- Operator overloading is used to redefine existing operators in C++.
- Operator overloading is a type of compile-time polymorphism.
- Operator overloading is a type of runtime polymorphism.
- We can overload all existing operators in C++.
Options:
- A and B
- A and C
- A, B, and D
- A, C, and D
Correct Answer - 3
A, B, and D
Explanation:
Statements A, B, and D are correct about operator overloading.
2) Which of the following operators cannot be overloaded in C++?
- ?:
- <
- <<
- sizeof()
Options:
- A and B
- A and D
- A, C, and D
- A, B, and C
Correct Answer - 2
A and D
Explanation:
The sizeof() and ternary operators cannot be overloaded in C++.
3) Which of the following keyword is used to overload operators in C++?
- overload
- operator
- operate
- op
Correct Answer - 2
operator
Explanation:
The operator keyword is used to overload operators in C++.
4) How can we overload an operator in C++?
- By defining a user-defined structure.
- By defining a user-defined function with the specified operator.
- By defining multiple functions with the same name.
- None of the above
Correct Answer - 2
By defining a user-defined function with the specified operator.
Explanation:
We can overload an operator by defining a user-defined function.
5) Which of the following is the correct syntax for operator overloading in C++?
- <return_type> <class_name>:: operator <op>(argument lists){}
- <class_name>:: <return_type> operator <op>(argument lists) {}
- <return_type> operator <return_type> operator <op>(argument lists) <op>(argument lists){}
- None of the above
Correct Answer - 1
<return_type> <class_name>:: operator <op>(argument lists){}
Explanation:
The 1st option is the correct syntax to overload an operator in C++.
6) Can we overload the "new" operator in C++?
- Yes
- No
Correct Answer - 2
No
Explanation:
We cannot overload the new operator in C++.
7) Can we use friend function to overload operators in C++?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
We can overload operators using a friend function.
8) The binary operator that overloaded through a member function takes only one explicit argument?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
Yes, the binary operator that overloaded through a member function takes only one explicit argument.
9) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Sample {
private:
int number;
public:
Sample()
{
number = 10;
}
void operator++()
{
number = number + 1;
}
void Display()
{
cout << number << endl;
}
};
int main()
{
Sample S;
S++;
S++;
S.Display();
return 0;
}
Options:
- 12
- 10
- Syntax error
- Garbage value
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error.
10) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Sample {
private:
int number;
public:
Sample()
{
number = 10;
}
void operator++()
{
number = number + 1;
}
void Display()
{
cout << number << endl;
}
};
int main()
{
Sample S;
++S;
++S;
S.Display();
return 0;
}
Options:
- 12
- 10
- Syntax error
- Garbage value
Correct Answer - 1
12
Explanation:
The above code will print "12" on the console screen.
11) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Sample {
private:
int number;
public:
Sample()
{
number = 10;
}
void operator++(int)
{
number = number + 1;
}
void Display()
{
cout << number << endl;
}
};
int main()
{
Sample S;
S++;
S++;
S.Display();
return 0;
}
Options:
- 12
- 10
- Syntax error
- Garbage value
Correct Answer - 1
12
Explanation:
The above code will print "12" on the console screen.
12) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Sample {
private:
int number;
public:
Sample()
{
number = 0;
}
Sample(int n)
{
number = n;
}
Sample operator+(Sample S)
{
Sample temp;
temp.number = number + S.number;
return temp;
}
void Display()
{
cout << number << endl;
}
};
int main()
{
Sample S1(10);
Sample S2(20);
Sample S3;
S3 = S1 + S2;
S2.Display();
return 0;
}
Options:
- 10
- 20
- 30
- Syntax error
Correct Answer - 2
20
Explanation:
The above code will print "20" on the console screen.
13) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Sample {
private:
int number;
public:
Sample()
{
number = 0;
}
Sample(int n)
{
number = n;
}
friend Sample operator+(Sample S1, Sample S2);
void Display()
{
cout << number << endl;
}
};
friend Sample operator+(Sample S1, Sample S2)
{
Sample temp;
temp.number = S1.number + S2.number;
return temp;
}
int main()
{
Sample S1(10);
Sample S2(20);
Sample S3;
S3 = S1 + S2;
S3.Display();
return 0;
}
Options:
- 10
- 20
- 30
- Syntax error
Correct Answer - 4
Syntax error
Explanation:
The above code will generate a syntax error.
14) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Sample {
private:
int number;
public:
Sample()
{
number = 0;
}
Sample(int n)
{
number = n;
}
friend Sample operator+(Sample S1, Sample S2);
void Display()
{
cout << number << endl;
}
};
Sample operator+(Sample S1, Sample S2)
{
Sample temp;
temp.number = S1.number + S2.number;
return temp;
}
int main()
{
Sample S1(10);
Sample S2(20);
Sample S3;
S3 = S1 + S2;
S3.Display();
return 0;
}
Options:
- 10
- 20
- 30
- Synatx error
Correct Answer - 3
30
Explanation:
The above code will print "30" on the console screen.
15) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Sample {
private:
int number;
public:
Sample()
{
number = 0;
}
Sample(int n)
{
number = n;
}
friend Sample operator++(Sample S, int);
void Display()
{
cout << number << endl;
}
};
Sample operator++(Sample S, int)
{
S.number = S.number + 1;
return S;
}
int main()
{
Sample S1(10);
S1++;
S1.Display();
return 0;
}
Options:
- 10
- 11
- Garbage value
- Syntax error
Correct Answer - 4
Syntax error
Explanation:
The above code will generate a syntax error.
16) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Sample {
private:
int number;
public:
Sample()
{
number = 0;
}
Sample(int n)
{
number = n;
}
friend Sample operator++(Sample S);
void Display()
{
cout << number << endl;
}
};
Sample operator++(Sample S)
{
S.number = S.number + 1;
return S;
}
int main()
{
Sample S1(10);
++S1;
S1.Display();
return 0;
}
Options:
- 10
- 11
- Garbage value
- Syntax error
Correct Answer - 1
10
Explanation:
The above code will print "10" on the console screen.
17) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Sample {
private:
float num1, num2;
public:
Sample(float n1, float n2)
{
num1 = n1;
num2 = n2;
}
operator float() const
{
return float(num1) / float(num2);
}
};
int main()
{
Sample S(11.0, 2.0);
float num = S;
cout << num << endl;
return 0;
}
Options:
- 5.5
- Garbage value
- Syntax error
- Runtime error
Correct Answer - 1
5.5
Explanation:
The above code will print "5.5" on the console screen.
18) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Sample {
private:
int num1, num2;
public:
Sample(int n1, int n2)
{
num1 = n1;
num2 = n2;
}
operator int()
{
return int(num1) / int(num2);
}
};
int main()
{
Sample S(11.0, 2.0);
int num = S;
cout << num << endl;
return 0;
}
Options:
- 5.5
- 5
- Garbage value
- Syntax error
Correct Answer - 2
5
Explanation:
The above code will print "5" on the console screen.
19) Can we overload membership operator (.) in C++?
- Yes
- No
Correct Answer - 2
No
Explanation:
No, we cannot overload the membership operator(.) in C++.
20) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Sample {
private:
int number;
public:
Sample()
{
number = 0;
}
Sample(int n)
{
number = n;
}
friend Sample operator ::(Sample S1, Sample S2);
void Display()
{
cout << number << endl;
}
};
Sample operator ::(Sample S1, Sample S2);
{
Sample S;
S.number = S1.number + S2.number;
return S;
}
int main()
{
Sample S1(10);
Sample S2(20);
Sample S3;
S3 = S1::S2
S1.Display();
return 0;
}
Options:
- 30
- Garbage value
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error. Because we cannot overload the scope resolution operator.