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

«
»

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

1. Which header file is required to use complex class in your program?
a) <complex>
b) <math>
c) <complex_math>
d) <algorithm>
View Answer

Answer: a
Explanation: <complex> header file is required to use the functionalitites of complex numbers.
advertisement

2. Which of the following is the correct syntax of declaring a complex number?
a) complex variable_name<type>;
b) complex<type> variable_name;
c) Complex<type> variable_name;
d) Complex variable_name<type>;
View Answer

Answer: b
Explanation: The correct syntax of declaring a complex number object is complex<type> variable_name.

3. Which function is used to get the real part of the complex number?
a) img_p()
b) imag_p()
c) real()
d) real_p()
View Answer

Answer: c
Explanation: The real() function is provided by the complex <header> to access the real part of a complex number object.
advertisement
advertisement

4. Which function is used to get the imaginary part of the complex number?
a) real()
b) imag()
c) imag_p()
d) real_p()
View Answer

Answer: b
Explanation: The imag() function is provided by the complex <header> to access the imaginary part of a complex number object.

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, 5.0);
	cout<<"Complex number is: "<<real(cn)<<" + "<<imag(cn)<<"i"<<endl;
	return 0;
}

a) Complex number is: 3 + 5i
b) Complex number is: 5 + 3i
c) Complex number is: 9 + 25i
d) Complex number is: 3 – 5i
View Answer

Answer: a
Explanation: The first part in the constructor of complex object denotes real part and second part denotes the imaginary part hence the complex number is 3 + 5i.
advertisement

6. Which function is used to get the absolute of a complex number?
a) ret()
b) norm()
c) mod()
d) abs()
View Answer

Answer: d
Explanation: abs() function is provided by the header to calculate the absolute value of a complex number.

7. 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, 5.0);
	cout<<"Absolute value is: "<<abs(cn)<<endl;
	return 0;
}

a) Absolute value is: 4
b) Absolute value is: 5
c) Absolute value is: 3
d) Absolute value is: 5.83095
View Answer

Answer: d
Explanation: In this program we are trying to print the absolute value of a complex number using abs() function of <complex> header.

8. Which function is used to get the argument of a complex number?
a) abs()
b) norm()
c) arg()
d) argu()
View Answer

Answer: c
Explanation: The argument of a complex is calculated using the arg() function of the <complex> header.

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, 5.0);
	cout<<arg(cn)<<endl;
	return 0;
}

a) 1.03038
b) 0
c) Not defined
d) Error
View Answer

Answer: a
Explanation: In this program we are trying to print the argument value of a complex number using arg() function of <complex> header.

10. What is the use of polar function?
a) Used to construct a complex number from the real and imaginary part
b) Used to construct a complex number from magnitude and phase angle
c) Used to construct a complex number from the magnitude and real part
d) Used to construct a complex number from argument and phase angle
View Answer

Answer: b
Explanation: The polar() function of a complex header is used to construct the complex number using the magnitude and phase angle.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

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!

advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter