Here is a listing of C++ Programming Questions & Answers focuses on “Allocators” along with answers, explanations and/or solutions:
1. Where are allocators used?
a) Allocation of memory
b) Deallocation of memory
c) Used for pointers
d) Both Allocation & Deallocation of memory
View Answer
Explanation: Allocators handle all the request for allocation and deallocation of memory for the container.
2. Where are allocators implemented?
a) Template library
b) Standard library
c) C++ code library
d) String library
View Answer
Explanation: Allocators are implemented in C++ standard library but it is used for C++ template library.
3. Which operator is used to allocate the memory?
a) =
b) +
c) new
d) free
View Answer
Explanation: The default allocator uses operator new to allocate memory.
4. What will be the output of the following C++ code?
#include <iostream>
#include <memory>
#include <algorithm>
using namespace std;
int main ()
{
int numbers[] = {1, 5, 4, 5};
pair <int*, ptrdiff_t> result = get_temporary_buffer<int>(4);
if (result.second > 0)
{
uninitialized_copy (numbers, numbers + result.second, result.first);
sort (result.first, result.first + result.second);
for (int i = 0; i < result.second; i++)
cout << result.first[i] << " ";
return_temporary_buffer (result.first);
}
return 0;
}
a) 1 5 5 4
b) 5 5 4 1
c) 1 4 5 5
d) 1 4 5 2
View Answer
Explanation: In this program, We are sorting the array by using the allocator.
Output:
$ g++ all.cpp $ a.out 1 4 5 5
5. What will be the output of the following C++ code?
#include <iostream>
#include <memory>
#include <string>
using namespace std;
int main ()
{
string numbers[] = {"steve", "jobs"};
pair <string*, ptrdiff_t> result = get_temporary_buffer<string>(2);
if (result.second>0)
{
uninitialized_copy ( numbers, numbers + result.second, result.first );
for (int i = 0; i < result.second; i++)
cout << result.first[i] << " ";
return_temporary_buffer(result.first);
}
return 0;
}
a) steve
b) jobs
c) jobs steve
d) steve jobs
View Answer
Explanation: In this program, We are storing the string and retrieving the string by using get_temporary_method.
Output:
$ g++ all1.cpp $ a.out steve jobs
6. What will be the output of the following C++ code?
#include <iostream>
#include <memory>
#include <string>
using namespace std;
int main ()
{
pair <string*, ptrdiff_t>
result = get_temporary_buffer<string>(3);
if (result.second > 0)
{
uninitialized_fill ( result.first, result.first + result.second,
"Hai" );
for (int i=0; i<result.second; i++)
cout << result.first[i] ;
return_temporary_buffer(result.first);
}
return 0;
}
a) Hai
b) HaiHai
c) HaiHaiHai
d) HaiH
View Answer
Explanation: In this program, We storing the string in the string buffer and then we are printing it.
Output:
$ g++ all2.cpp $ a.out HaiHaiHai
7. Which operator is used to deallocate the memory?
a) destroy
b) free
c) empty
d) insert
View Answer
Explanation: free operator is used to deallocate the memory.
8. Which header file is used to manipulate the allocater?
a) allocater
b) memory
c) object
d) iterator
View Answer
Explanation: Because all the memory allocation and deallocation libraries are declared in <memory>.
9. What is the use of reference member type in allocator?
a) Point to an element
b) Quantities of element
c) Reference to an element
d) Sequence of an element
View Answer
Explanation: free() function is used to free the memory used by the program.
eg,
int *p = (int*) malloc(sizeof(int)); //allocation of memory.
free(p); // freeing the memory occupied by pointer p.
10. What is the correct syntax for declaring an allocator?
a) template < class T > class allocator;
b) template < class T > class;
c) template class allocator;
d) template class()
View Answer
Explanation: It is a type of syntax for declaring the allocater.
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]
- Check Programming Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check C++ Books