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
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];
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?
#include <iostream>
#include <array>
using namespace std;
int main(int argc, char const *argv[])
{
array arr1 = {1,2,3,4,5};
array 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.
6 7 8 9 10
6 7 8 9 10
D.
1 2 3 4 5
1 2 3 4 5
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.
4. 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 arr1 = {1,2,3,4,5};
array 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
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
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
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 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
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
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 arr1;
arr1.fill(5);
cout<<get<5>(arr1);
return 0;
}
A. 5
B. Compile-time error
C. Run-time error
D. Segmentation fault
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
#include
using namespace std;
int main()
{
array arr1;
arr1.fill(5);
cout<<get<5>(arr1);
return 0;
}
=====================
===== Program 2 =====
#include
#include
using namespace std;
int main()
{
array 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
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.
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
Answer: D
Explanation:
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()
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()
Answer: B
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()
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.
- 10+ TOP C++ MCQs on OOPs Concept – 4
- 10+ TOP C++ MCQs on Floating Point Types
- 10+ TOP C++ MCQs on Sizes
- 10+ TOP C++ MCQs on References – 3
- 10+ TOP C++ MCQs on Operator Overloading – 1
- 10+ TOP C++ MCQs on Dereferencing
- 10+ TOP C++ MCQs on Increment and Decrement
- 10+ TOP C++ MCQs on Design of Class Hierarchies