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 7 Question 84 Discussion

Actual exam question for C++ Institute's CPA exam
Question #: 84
Topic #: 7
[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:

Freida
6 months ago
I think the answer is C as well. The increment in the pointer changes the print order.
upvoted 0 times
...
Glennis
6 months ago
I'm not sure, but I think it might be D because of how the pointers are being used.
upvoted 0 times
...
Viva
6 months ago
I agree with Tatum, the code makes use of pointer arithmetic to print A B.
upvoted 0 times
...
Tatum
6 months ago
I think the answer is C because it alternates between A and B.
upvoted 0 times
...
Crista
6 months ago
I think because we are incrementing the pointer to class B and then calling the print function
upvoted 0 times
...
Shenika
6 months ago
But why do you think it prints: B B?
upvoted 0 times
...
Pamela
6 months ago
I disagree, I believe it prints: B B
upvoted 0 times
...
Crista
7 months ago
I feel like it may print: A B too
upvoted 0 times
...
Shenika
7 months ago
I think it prints: A B
upvoted 0 times
...
Luann
8 months ago
You know, I'm starting to wonder if the answer is actually D) B A. Think about it - we're casting the sc array to a B* pointer, but the underlying objects are actually A objects. So when we call print() on the B* pointer, it should invoke the print() function of the A class, right? Hmm, I'm not totally sure, but that's my best guess.
upvoted 0 times
...
Gladys
8 months ago
Haha, I bet the exam writers are just trying to trip us up with this one. They're probably hoping we'll overthink it and second-guess ourselves. I'm just going to go with my gut and say it prints B B. What could be simpler than that?
upvoted 0 times
...
Shawnna
8 months ago
Hold on, I'm not so sure about that. If the objects are actually A objects, shouldn't it be printing A A? Or maybe it's going to print A B since we're accessing the objects through a B* pointer? This is really confusing, I need to think this through more carefully.
upvoted 0 times
...
Katie
8 months ago
I think the key here is the casting of the sc array to a B* pointer. Since we're calling print() on the B* pointer, it's going to invoke the print() function of the B class, even though the underlying objects are actually A objects. So my guess is that it's going to print B B.
upvoted 0 times
...
Vicky
8 months ago
Okay, let's take a closer look at this. We've got two classes, A and B, both with a print() function that prints out their respective letters. Then in the main function, we create an array of B objects and cast it to a B* pointer. Finally, we loop through and call the print() function on each element. Hmm, this is tricky.
upvoted 0 times
Belen
7 months ago
I think it will print 'A B' because we are casting the array of B objects to a B* pointer and calling the print() function on each element.
upvoted 0 times
...
Luther
7 months ago
C) It prints: A B
upvoted 0 times
...
...
Wayne
8 months ago
Whoa, this question looks like a real mindbender! I'm not even sure what's going to happen when I run this code. Is it going to print A A, B B, A B, or B A? My brain is already starting to hurt just thinking about it.
upvoted 0 times
...

Save Cancel