Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute Exam CPA Topic 6 Question 89 Discussion

Actual exam question for C++ Institute's CPA exam
Question #: 89
Topic #: 6
[All CPA Questions]

What happens when you attempt to compile and run the following code?

#include

using namespace std;

class A {

public:

int x;

A() { x=0;}

};

class B : public A {

public:

B() { x=1;}

};

class C : private B {

public:

C() { x=2;}

};

int main () {

C c1;

cout << c1.x;

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

Eugene
10 months ago
Woah, this question really tests your understanding of object-oriented programming and pointers. I'm going to go with B, because the code is clearly working with B objects, even though it's doing some fancy pointer stuff. Gotta love these tricky certification questions!
upvoted 0 times
Providencia
9 months ago
Yeah, I agree. It's all about working with those B objects.
upvoted 0 times
...
Providencia
9 months ago
I think it prints: B B
upvoted 0 times
...
...
Leah
10 months ago
This is a classic pointer-related question. I'm confident the answer is B. The code is explicitly creating an array of B objects and then calling the print() method on each one, so the output should be 'B B'.
upvoted 0 times
Gerald
9 months ago
Yes, the output should be 'B B' since it's iterating through the array of B objects.
upvoted 0 times
...
Gwenn
9 months ago
I agree, the code is creating an array of B objects and calling the print() method on each one.
upvoted 0 times
...
Horace
10 months ago
That's right, the code is calling the print() method on each B object in the array.
upvoted 0 times
...
Iluminada
10 months ago
I think the answer is B too. It makes sense with the code logic.
upvoted 0 times
...
Elenor
10 months ago
Yeah, it makes sense because it's creating an array of B objects.
upvoted 0 times
...
...
Ilene
10 months ago
Haha, this question is like a magic trick! The answer must be C. The pointer is casting the array of B objects to a pointer to B objects, but the print() method being called is the one from the A class. The output will be a mix of 'A' and 'B'.
upvoted 0 times
...
Vanesa
10 months ago
Hmm, I'm not sure about this one. The code looks a bit tricky, but I'm leaning towards D. The way the pointer is being incremented and cast, it might end up calling the print() method of the A class instead. Let's see what the others think.
upvoted 0 times
...
Francine
10 months ago
I think the answer is B. The code creates an array of B objects, then casts it to a pointer to B objects and calls the print() method on each one. Since the print() method is defined in the B class, it should print 'B B'.
upvoted 0 times
Raina
10 months ago
It prints: B B
upvoted 0 times
...
Earleen
10 months ago
I think the answer is B.
upvoted 0 times
...
...
Francine
10 months ago
I believe it prints: B B because the pointer is typecasted to B* and then print function is called for each object in the array
upvoted 0 times
...
Serina
11 months ago
I think it prints: B B
upvoted 0 times
...

Save Cancel