C++ Programming Questions and Answers – Allocators

This section on C++ Programming Multiple Choice Questions focuses on “Allocators”. One shall practice these questions 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++ questions comes with detailed explanation of the answers which helps in better understanding of C++ concepts.

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

Answer: d
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

Answer: b
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

Answer: c
Explanation: The default allocator uses operator new to allocate memory.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <memory>
  3.     #include <algorithm>
  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
View Answer

Answer: c
Explanation: In this program, We are sorting the array by using the allocator.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ all.cpp
$ a.out
1 4 5 5

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

advertisement
  1.     #include <iostream>
  2.     #include <memory>
  3.     #include <string>
  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
View Answer

Answer: d
Explanation: In this program, We are storing the string and retrieving the string by using get_temporary_method.
Output:

advertisement
$ g++ all1.cpp
$ a.out
steve jobs

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

  1.     #include <iostream>
  2.     #include <memory>
  3.     #include <string>
  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
View Answer

Answer: c
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

Answer: b
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

Answer: b
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

Answer: c
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

Answer: a
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]

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.