C++ Programming Questions and Answers – Complex Number Type

This section on C++ interview questions and answers focuses on “Complex Number Type”. One shall practice these interview 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++ interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Complex Number Type” along with answers, explanations and/or solutions:

1. Which header file is used to declare the complex number?
a) complexnum
b) complex
c) complex number
d) complexarg
View Answer

Answer: b
Explanation: <complex> header file is used for declaring a complex number in C++.

2. How to declare the complex number?
a) (3, 4)
b) complex(3, 4)
c) (3, 4i)
d) (3, 4g)
View Answer

Answer: b
Explanation: We can declare the complex number by using complex(3,4) where 3 is a real number and 4 is imaginary part.

3. How many real types are there in complex numbers?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: There are three real types in complex numbers. They are float complex, double complex, long double complex.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <complex>
  3.     using namespace std;
  4.     int main() 
  5.     {
  6.        complex<double> c1(4.0, 16.0), c2;
  7.        c2 = pow(c1, 2.0);
  8.        cout << c2;
  9.        return 0;          
  10.     }

a) (-240, 128)
b) (240, 128)
c) (240, 120)
d) (240, -122)
View Answer

Answer: a
Explanation: In this program, we are finding the square of the complex number.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ comp.cpp
$ a.out
(-240,128)

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

advertisement
  1.     #include <iostream>
  2.     #include <complex>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         complex<double> c_double(2, 3);
  7.         complex<int> c_int(4, 5);
  8.         c_double *= 2;
  9.         c_double = c_int;
  10.         cout << c_double;
  11.         return 0;
  12.     }

a) (2, 3)
b) (4, 5)
c) (8, 15)
d) (8, 10)
View Answer

Answer: b
Explanation: We are just copying the value of c_int into c_double, So it’s printing as (4,5).
Output:

advertisement
$ g++ comp1.cpp
$ a.out
(4,5)

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

  1.     #include <iostream>
  2.     #include <complex>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         complex<int> i(2, 3);
  7.         i = i * 6 / 3;
  8.         cout << i;
  9.         return 0;
  10.     }

a) (4, 6)
b) (2, 3)
c) (6, 12)
d) (6, 15)
View Answer

Answer: a
Explanation: We are multiplying the complex number by 2.
Output:

$ g++ comp2.cpp
$ a.out
(4,6)

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

  1.     #include <iostream>
  2.     #include <complex>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.        complex<double> c1(4.0,3.0);
  7.        cout << "c1: " << c1;
  8.        complex<float> c2(polar(5.0,0.75));
  9.        cout << c1 + complex<double>(c2.real(),c2.imag());
  10.        return 0;
  11.     }

a) c1: (4,3)(7.65844,6.40819)
b) c1: (4,3)(7,6)
c) both c1: (4,3)(7.65844,6.40819) & c1: (4,3)(7,6)
d) c1: (5,3)(7,6)
View Answer

Answer: a
Explanation: We are adding the two complex numbers and printing the result.
Output:

$ g++ comp3.cpp
$ a.out
c1: (4,3)(7.65844,6.40819)

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

  1.     #include <iostream>
  2.     #include <complex>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         complex<double> c1(4.0, 3.0);
  7.         complex<float> c2(polar(5.0, 0.75));
  8.         cout  << (c1 += sqrt(c1)) << endl;
  9.         return 0;
  10.     }

a) (4.0, 3.0)
b) (6.12132, 3.70711)
c) (5.0, 0.75)
d) (5.0, 3.75)
View Answer

Answer: b
Explanation: In this program, we are adding both complex number and finding the square root of it.
Output:

$ g++ comp4.cpp
$ a.out
(6.12132,3.70711)

9. Which of the following is not a function of complex values?
a) real
b) imag
c) norm
d) cartesian
View Answer

Answer: d
Explanation: Real is used for returning real part, imag for imaginary part and norm for calculating norm of a complex number. There is no such function Cartesian in complex header file.

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

  1.     #include <iostream>
  2.     #include <complex>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         complex<double> mycomplex (20.0, 2.0);
  7.         cout << imag(mycomplex) << endl;
  8.         return 0;
  9.     }

a) 2
b) 20
c) 40
d) 30
View Answer

Answer: a
Explanation: imag part will return the imaginary part of the complex number.
Output:

$ g++ comp5.cpp
$ a.out
2

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.