C++ Programming Questions and Answers – Array Type Manipulation

«
»

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

1. Which of the header file is used for array type manipulation?
a) <array>
b) <type_traits>
c) <iostream>
d) std namespace
View Answer

Answer: d
Explanation: Array type manipulation functions are declared incside the namespace std so you can use namespace std to use these functions.
advertisement

2. What is the use of is_array() function in C++?
a) To check if a variable is array type or not
b) To check if a variable is 1-D array type or not
c) To check if a variable is 2-D array type or not
d) To check if a variable is 1-D or 2-D array type or not
View Answer

Answer: a
Explanation: is_array() function is used to check whether a given variable is of array type or not.

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

#include <iostream>
#include <string>
using namespace std;
int main()
{
	cout<<is_array<int>::value;
	cout<<is_array<char[10]>::value;
	cout<<is_array<string>::value;
	return 0;
}

a) 010
b) 101
c) 001
d) 110
View Answer

Answer: a
Explanation: As int and string are not of array type therefore 0 is printed corresponding to them and char[10] is an array of character of size 10 therefore 1 is printed corresponding to this. Hence answer is 010.

4. What is the use of is_same() function in C++?
a) To check if a variable is array type or not
b) To check whether two variables have the same characteristics
c) To check if two variable is of array type or not
d) To check whether two variables are different or not
View Answer

Answer: b
Explanation: is_same() function is used to check whether two variables have the same characteristics or not.

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

advertisement
#include <iostream>
#include <string>
using namespace std;
int main()
{
	cout<<is_same<int,char>::value;
	cout<<is_same<char[10], char[10]>::value;
	cout<<is_same<char*[10], string>::value;
	return 0;
}

a) 011
b) 101
c) 010
d) 110
View Answer

Answer: c
Explanation: In 1st and 3rd case both the variables passed to is_same() function are different whereas for 2nd they are same. Hence the answer is 010.

6. What is the use of rank() function in C++?
a) Returns size of each dimension
b) Returns how many total elements can be stored in an array
c) Returns how many elements are in array currently
d) Returns the dimension of an array
View Answer

Answer: d
Explanation: rank() function returns the rank of the array i.e. the dimension of an array. For example, int arr[10][10] has rank 2.

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

#include <iostream>
#include <string>
using namespace std;
int main()
{
	cout<<rank<int[10]>::value;
	cout<<rank<char[10][10]>::value;
	cout<<rank<string[10][10][10]>::value;
	return 0;
}

a) 111
b) 123
c) 321
d) 121
View Answer

Answer: b
Explanation: In this program first array has the single dimension, second one has two dimensions and third one has three dimension therefore the program prints 123.

8. Which of the following is correct about extent() function?
a) Returns how many elements are in array currently
b) Returns the size of the 1st dimension
c) Returns how many total elements can be stored in an array
d) Returns the size of a given dimension
View Answer

Answer: d
Explanation: The extent() function takes two parameters one denoting the array other showing the dimension for which the size we want to know.
advertisement

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

#include <iostream>
#include <string>
using namespace std;
int main()
{
	cout<<extent<string[10][20][30], 0>::value;
	cout<<extent<string[10][20][30], 1>::value;
	cout<<extent<string[10][20][30], 2>::value;
	return 0;
}

a) 101010
b) 102030
c) 302010
d) 102010
View Answer

Answer: b
Explanation: In first cout we are passing 0 and size of first dimension of array is 10 therefore 10 is printed. In following cases we have passed 1 and 2 therefore 20 and 30 are printed respectively.

10. Which of the following is correct about remove_extent() function?
a) Removes the given dimension from an array
b) Removes the first dimension from the right of the array
c) Removes the first dimension from the left of the array
d) Removes the last dimension from the left of the array
View Answer

Answer: c
Explanation: remove_extent() function removes the first dimension i.e. the first dimension from the given array.

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

#include <iostream>
#include <string>
using namespace std;
int main()
{
	cout<<rank<remove_extent<string[10][20]>::type>::value;
	cout<<rank<remove_extent<string[10][20][30]>::type>::value;
	return 0;
}

a) 11
b) 12
c) 21
d) 22
View Answer

Answer: b
Explanation: As we are removing the dimensions from these array and then printing the rank of arrays. Therefore as initially they have 2 and 3 as their rank so after deleting the rank becomes 1 and 2 hence the output is 12.

12. Which of the following is correct about remove_all_extents() function?
a) Removes the all dimension from an array
b) Removes the first dimension from the left of the array
c) Removes the first dimension from the right of the array
d) Removes the last dimension from the left of the array
View Answer

Answer: a
Explanation: As the name suggests remove_all_extent() function removes all the dimensions from the array. So rank os array after this operation becomes 0.
advertisement

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

#include <iostream>
#include <string>
using namespace std;
int main()
{
	cout<<rank<remove_all_extents<string[10][20]>::type>::value;
	return 0;
}

a) 1
b) 0
c) Error
d) Segmentation fault
View Answer

Answer: b
Explanation: As we ahve deleted all the dimensions of this array therefore the rank of the array becomes zero hence the output is 0.

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

#include <iostream>
#include <string>
using namespace std;
int main()
{
	cout<<extent<remove_extent<string[10][20]>::type>::value;
	cout<<extent<remove_extent<string[10][20][30]>::type>::value;
	return 0;
}

a) 1010
b) 1020
c) 2020
d) 2030
View Answer

Answer: c
Explanation: As we are deleting the first dimension from both the arrays and then printing the extent i.e. size of dimension therefore the answer is 2020 as both the array have 20 as the size of their second dimension.

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

#include <iostream>
#include <string>
using namespace std;
int main()
{
	cout<<extent<remove_all_extents<string[10][20][30]>::type>::value;
	return 0;
}

a) 20
b) 10
c) Error
d) 0
View Answer

Answer: d
Explanation: As we have removed all the dimensions from the array therefore the output of extent is 0.

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

advertisement
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