C++ Programming Questions and Answers – Valarray

«
»

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Valarray”.

1. What is Valarray in C++?
a) Container for efficient operations on an array
b) Container for efficient printing of the array
c) Container for efficient value conversion of array
d) Container for efficient type conversion of array
View Answer

Answer: a
Explanation: Valarray is a special container provided by C++ to hold and perform mathematical operations on array efficiently.
advertisement

2. Which of the following is correct about Valarray?
a) Supports element-wise mathematical operations
b) Slower than a normal array
c) Harder to use
d) Can have only integer Valarrays
View Answer

Answer: a
Explanation: Valarray is good at performing mathematical operations. Also, Valarray supports element-wise mathematical operations. They are easier to use and can be of any type.

3. Which header file is required for using Valarray?
a) <array>
b) <Valarray>
c) <stl>
d) <algorithm>
View Answer

Answer: b
Explanation: <Valarray> header file is required to use the functionalities of Valarrays.
advertisement
advertisement

4. What is the use of apply() function in Valarray?
a) Returns new array after shifting elements by the given number
b) Returns the summation of all elements of the Valarray
c) Applies the manipulation provided to all the elements of the array
d) Returns new array after circular shifting elements by the given number
View Answer

Answer: c
Explanation: <Valarray> header provides apply() function to apply any manipulation passed to the function to all the elements in the Valarray.

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

advertisement
#include <iostream>
#include <Valarray>
using namespace std;
int main()
{
	Valarray<int> varr = { 10, 2, 20, 1, 30 };
	for (int &x: varr) cout << x << " ";
	cout<<endl;
	varr = varr.apply([](int x){return x+=5;});
	for (int &x: varr) cout << x << " ";
	return 0;
}

a)

10 2 20 1 30 
15 7 25 6 35
advertisement

b)

10 2 20 1 30 
10 7 25 6 35

c)

10 2 20 1 30 
15 7 20 1 35
advertisement

d) 15 7 25 6 35
View Answer

Answer: a
Explanation: In this program, we are trying to manipulate the Valarray by adding 5 to all the elements of the Valarray. So first we are printing the original array and then manipulated array.

6. What is the use of sum() function in Valarray?
a) Applies the manipulation provided to all the elements of the array
b) Returns the summation of all elements of Valarray
c) Returns new array after shifting elements by the given number
d) Returns new array after circular shifting elements by the given number
View Answer

Answer: b
Explanation: <Valarray> header provides sum() function to sum all the elements in the Valarray and returns the total sum.

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

#include <iostream>
#include <valarray>
using namespace std;
int main()
{
	valarray<int> varr = { 10, 2, 20, 1, 30 };
	cout<<"Sum of array: "<<varr.sum();
	return 0;
}

a) Sum of array: 20
b) Sum of array: 53
c) Sum of array: 12
d) Sum of array: 63
View Answer

Answer: d
Explanation: In this program we are trying to sum up all the elements of Valarray using the sum() function of the complex header.

8. What is the function of shift()?
a) Applies the manipulation provided to all the elements of the array
b) Returns the summation of all elements of Valarray
c) Returns new array after shifting elements by the given number
d) Returns new array after circular shifting elements by the given number
View Answer

Answer: c
Explanation: <Valarray> header provides shift() function to shift all the elements of the Valarray by a given number either to the left or to the right.

9. Which of the following is correct about the shift?
a) Returns new array after shifting elements by the given number
b) Shifts the elements towards left if the argument supplied is positive
c) Shifts the elements towards the right if the argument supplied is negative
d) All of the mentioned
View Answer

Answer: d
Explanation: shift() function is used to shift all the elements of the Valarray by a given number. the elements are shifted towards left if the number if positive and towards the right if the number if negative.

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

#include <iostream>
#include <Valarray>
using namespace std;
int main()
{
	Valarray<int> varr = { 1, 2, 3, 4, 5 };
	for (int &x: varr) cout << x << " ";
	cout<<endl;
	varr = varr.shift(2);
	for (int &x: varr) cout << x << " ";
	return 0;
}

a)

1 2 3 4 5 
3 4 5 0 0 

b)

1 2 3 4 5 
0 0 3 4 5

c)

1 2 3 4 5 
1 2 3 4 5

d)

1 2 3 4 5 
3 4 5 1 2
View Answer
Answer: a
Explanation: In this program we are trying to shift elements of Valarray towards left by 2 using the shift() function of the complex header.
 
 

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

#include <iostream>
#include <Valarray>
using namespace std;
int main()
{
	Valarray<int> varr = { 1, 2, 3, 4, 5 };
	for (int &x: varr) cout << x << " ";
	cout<<endl;
	varr = varr.shift(-3);
	for (int &x: varr) cout << x << " ";
	return 0;
}

a)

1 2 3 4 5 
1 2 0 0 0

b)

1 2 3 4 5 
0 0 0 1 2

c)

1 2 3 4 5 
3 4 5 1 2 

d)

1 2 3 4 5 
1 2 3 4 5
View Answer
Answer: b
Explanation: In this program we are trying to shift elements of Valarray towards right by 3 using the shift() function of the complex header.
 
 

12. Which of the following is correct about shift() and cshift()?

1) shift() makes some values of Valarray equal to 0 after shifting by a non-zero number
2) cshift() makes some values of Valarray equal to 0 after shifting by a non-zero number

a) 2 only
b) 1 only
c) Both 1 and 2
d) Neither 1 nor 2
View Answer

Answer: b
Explanation: After shifting by some non-zero number the shift() function makes some elements of Valarray equal to zero whereas cshift() does no such things.

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

#include <iostream>
#include <Valarray>
using namespace std;
int main()
{
	Valarray<int> varr = { 1, 2, 3, 4, 5 };
	for (int &x: varr) cout << x << " ";
	cout<<endl;
	varr = varr.cshift(-3);
	for (int &x: varr) cout << x << " ";
	return 0;
}

a)

1 2 3 4 5 
0 0 0 1 2

b)

1 2 3 4 5 
3 4 5 0 0 

c)

1 2 3 4 5 
3 4 5 1 2

d)

1 2 3 4 5 
0 0 0 0 0
View Answer
Answer: c
Explanation: In this program we are trying to circular shift elements of Valarray towards right by 3 using the cshift() function of the complex header.
 
 

14. Which function is used to swap two Valarray?
a) max()
b) min()
c) swap()
d) change()
View Answer

Answer: c
Explanation: <Valarray> header provides swap() function to swap two Valarrays.

15. Which function is used to print the maximum element from Valarray?
a) change()
b) min()
c) swap()
d) max()
View Answer

Answer: d
Explanation: <Valarray> header provides max() function to print the maximum element from Valarray.

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