C++ Multiple Choice Questions focuses on “Conversion Operators”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions comes with a detailed explanation of the answers which helps in better understanding of C++ concepts.
C++ Questions & Answers focuses on “Conversion Operators” along with answers, explanations and/or solutions:
Q 1. What is the return type of the conversion operator?
A. void
B. int
C. float
D. no return type
Show Answer
Answer:-D. no return typeExplanation
Conversion operator doesn’t have any return type not even void.Q 2. Why we use the “dynamic_cast” type conversion?
A. to be used in low memory
B. result of the type conversion is a valid
C. result of the type conversion is an invalid
D. it is used for storage
Show Answer
Answer:-B. result of the type conversion is a validExplanation
It is used to check that operators and operands are compatible after conversion.Q 3. How many parameters does a conversion operator may take?
A. 0
B. 1
C. 2
D. as many as possible
Show Answer
Answer:-A. 0Explanation
0 parameters does a conversion operator will take.Q 4. How are types therein user-defined conversion?
A. 1
B. 2
C. 3
D. 4
Show Answer
Answer:-B. 2Explanation
There are two types of user-defined conversions. They are conversion by the constructor, Conversion functions.Q 5. Pick out the correct syntax of operator conversion.
A. operator float()const
B. operator float()
C. operator const
D. operator const()
Show Answer
Answer:-A. operator float()constExplanation
The syntax of operator conversion is operator float()const.Q 6. 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
Show Answer
Answer:-D. runtime error or random numberExplanation
As it assigns to a reference to an object of another incompatible type using explicit type-casting. Output: $ g++ con.cpp $ a.out 14032334Q 7. 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
Show Answer
Answer:-B. 4Explanation
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 4Q 8. 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. 4 5
B. 5 5
C. 6 6
D. 7 5
Show Answer
Answer:-B. 5 5Explanation
In this program, we are calculating the magnitude value by two ways. Output: $ g++ con3.cpp $ a.out 55Q 9. 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
Show Answer
Answer:-A. convertedExplanation
In this program, We casted the string to the object of the class. Output: $ g++ con4.cpp $ a.out convertedQ 10. 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. 1210
B. 1210
C. 21
D. 2110
Leave a Reply