C++ Programming Questions and Answers – Complex Library – 2

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Complex Library – 2”.

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

#include <iostream>
#include <complex>
using namespace std;
int main()
{
	complex <double> cn(3.0, 5.0);
	cout<<"Complex number with magnitude "<<abs(cn)<<" 
        and phase angle "<<arg(cn)<<" is: 
        "<<polar(abs(cn), arg(cn))<<endl;
	return 0;
}

a) Complex number with magnitude 5.83095 and phase angle 1.03038 is: (3,5)
b) Complex number with magnitude 1.03038 and phase angle 5.83095 is: (3,5)
c) Complex number with magnitude 5.83095 and phase angle 5.83095 is: (3,5)
d) Complex number with magnitude 1.03038 and phase angle 1.03038 is: (3,5)
View Answer

Answer: a
Explanation: In this program we are trying to costruct a complex number using polar() function of <complex> header.
advertisement
advertisement

2. Which function is used to calculate the norm of a complex number?
a) abs()
b) norm()
c) mod()
d) square_sum()
View Answer

Answer: b
Explanation: <complex> header provides norm() function to calculate the norm of a complex number.

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
#include <iostream>
#include <complex>
using namespace std;
int main()
{
	complex <double> cn(3.0, 4.0);
	cout<<"Norm is: "<<norm(cn)<<endl;
	return 0;
}

a) 9
b) 16
c) 25
d) 5
View Answer

Answer: c
Explanation: In this program we are trying to calculate the norm of a complex number using norm() function of <complex> header.
advertisement

4. Which function is used to calculate the conjugate of a complex number?
a) conj()
b) reverse()
c) opp()
d) find_conj()
View Answer

Answer: a
Explanation: <complex> header provides conj() function to calculate the conjugate of a complex number.

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

advertisement
#include <iostream>
#include <complex>
using namespace std;
int main()
{
	complex <double> cn(3.0, 4.0);
	auto cnj = conj(cn);
	cout<<"Conjugate of "<<real(cn)<<"+("<<imag(cn)<<")i is: "<<real(cnj)<<"
        +("<<imag(cnj)<<")i"<<endl;
	return 0;
}

a) Conjugate of 3+(4)i is: 3+(4)i
b) Conjugate of 3+(4)i is: 3-(-4)i
c) Conjugate of 3+(4)i is: 3-(+4)i
d) Conjugate of 3+(4)i is: 3+(-4)i
View Answer

Answer: d
Explanation: The complex conjugate of a+ib is a-ib so conj() of 3+4i = 3-4i or conj(complex(3,4)) = complex(3,-4).

6. What is the use of proj() function?
a) Used to calculate the argument of a complex number
b) Used to calculate the conjugate of a complex number
c) Used to calculate the negative of a complex number
d) Used to calculate the projection of a complex number
View Answer

Answer: d
Explanation: <complex> header provides proj() function to calculate the projection of a complex number a + ib.

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

#include <iostream>
#include <complex>
using namespace std;
int main()
{
	complex <double> cn(3.0, 4.0);
	cout<<"proj"<<cn<<" : "<<proj(cn)<<endl;
	return 0;
}

a) proj(3,4) : (3,4)
b) proj(3,4) : (4,3)
c) proj(3,4) : (-3,-4)
d) proj(3,4) : (-3,4)
View Answer

Answer: a
Explanation: In this program we are trying to calculate the projection of a complex number complex(3,4) using proj() function of <complex> header the answer to which is (3,4).

8. What is the use of log() function in a complex?
a) To calculate the log of the imaginary part of a complex number
b) To calculate the log of rethe al part of a complex number
c) To calculate the log of a complex number
d) To calculate the log of the argument of a complex number
View Answer

Answer: c
Explanation: <complex> header provides log() function to calculate the logarithm of a complex number a + ib.

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

#include <iostream>
#include <complex>
using namespace std;
int main()
{
	complex <double> cn(3.0, 4.0);
	cout<<log(cn)<<endl;
	return 0;
}

a) 1.60944
b) (1.60944,0.927295)
c) 0.927295
d) 1.60944 + 0.927295
View Answer

Answer: b
Explanation: In this program we are trying to calculate the logarithm of a complex number complex(3,4) using log() function of <complex> header the answer to which is (1.60944,0.927295).

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

#include <iostream>
#include <complex>
using namespace std;
int main()
{
	complex <double> cn(3.0, 4.0);
	cout<<sin(cn)<<endl;
	return 0;
}

a) (3.85374,-27.0168)
b) 3.85374
c) -27.0168
d) 3.85374 – 27.0168
View Answer

Answer: a
Explanation: In this program we are trying to calculate the sine of a complex number complex(3,4) using sin() function of <complex> header the answer to which is (3.85374,-27.0168).

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.