This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “seq_con Array Class – 1”.
1. What is sequence container arrays?
a) C-like arrays
b) Template class sequence container, alternative for C-like arrays
c) Collection of data of the same type
d) Collection of objects
View Answer
Explanation: Sequence Containers arrays are an alternative for C-like arrays. It is a static continuous array that uses template classes with extended features for array implementation.
2. Pick the correct statement.
a) Sequence Container arrays know (somehow stores within) its size whereas C-like arrays do not
b) Sequence Container arrays have no advantage over C-like arrays
c) Sequence Container arrays are same as C-like arrays
d) Sequence Container arrays are also present in C
View Answer
Explanation: Sequence Containers Arrays stores its size within itself so need to pass extra size parameter when passing this array as an argument.
3. Which of the following is/are advantage(s) of Sequence Container arrays over C-like arrays?
a) Sequence Container arrays store its size within itself whereas C-like arrays do not
b) Sequence Container arrays are more efficient
c) Sequence Container arrays have no array decay problem whereas C-like arrays do have
d) All of the mentioned
View Answer
Explanation: Sequence Container arrays(a.k.a Array classes) somehow stores its size and it can be implemented efficiently. Also, Array classes do not have Array decay problem.
4. Which of the follwoing function(s) of Array classes are similar to [] operator?
a) at()
b) get()
c) both at() and get()
d) front()
View Answer
Explanation: Both at() and get() function are used to access the elements stored at i’th position of the array.
5. How many different ways are there to access an element of array classes at the ith position?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: There are three ways of accessing Array classes as mentioned below:
i. using [] operator(same as C-like arrays)
ii. using at() function available in array classes.
iii. using get() function not a member of the array class.
6. What header file is included to use array classes?
a) <array>
b) <Array>
c) <algorithm>
d) <ARRAY>
View Answer
Explanation: <array> header file is provided by the C++ to use array classes.
7. What is the correct syntax of declaring an array class?
a) array<type> arr;
b) array<type,size> arr;
c) Array<type> arr;
d) Array<type,size> arr;
View Answer
Explanation: The declaration of array class starts with a keyword array followed by <> specifying the type and size of array and then the name of the identifier. Example: array<int, 10> arr; arr is an array class of type in with size = 10.
8. What will be the output of the following C++ code?
#include <iostream> #include <array> using namespace std; int main(int argc, char const *argv[]) { array<int,5> arr = {1,2,3,4,5}; cout<<"Printing Using [] operator: "; for(int i=0;i<5;i++){ cout<<arr[i]<<" "; } cout<<endl; cout<<"Printing Using at() function: "; for(int i=0;i<5;i++){ cout<<arr.at(i)<<" "; } cout<<endl; return 0; }
a)
1 2 3 4 5 1 2 3 4 5
b)
Printing Using [] operator: 1 2 3 4 5 Printing Using at() function: 1 2 3 4 5
c)
Printing Using at() function: 1 2 3 4 5 Printing Using [] operator: 1 2 3 4 5
d) Printing Using at() function: 1 2 3 4 5
View Answer
Explanation: In this program we are trying to print the array first using [] operator then using the at() function of the array class.
Output:
$ ./a.out Printing Using [] operator: 1 2 3 4 5 Printing Using at() function: 1 2 3 4 5
9. What is the syntax of printing the first element of an array Arr using get() function?
a) Arr.get(0)
b) get<0>(Arr)
c) Arr.get[0]
d) get<0>[Arr]
View Answer
Explanation: To access the first element of an array class Arr using get() function, we use the following get<index>(Arr) where index is an integer constant number, not an identifier.
10. Which header file is required to use get() function?
a) <array>
b) <tuple>
c) <Array>
d) <access>
View Answer
Explanation: <tuple> header file is required to use the get() function for accessing an element.
11. What is the difference between get() and at()?
a) at() is available under <array> header file whereas get() is available under <tuple> header file
b) at() is a member function of array class whereas get() is not
c) get() takes array class as a parameter whereas at() takes a constant integer(i.e. index) as a parameter
d) all of the mentioned
View Answer
Explanation: get() and at() differ in various ways. get() is not a part of array class, get is available under <tuple> header and get() takes array class also as a parameter to access the element.
12. Which function is used to access the first element of an array class?
a) front()
b) start()
c) back()
d) first()
View Answer
Explanation: Array class provides front() function to access the first element of the array class.
13. Which function is used to access the last element of an array class?
a) end()
b) start()
c) back()
d) last()
View Answer
Explanation: Array class provides back() function to access the last element of the array class.
14. Which of the following function(s) is/are used to get the size of the array class?
a) size()
b) max_size()
c) both size() and max_size()
d) get_size()
View Answer
Explanation: Both size() and max_size() are used to get the size of array class. There is no difference between size() and max_size() of array class.
15. What will be the output of the following C++ code?
#include <iostream> #include <array> using namespace std; int main(int argc, char const *argv[]) { array<int,10> arr = {1,2,3,4,5}; cout<<"size:"<<arr.size()<<endl; cout<<"maxsize:"<<arr.max_size()<<endl; return 0; }
a)
size:10 maxsize:10
b)
size:5 maxsize:10
c)
size:5 maxsize:5
d)
size:10 maxsize:5View Answer
Explanation: Both size() and max_size() returns the same value i.e. the size of array defined during declaration. Therefore both prints the value 10.
Output:
$ ./a.out size:10 maxsize:10
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check C++ Books
- Apply for Computer Science Internship
- Apply for Information Technology Internship
- Check Programming Books
- Practice Computer Science MCQs