250+ TOP MCQs on Valarray and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) 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
Answer: a
Clarification: Valarray is a special container provided by C++ to hold and perform mathematical operations on array efficiently.

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
Answer: a
Clarification: 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)
b)
c)
d)
Answer: b
Clarification: header file is required to use the functionalities of Valarrays.

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
Answer: c
Clarification: 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?

#include 
#include 
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

b)

10 2 20 1 30 
10 7 25 6 35

c)

10 2 20 1 30 
15 7 20 1 35

d) 15 7 25 6 35
Answer: a
Clarification: 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
Answer: b
Clarification: 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 
#include 
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
Answer: d
Clarification: 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
Answer: c
Clarification: 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
Answer: d
Clarification: 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 
#include 
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
Clarification: 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 
#include 
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
Clarification: 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
Answer: b
Clarification: 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 
#include 
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
Clarification: 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()
Answer: c
Clarification: 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()
Answer: d
Clarification: header provides max() function to print the maximum element from Valarray.

  250+ TOP MCQs on Pointers into Arrays and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top