250+ TOP MCQs on Conversion Operators and Answers

This section on C++ quiz on “Conversion Operators”. One shall practice these quizzes 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++ quiz comes with a detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ quiz 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
Answer: d
Clarification: 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
Clarification: 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
Clarification: 0 parameters does a conversion operator will take.

4. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     class sample1 
  4.     {
  5.         float i, j;
  6.     };
  7.     class sample2 
  8.     {
  9.         int x, y;
  10.         public:
  11.         sample2 (int a, int b) 
  12.         {
  13.              x = a; 
  14.              y = b;
  15.         }
  16.         int result() 
  17.         { 
  18.              return x + y;
  19.          }
  20.     };
  21.     int main () 
  22.     {
  23.         sample1 d;
  24.         sample2 * padd;
  25.         padd = (sample2*) &d;
  26.         cout<< padd->result();
  27.         return 0;
  28.     }

a) 20
b) runtime error
c) random number
d) runtime error or random number
Answer: d
Clarification: 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?

  1.     #include 
  2.     using namespace std;
  3.     class sample
  4.     {
  5.         public:
  6.         sample(int i) : m_i(i) { }
  7.         public:
  8.         int operator()(int i = 0) const 
  9.         { 
  10.             return m_i + i; 
  11.         }
  12.         operator int () const   
  13.         { 
  14.             return m_i; 
  15.         }
  16.         private:
  17.         int m_i;
  18.         friend int g(const sample&);
  19.     };
  20.     int f(char c)
  21.     {
  22.         return c;
  23.     }
  24.     int main()
  25.     {
  26.         sample f(2);
  27.         cout << f(2);
  28.         return 0;
  29.     }

a) 3
b) 4
c) 5
d) 6
Answer: b
Clarification: In this program, we are adding its value with it itself, So only we got the output as 4.
Output:

6. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     class Complex
  5.     {
  6.         private:
  7.         double real;
  8.         double imag;
  9.         public:
  10.         Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)
  11.         {}
  12.         double mag()
  13.         {  
  14.             return getMag();
  15.         }
  16.         operator double ()
  17.         {
  18.             return getMag();
  19.         }
  20.         private:
  21.         double getMag()
  22.         {
  23.             return sqrt(real * real + imag * imag);
  24.         }
  25.     };
  26.     int main()
  27.     {
  28.         Complex com(3.0, 4.0);
  29.         cout << com.mag();
  30.         cout << com;
  31.         return 0
  32.     }

a) 5 5
b) 4 5
c) 6 6
d) 7 5
Answer: a
Clarification: 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?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     class test
  5.     {
  6.         public:
  7.         operator string () 
  8.         {  
  9.             return "Converted";
  10.         }
  11.     };
  12.     int main()
  13.     {
  14.         test t;
  15.         string s = t;
  16.         cout << s << endl;
  17.         return 0;
  18.     }

a) converted
b) error
c) run time error
d) convertedconverted
Answer: a
Clarification: 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?

  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         double a = 21.09399;
  6.         float b = 10.20;
  7.         int c ;
  8.         c = (int) a;
  9.         cout << c ;
  10.         c = (int) b;
  11.         cout << c ;
  12.         return 0;
  13.     }

a) 2110
b) 1210
c) 21
d) 121
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Clarification: The syntax of operator conversion is operator float()const.

  250+ TOP MCQs on Standard Library Design and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top