C++ Arrays & Strings | Set 3
21. Which of the header file is used for array type manipulation?
a) <array>
b) <type_traits>
c) <iostream>
d) std namespace
Answer: D
Explanation: Array type manipulation functions are declared incside the namespace std so you can use namespace std to use these functions.
22. 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
Answer: A
Explanation: is_array() function is used to check whether a given variable is of array type or not.
23. 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
Answer: B
Explanation: is_same() function is used to check whether two variables have the same characteristics or not.
24. 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
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.
25. What will be the output of the following C++ code?
#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
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.
26. 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
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.
27. 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
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.
28. 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
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.
29. 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
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.
30. 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
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.