C++ Programming Questions and Answers – Conversion Operators

This section on 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.

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

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
View Answer

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
View Answer

Answer: a
Explanation: 0 parameters does a conversion operator will take.
advertisement
advertisement

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

  1.     #include <iostream>
  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
View Answer

Answer: d
Explanation: As it assigns to a reference to an object of another incompatible type using explicit type-casting.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ con.cpp
$ a.out
14032334

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

advertisement
  1.     #include <iostream>
  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
View Answer

Answer: b
Explanation: In this program, we are adding its value with it itself, So only we got the output as 4.
Output:

advertisement
$ g++ con1.cpp
$ a.out
4

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

  1.     #include <iostream>
  2.     #include <cmath>
  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
View Answer

Answer: a
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?

  1.     #include <iostream>
  2.     #include <string>
  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
View Answer

Answer: a
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?

  1.     #include <iostream>
  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
View Answer

Answer: a
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

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()
View Answer

Answer: a
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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.