C++ Programming Questions and Answers – seq_con Array Class – 1

«
»

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

Answer: b
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.
advertisement

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

Answer: a
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

Answer: d
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

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

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

Answer: a
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

Answer: b
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.
advertisement

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

Answer: b
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

Answer: b
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.
advertisement

10. Which header file is required to use get() function?
a) <array>
b) <tuple>
c) <Array>
d) <access>
View Answer

Answer: b
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

Answer: d
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

Answer: a
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

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

Answer: c
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
advertisement

b)

size:5
maxsize:10

c)

size:5
maxsize:5

d)

size:10
maxsize:5
View Answer
Answer: a
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.

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