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
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
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
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().
4. What will be the output of the following C++ code?
#include <iostream>
#include <vector>
using namespace std;
class Component
{
public:
virtual void traverse() = 0;
};
class Leaf: public Component
{
int value;
public:
Leaf(int val)
{
value = val;
}
void traverse()
{
cout << value << ' ';
}
};
class Composite: public Component
{
vector < Component * > children;
public:
void add(Component *ele)
{
children.push_back(ele);
}
void traverse()
{
for (int i = 0; i < children.size(); i++)
children[i]->traverse();
}
};
int main()
{
Composite containers[4];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 3; j++)
containers[i].add(new Leaf(i *3+j));
for (int k = 1; k < 4; k++)
containers[0].add(&(containers[k]));
for (int p = 0; p < 4; p++)
{
containers[p].traverse();
}
}
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 11View Answer
Explanation: In this program, We are choosing and printing the numbers based on the certain limit and this is a composite design pattern.
Output:
$ 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
Explanation: Vector & deque container provides random access iterators.
6. What will be the output of the following C++ code?
#include <iostream>
#include <vector>
#include <iterator>
#include <stddef.h>
using namespace std;
template<class myType>
class SimpleContainer
{
public:
SimpleContainer(size_t xDim, size_t yDim, myType const& defaultValue)
: objectData(xDim * yDim, defaultValue)
, xSize(xDim)
, ySize(yDim)
{
}
myType& operator()(size_t x, size_t y)
{
return objectData[y * xSize + x];
}
myType const& operator()(size_t x, size_t y) const
{
return objectData[y * xSize + x];
}
int getSize()
{
return objectData.size();
}
void inputEntireVector(vector<myType> inputVector)
{
objectData.swap(inputVector);
}
void printContainer(ostream& stream)
{
copy(objectData.begin(), objectData.end(),
ostream_iterator<myType>(stream, ""/*No Space*/));
}
private:
vector<myType> objectData;
size_t xSize;
size_t ySize;
};
template<class myType>
inline ostream& operator<<(ostream& stream, SimpleContainer<myType>& object)
{
object.printContainer(stream);
return stream;
}
void sampleContainerInterfacing();
int main()
{
sampleContainerInterfacing();
return 0;
}
void sampleContainerInterfacing()
{
static int const ConsoleWidth = 80;
static int const ConsoleHeight = 25;
size_t width = ConsoleWidth;
size_t height = ConsoleHeight;
SimpleContainer<int> mySimpleContainer(width, height, 0);
cout << mySimpleContainer.getSize() << endl;
mySimpleContainer(0, 0) = 5;
}
a) 2000
b) No Space
c) Error
d) Depends on the compiler
View Answer
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
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
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
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
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.
- Practice Computer Science MCQs
- Practice Programming MCQs
- Check Computer Science Books
- Check C++ Books
- Check Programming Books