C++ Programming Questions and Answers – Container Design

This section on advanced C++ programming questions focuses on “Container Design”. One shall practice these advanced C++ 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 advanced C++ questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of advanced C++ programming questions on “Container Design” along with answers, explanations and/or solutions:

1. How many sets of requirements are need in designing a container?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: There are three sets of requirements. They are container interface requirements, Allocator interface requirements and iterator requirements.

2. Which interface in the container is required for storage management?
a) Memory management
b) Allocator interface
c) Memory interface
d) Storage interface
View Answer

Answer: b
Explanation: Allocator interface in the container is required for storage management.

3. Which is present in the basic interface of the allocator interface?
a) Set of typedefs
b) A pair of allocation functions
c) allocate()
d) All of the mentioned
View Answer

Answer: d
Explanation: The basic interface of an allocator class consists of a set of typedefs, a pair of allocation functions, allocate() and deallocate() and a pair of construction/destruction members, construct() and destroy().
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     class Component
  5.     { 
  6.         public:
  7.         virtual void traverse() = 0;
  8.     };
  9.     class Leaf: public Component
  10.     {
  11.         int value;
  12.         public:
  13.         Leaf(int val)
  14.         {
  15.             value = val;
  16.         }
  17.         void traverse()
  18.         {
  19.             cout << value << ' ';
  20.         }
  21.     };
  22.     class Composite: public Component
  23.     {
  24.         vector < Component * > children;
  25.         public:
  26.         void add(Component *ele)
  27.         {
  28.             children.push_back(ele);
  29.         }
  30.         void traverse()
  31.         {
  32.             for (int i = 0; i < children.size(); i++)
  33.                 children[i]->traverse();
  34.         }
  35.     };
  36.     int main()
  37.     {
  38.         Composite containers[4];
  39.         for (int i = 0; i < 4; i++)
  40.             for (int j = 0; j < 3; j++)
  41.                 containers[i].add(new Leaf(i *3+j));
  42.             for (int k = 1; k < 4; k++)
  43.                 containers[0].add(&(containers[k]));
  44.             for (int p = 0; p < 4; p++)
  45.             {
  46.                 containers[p].traverse();
  47.             }
  48.     }

a) 345
b) 678
c) 901
d)

0 1 2 3 4 5 6 7 8 9 10 11 
3 4 5 
6 7 8 
9 10 11 
View Answer
Answer: d
Explanation: In this program, We are choosing and printing the numbers based on the certain limit and this is a composite design pattern.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ cont.cpp
$ a.out
0 1 2 3 4 5 6 7 8 9 10 11 
3 4 5 
6 7 8 
9 10 11
 
 

5. Which container provides random access iterators?
a) vector
b) deque
c) sort
d) both vector & deque
View Answer

Answer: d
Explanation: Vector & deque container provides random access iterators.
advertisement

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

advertisement
  1.     #include <iostream>
  2.     #include <vector>
  3.     #include <iterator>
  4.     #include <stddef.h>
  5.     using namespace std;
  6.     template<class myType>
  7.     class SimpleContainer
  8.     {
  9.         public:
  10.         SimpleContainer(size_t xDim, size_t yDim, myType const& defaultValue)
  11.         : objectData(xDim * yDim, defaultValue)
  12.         , xSize(xDim)
  13.         , ySize(yDim)
  14.         {
  15.         }
  16.         myType& operator()(size_t x, size_t y)
  17.         {
  18.             return objectData[y * xSize + x];
  19.         }
  20.         myType const& operator()(size_t x, size_t y) const 
  21.         {
  22.             return objectData[y * xSize + x];
  23.         }
  24.         int getSize()
  25.         {
  26.             return objectData.size();
  27.         }
  28.         void inputEntireVector(vector<myType> inputVector)
  29.         {
  30.             objectData.swap(inputVector);
  31.         }
  32.         void printContainer(ostream& stream)
  33.         {
  34.             copy(objectData.begin(), objectData.end(),
  35.             ostream_iterator<myType>(stream, ""/*No Space*/));
  36.         }
  37.         private:
  38.         vector<myType> objectData;
  39.         size_t  xSize;
  40.         size_t  ySize;
  41.     };
  42.     template<class myType>
  43.     inline ostream& operator<<(ostream& stream, SimpleContainer<myType>& object)
  44.     {
  45.         object.printContainer(stream);
  46.         return stream;
  47.     }
  48.     void sampleContainerInterfacing();
  49.     int main()
  50.     {
  51.         sampleContainerInterfacing();
  52.         return 0;
  53.     }
  54.     void sampleContainerInterfacing()
  55.     {
  56.         static int const ConsoleWidth  = 80;
  57.         static int const ConsoleHeight = 25;
  58.         size_t width  = ConsoleWidth;
  59.         size_t height = ConsoleHeight;
  60.         SimpleContainer<int> mySimpleContainer(width, height, 0);
  61.         cout << mySimpleContainer.getSize() << endl;
  62.         mySimpleContainer(0, 0) = 5;
  63.     }

a) 2000
b) No Space
c) Error
d) Depends on the compiler
View Answer

Answer: d
Explanation: In this program, We formed a simple container and got the size of it and printing it.
Output:

$ g++ cont1.cpp
$ a.out
200

7. Which is used for manually writing lookup table?
a) std:map
b) std:lookup
c) std:find
d) std:lock
View Answer

Answer: a
Explanation: Lookup table means storing values in a table with unique keys for each value so that can be checked in future easily. So for such lookup tables maps are used in C++.

8. How can the member functions in the container be accessed?
a) Iterator
b) Indirect
c) Both Iterator & Indirect
d) Vector
View Answer

Answer: a
Explanation: The container manages the storage space for its elements and provides member functions to access them, either directly or through iterators which reference objects with similar properties to pointers.

9. Which of the following type does the container should define?
a) Iterator type
b) Vector type
c) Storage type
d) Memory type
View Answer

Answer: a
Explanation: Every container must define an iterator type. Iterators allow algorithms to iterate over the container’s contents.

10. Which are the parameters for the content of the buffer?
a) start
b) finish
c) both start & finish
d) pause
View Answer

Answer: c
Explanation: The contents of the buffer are initialized using the values from the iterator range supplied to the constructor by the start and finish parameters.

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.