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

This set of Advanced C++ Programming Questions and Answers focuses on “seq_con Array Class – 2”.

1. What is the use of swap() function in array class?
a) Swaps two elements of an array given elements
b) Swaps two arrays
c) Swaps two elements given indices of elements
d) Swaps same elements of the array if required
View Answer

Answer: b
Explanation: swap() function is used to swap elements of two array classes provided the size of both arrays classes are same.

2. What is the syntax of swap()?
a) swap(arr1, arr2);
b) arr1.swap(arr2);
c) swap<int, int>(arr1, arr2);
d) swap[arr1, arr2];
View Answer

Answer: b
Explanation: The correct syntax of swap function is arr1.swap(arr2) i.e. one array calling swap() function with second array as parameter to swap function. Also swap is a function therefore [] operator cannot be used to call swap function.

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

advertisement
advertisement
#include <iostream>
#include <array>
 
using namespace std;
 
int main(int argc, char const *argv[])
{
	array<int, 5> arr1 = {1,2,3,4,5};
	array<int, 5> arr2 = {6,7,8,9,10};
	arr1.swap(arr2);
	for(int i=0;i<5;i++)
		cout<<arr1[i]<<" ";
	cout<<endl;
	for(int i=0;i<5;i++)
		cout<<arr2[i]<<" ";
	cout<<endl;
	return 0;
}

a)

6 7 8 9 10 
1 2 3 4 5
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

b)

1 2 3 4 5
6 7 8 9 10 

c)

6 7 8 9 10 
6 7 8 9 10 
advertisement

d)

1 2 3 4 5
1 2 3 4 5
View Answer
Answer: a
Explanation: arr1 has elements from 1-5 and arr2 has elements 6-10 initially. After swapping arr1 has elements from 6-10 and arr2 has elements from 1-5. Therefore output is 6 7 8 9 10 then 1 2 3 4 5.
Output:

$ ./a.out 
6 7 8 9 10 
1 2 3 4 5
 
 

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

advertisement
#include <iostream>
#include <array>
 
using namespace std;
 
int main(int argc, char const *argv[])
{
	array<int, 10> arr1 = {1,2,3,4,5};
	array<int, 5> arr2 = {6,7,8,9,10};
	arr1.swap(arr2);
	for(int i=0;i<5;i++)
		cout<<arr1[i]<<" ";
	cout<<endl;
	for(int i=0;i<5;i++)
		cout<<arr2[i]<<" ";
	cout<<endl;
	return 0;
}

a)

6 7 8 9 10 
1 2 3 4 5

b)

1 2 3 4 5
6 7 8 9 10

c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: As the size of both the array classes is not equal therefore the swap function gives an error stating that no matching function available.

5. What is the use of empty() function in array classes?
a) To check whether the size of an array is zero or not
b) To check whether an array is empty or not
c) To check how many elements are there in the array
d) To check whether an array contains negative elements or not
View Answer

Answer: a
Explanation: empty() function is used to check whether the size of an array class is zero or not. It is not used to check whether an array is empty or not. The function true only if size/max_size of an array is zero otherwise it returns false.

6. What is the use of fill() function in array class?
a) To fill an array with a given single value
b) To delete all the elements that are equal to the given value
c) To replace all the elements of the array which are equal to the given value
d) To check whether given element fills the array or not
View Answer

Answer: a
Explanation: fill() function is used to fill an array class with the given single value.

7. 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> arr1;
	arr1.fill(2);
	for(int i=0;i<5;i++)
		cout<<arr1[i];
	cout<<endl;
	return 0;
}

a) 22222
b) 20000
c) 00002
d) 20002
View Answer

Answer: a
Explanation: fill() function sets the value of each element equal to the value passed as parameter to the function.

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[])
{
	int arr1[5] = {1,2,3,4,5};
	int arr2[5] = {6,7,8,9,10};
	arr1.swap(arr2);
	for(int i=0;i<5;i++)
		cout<<arr1[i]<<" ";
	cout<<endl;
	for(int i=0;i<5;i++)
		cout<<arr2[i]<<" ";
	cout<<endl;
	return 0;
}

a)

6 7 8 9 10
1 2 3 4 5

b)

1 2 3 4 5
6 7 8 9 10

c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: swap() function is used for swapping two array classes not two C-like arrays. Therefore the swap() function gives error.

9. 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> arr1;
	arr1.fill(5);
	cout<<get<5>(arr1);
	return 0;
}

a) 5
b) Compile-time error
c) Run-time error
d) Segmentation fault
View Answer

Answer: b
Explanation: The compiler detects that the array class size is 5 and we are trying to access the 5th index which is out of bound therefore the program gives error.

10. What happens when both of the following C++ programs are compiled and executed?

===== Program 1 =====
#include <iostream>
#include <array>
 
using namespace std;
 
int main()
{
	array<int,5> arr1;
	arr1.fill(5);
	cout<<get<5>(arr1);
	return 0;
}
=====================
===== Program 2 =====
#include <iostream>
#include <array>
 
using namespace std;
 
int main()
{
	array<int,5> arr1;
	arr1.fill(5);
	cout<<arr1.at(5);
	return 0;
}
=====================

a) Program 1 gives compile-time error and Program 2 gives run-time error
b) Program 1 gives run-time error and Program 2 gives compile-time error
c) Both programs results into compile-time error
d) Both programs results into run-time error
View Answer

Answer: a
Explanation: The Program 1 gives compile-time error whereas Program 2 gives run-time error. This is because get() function takes constant integer as the argument for accessing element of the array, therefore at compile time only the compiler verifies whether the index is accessible or not as we know the array class size during compile time, Whereas in case of at() function it takes variable as the parameter for accessing element, therefore the index range is checked during run-time therefore the error is detected during run-time.

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.