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
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
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
Explanation: There are three real types in complex numbers. They are float complex, double complex, long double complex.
4. What will be the output of the following C++ code?
#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<double> c1(4.0, 16.0), c2;
c2 = pow(c1, 2.0);
cout << c2;
return 0;
}
a) (-240, 128)
b) (240, 128)
c) (240, 120)
d) (240, -122)
View Answer
Explanation: In this program, we are finding the square of the complex number.
Output:
$ g++ comp.cpp $ a.out (-240,128)
5. What will be the output of the following C++ code?
#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<double> c_double(2, 3);
complex<int> c_int(4, 5);
c_double *= 2;
c_double = c_int;
cout << c_double;
return 0;
}
a) (2, 3)
b) (4, 5)
c) (8, 15)
d) (8, 10)
View Answer
Explanation: We are just copying the value of c_int into c_double, So it’s printing as (4,5).
Output:
$ g++ comp1.cpp $ a.out (4,5)
6. What will be the output of the following C++ code?
#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<int> i(2, 3);
i = i * 6 / 3;
cout << i;
return 0;
}
a) (4, 6)
b) (2, 3)
c) (6, 12)
d) (6, 15)
View Answer
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?
#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<double> c1(4.0,3.0);
cout << "c1: " << c1;
complex<float> c2(polar(5.0,0.75));
cout << c1 + complex<double>(c2.real(),c2.imag());
return 0;
}
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
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?
#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<double> c1(4.0, 3.0);
complex<float> c2(polar(5.0, 0.75));
cout << (c1 += sqrt(c1)) << endl;
return 0;
}
a) (4.0, 3.0)
b) (6.12132, 3.70711)
c) (5.0, 0.75)
d) (5.0, 3.75)
View Answer
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
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?
#include <iostream>
#include <complex>
using namespace std;
int main ()
{
complex<double> mycomplex (20.0, 2.0);
cout << imag(mycomplex) << endl;
return 0;
}
a) 2
b) 20
c) 40
d) 30
View Answer
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.
- Check C++ Books
- Apply for C++ Internship
- Apply for Computer Science Internship
- Check Programming Books
- Practice Computer Science MCQs