Test Series - cpp

Test Number 67/102

Q: 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
Solution: swap() function is used to swap elements of two array classes provided the size of both arrays classes are same.
Q: What is the syntax of swap()?
A. swap(arr1, arr2);
B. arr1.swap(arr2);
C. swap(arr1, arr2);
D. swap[arr1, arr2];
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: fill() function is used to fill an array class with the given single value.
Q: What are the vectors?
A. Arrays with dynamic size
B. Arrays with different types of elements
C. Same as array classes
D. Arrays with static size but use template classes
Solution: Vectors are just like arrays with the ability to resize itself whenever an element is added to or deleted from it.
Q: Pick the correct statement.
A. Vectors have dynamic size whereas Array classes have a static size
B. Both vectors and Array classes have a dynamic size
C. Both vectors and Array classes have a static size
D. Vectors have static size whereas Array classes have a dynamic size
Solution: Vectors are implemented in a way so that it can handle any number of elements at a time means the size of a vector can vary, whereas Array classes have fixed size.
Q: Pick the incorrect statement.
A. Vectors have a dynamic size
B. Vectors are placed in contiguous storage
C. Insertion in vectors always takes constant time
D. Vectors insert the element at the end
Solution: Insertion in vectors are not always constant. When we are inserting an element at the end of the vector then if a vector is full then it needs to size itself which takes time to resize and time to insert element else just time for inserting that element at the end. Hence the insertion time is not constant always. Vectors have a dynamic size. They are placed in contiguous memory for easy access.
Q: Which of the following header file is needed to use vectors in your program?
A. 
B. 
C. 
D. 
Solution: Header file  contains all the implementation of vector methods, hence we need to include this header file.
Q: Which of the following(s) can be used to access the first element of a vector v?
A. v.begin()
B. v.cbegin()
C. v[0]
D. all of the mentioned
Solution: To access the first element of a vector we can use the following things:
i) v.begin()
ii) v.cbegin()
iii) v[0]
iv) v.at(0)
Q: Which of the following(s) can be used to access the last element of a vector v?
A. v.end()
B. v.cend()
C. both v.end() and v.cend()
D. vectors do not have a function to access the last element
Solution: There are no function to access the last element of the vector. The end() and cend() returns the iterator to an element which is kept at the last of the vector to keep the knowledge about the end of a vector. In order to access the last element, you can first find the size and then can use v[size-1] or v.at(size – 1) to access the last element.

You Have Score    /10