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 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.
C++ interview questions on “Complex Number Type” along with answers, explanations and/or solutions:
Q 1. Which header file is used to declare the complex number?
A. complexnum
B. complex number
C. complex
D. complexarg
Show Answer
Answer:-C. complexExplanation
Q 2. How to declare the complex number?
A. (3, 4)
B. complex(3, 4)
C. (3, 4i)
D. (3, 4g)
Show Answer
Answer:-B. complex(3, 4)Explanation
We can declare the complex number by using complex(3,4) where 3 is a real number and 4 is imaginary part.Q 3. How many real types are there in complex numbers?
A. 1
B. 2
C. 3
D. 4
Show Answer
Answer:-C. 3Explanation
There are three real types in complex numbers. They are float complex, double complex, long double complex.Q 4. Which of the following is not a function of complex values?
A. real
B. imag
C. norm
D. cartesian
Show Answer
Answer:-D. cartesianExplanation
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.Q 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)
Show Answer
Answer:-B. (4, 5)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)Q 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. (2, 3)
B. (4, 6)
C. (6, 12)
D. (6, 15)
Show Answer
Answer:-B. (4, 6)Explanation
We are multiplying the complex number by 2. Output: $ g++ comp2.cpp $ a.out (4,6)Q 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)
Show Answer
Answer:-A. c1: (4,3)(7.65844,6.40819)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)Q 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)
Show Answer
Answer:-B. (6.12132, 3.70711)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)Q 9. 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
Show Answer
Answer:-A. 2Explanation
imag part will return the imaginary part of the complex number. Output: $ g++ comp5.cpp $ a.out 2Q 10. 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)
Leave a Reply