250+ TOP MCQs on Allocators and Answers

This section on C++ Programming quiz on “Allocators”. One shall practice these quizzes to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ quiz comes with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ Programming quiz 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
Answer: d
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: The default allocator uses operator new to allocate memory.

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main ()
  6.     {
  7.         int numbers[] = {1, 5, 4, 5};
  8.         pair <int*, ptrdiff_t> result = get_temporary_buffer<int>(4);
  9.         if (result.second > 0)
  10.         {
  11.             uninitialized_copy (numbers, numbers + result.second, result.first);
  12.             sort (result.first, result.first + result.second);
  13.             for (int i = 0; i < result.second; i++)
  14.                 cout << result.first[i] << " ";
  15.             return_temporary_buffer (result.first);
  16.         }
  17.         return 0;
  18.     }

a) 1 5 5 4
b) 5 5 4 1
c) 1 4 5 5
d) 1 4 5 2
Answer: c
Clarification: 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?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main ()
  6.     {
  7.         string numbers[] = {"steve", "jobs"};
  8.         pair <string*, ptrdiff_t> result = get_temporary_buffer<string>(2);
  9.         if (result.second>0) 
  10.         {
  11.             uninitialized_copy ( numbers, numbers + result.second, result.first );
  12.             for (int i = 0; i < result.second; i++)
  13.                 cout << result.first[i] << " ";
  14.             return_temporary_buffer(result.first);
  15.         }
  16.         return 0;
  17.     }

a) steve
b) jobs
c) jobs steve
d) steve jobs
Answer: d
Clarification: 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?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         pair <string*, ptrdiff_t>
  8.        result = get_temporary_buffer<string>(3);
  9.         if (result.second > 0)
  10.         {
  11.             uninitialized_fill ( result.first, result.first + result.second, 
  12.             "Hai" );
  13.             for (int i=0; i<result.second; i++)
  14.                 cout << result.first[i] ;
  15.             return_temporary_buffer(result.first);
  16.         }
  17.         return 0;
  18.     }

a) Hai
b) HaiHai
c) HaiHaiHai
d) HaiH
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: b
Clarification: Because all the memory allocation and deallocation libraries are declared in .

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
Answer: c
Clarification: 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()
Answer: a
Clarification: It is a type of syntax for declaring the allocater.

  250+ TOP MCQs on Modularization and Interfaces and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top