Sail E0 Webinar
Question


What is the output of this program?


1.
#include
2.
#include
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 traverse();
34.
}
35.
};
36.
int main()
37.
{
38.
Composite containers[4];
39.
for (int i = 0; i 40.
for (int j = 0; j 41.
containers[i].add(new Leaf(i *3+j));
42.
for (int k = 1; k 43.
containers[0].add(&(containers[k]));
44.
for (int p = 0; p 45.
{
46.
containers[p].traverse();
47.
}
48.
}
Options:
A .  345
B .  678
C .  901
D .  None of the mentioned
Answer: Option D

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



Was this answer helpful ?
Next Question

Submit Solution

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

Latest Videos

Latest Test Papers