1. What is the return type of the conversion operator?
A. void
B. int
C. float
D. no return type
Answer: D
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
Answer: A
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
Answer: A
Explanation:
0 parameters does a conversion operator will take.
4. What will be the output of the following C++ code?
#include
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. runtime error or random number
Answer: D
Explanation:
As it assigns to a reference to an object of another incompatible type using explicit type-casting.
5. What will be the output of the following C++ code?
#include
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
Answer: B
Explanation:
In this program, we are adding its value with it itself, So only we got the output as 4.
6. What will be the output of the following C++ code?
#include
#include
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
Answer: A
Explanation:
In this program, we are calculating the magnitude value by two ways.
7. What will be the output of the following C++ code?
#include
#include
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
Answer: A
Explanation:
In this program, We casted the string to the object of the class.
8. What will be the output of the following C++ code?
#include
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
Answer: A
Explanation:
In this program, we casted the data type to integer.
9. How are types therein user-defined conversion?
A. 1
B. 2
C. 3
D. 4
Answer: B
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()
Answer: A
Explanation:
The syntax of operator conversion is operator float()const.