Cyber Monday 2024! 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 :

void print() {

cout << "A ";

}

};

class B {

public :

void print() {

cout << "B ";

}

};

int main() {

B sc[2];

B *bc = (B*)sc;

for (int i=0; i<2;i++)

(bc++)->print();

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

Eugene
5 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
4 months ago
Yeah, I agree. It's all about working with those B objects.
upvoted 0 times
...
Providencia
5 months ago
I think it prints: B B
upvoted 0 times
...
...
Leah
6 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
5 months ago
Yes, the output should be 'B B' since it's iterating through the array of B objects.
upvoted 0 times
...
Gwenn
5 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
5 months ago
That's right, the code is calling the print() method on each B object in the array.
upvoted 0 times
...
Iluminada
5 months ago
I think the answer is B too. It makes sense with the code logic.
upvoted 0 times
...
Elenor
5 months ago
Yeah, it makes sense because it's creating an array of B objects.
upvoted 0 times
...
...
Ilene
6 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
6 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
6 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
5 months ago
It prints: B B
upvoted 0 times
...
Earleen
5 months ago
I think the answer is B.
upvoted 0 times
...
...
Francine
6 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
6 months ago
I think it prints: B B
upvoted 0 times
...

Save Cancel