Questions and Answers
- What is the output of this program?
#include <iostream>
#include <vector>
#include <iterator>
#include <stddef.h>
using namespace std;
template<class myType>
class Container
{
public:
Container(size_t xDim, size_t yDim, myType const& defaultValue)
: objData(xDim * yDim, defaultValue)
, xSize(xDim)
, ySize(yDim)
{
}
myType& operator()(size_t x, size_t y)
{
return objData[y * xSize + x];
}
myType const& operator()(size_t x, size_t y) const
{
return objData[y * xSize + x];
}
int getSize()
{
return objData.size();
}
void inputEntireVector(vector<myType> inputVector)
{
objData.swap(inputVector);
}
void printContainer(ostream& stream)
{
copy(objData.begin(), objData.end(),
ostream_iterator<myType>(stream, ""/*No Space*/));
}
private:
vector<myType> objData;
size_t xSize;
size_t ySize;
};
template<class myType>
inline ostream& operator<<(ostream& stream, Container<myType>& object)
{
object.printContainer(stream);
return stream;
}
void ContainerInterfacing();
int main()
{
ContainerInterfacing();
return 0;
}
void ContainerInterfacing()
{
static int const ConsoleWidth = 95;
static int const ConsoleHeight = 30;
size_t width = ConsoleWidth;
size_t height = ConsoleHeight;
Container<int> mySimpleContainer(width, height, 0);
cout << mySimpleContainer.getSize() << endl;
mySimpleContainer(0, 0) = 4;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
In this program, We formed a simple container and got the size of it and printing it.
- Which container provides random access iterators?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
both deque & vector
- What is the output of this program?
#include <iostream>
#include <vector>
using namespace std;
class ComponentClass
{
public:
virtual void traverse() = 0;
};
class LeafClass: public ComponentClass
{
int number;
public:
LeafClass(int num)
{
number = num;
}
void traverse()
{
cout << number << ' ';
}
};
class CompositeClass: public ComponentClass
{
vector < ComponentClass * > children;
public:
void add(ComponentClass *ptr)
{
children.push_back(ptr);
}
void traverse()
{
for (int k = 0; k < children.size(); k++)
children[k]->traverse();
}
};
int main()
{
CompositeClass containers[6];
for (int k = 0; k < 5; k++)
for (int L = 0; L < 4; L++)
containers[k].add(new LeafClass(k *4+L));
for (int m = 1; m < 5; m++)
containers[0].add(&(containers[m]));
for (int n = 0; n < 5; n++)
{
containers[n].traverse();
cout << endl;
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
In this program, We are choosing and printing the numbers based on the certain limit and this is a composite design pattern.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
4 5 6 7
8 9 10 11
12 13 14 15
16 17 18 19
- What does a default header file contain?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
In the header file, we define something that to be manipulated in the program.
- Identify the incorrect statement.
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
The iostream.h is used in the older versions of c++ and iostream is evolved from it in the std namespace.