Here is a listing of C++ Questions & Answers focuses on “Conversion Operators” along with answers, explanations and/or solutions:
1. What is the return type of the conversion operator?
a) void
b) int
c) float
d) no return type
View Answer
Explanation: Conversion operator doesn’t have any return type not even void.
2. 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 an invalid
d) it is used for storage
View Answer
Explanation: It is used to check that operators and operands are compatible after conversion.
3. How many parameters does a conversion operator may take?
a) 0
b) 1
c) 2
d) as many as possible
View Answer
Explanation: 0 parameters does a conversion operator will take.
4. What will be the output of the following C++ code?
#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<< padd->result();
return 0;
}
a) 20
b) runtime error
c) random number
d) runtime error or random number
View Answer
Explanation: As it assigns to a reference to an object of another incompatible type using explicit type-casting.
Output:
$ g++ con.cpp $ a.out 14032334
5. What will be the output of the following C++ code?
#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) 6
View Answer
Explanation: In this program, we are adding its value with it itself, So only we got the output as 4.
Output:
$ g++ con1.cpp $ a.out 4
6. What will be the output of the following C++ code?
#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) 7 5
View Answer
Explanation: In this program, we are calculating the magnitude value by two ways.
Output:
$ g++ con3.cpp $ a.out 55
7. What will be the output of the following C++ code?
#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) convertedconverted
View Answer
Explanation: In this program, We casted the string to the object of the class.
Output:
$ g++ con4.cpp $ a.out converted
8. What will be the output of the following C++ code?
#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) 121
View Answer
Explanation: In this program, we casted the data type to integer.
Output:
$ g++ con5.cpp $ a.out 2110
9. How are types therein user-defined conversion?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: There are two types of user-defined conversions. They are conversion by the constructor, Conversion functions.
10. Pick out the correct syntax of operator conversion.
a) operator float()const
b) operator float()
c) operator const
d) operator const()
View Answer
Explanation: The syntax of operator conversion is operator float()const.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Practice Programming MCQs
- Apply for C++ Internship
- Practice Computer Science MCQs
- Check C++ Books
- Apply for Computer Science Internship