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-21-02 Topic 2 Question 36 Discussion

Actual exam question for C++ Institute's CPA-21-02 exam
Question #: 36
Topic #: 2
[All CPA-21-02 Questions]

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

#include

#include

using namespace std;

class A {

int x;

protected:

int y;

public:

int z;

};

class B : public A {

string name;

public:

void set() {

y = 2;

z = 3;

}

void Print() { cout << y << z; }

};

int main () {

B b;

b.set();

b.Print();

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: D

Contribute your Thoughts:

Leslee
1 months ago
Wait, wait, wait... you guys are telling me this code actually compiles? I thought we were supposed to be looking for bugs and typos! Looks like the compiler is getting smarter than us programmers.
upvoted 0 times
Therese
7 days ago
Yes, the code compiles and runs without any errors.
upvoted 0 times
...
...
Vi
1 months ago
This code is as clear as mud! But I'm going to go with C, because it just feels right, you know? The protected and public variables are the key here.
upvoted 0 times
...
Ashley
2 months ago
Hmm, I'm not sure about this one. Let me think it through... Oh, I got it! The 'y' variable is protected, so the derived class can access it. And the 'z' variable is public, so it can be printed. C is the right answer, I think.
upvoted 0 times
Elvera
2 days ago
Exactly, C is the right answer.
upvoted 0 times
...
Cristina
8 days ago
So, the output should be 23.
upvoted 0 times
...
Haley
9 days ago
Yes, that makes sense. The output will be 23 because 'y' is set to 2 and 'z' is set to 3 in the derived class.
upvoted 0 times
...
Laura
15 days ago
I think you're right, the protected variable 'y' can be accessed by the derived class and the public variable 'z' can be printed. So, the output should be 23.
upvoted 0 times
...
Truman
16 days ago
Yes, that's correct. And the public variable 'z' can be printed as well.
upvoted 0 times
...
Tamra
20 days ago
I think the protected variable 'y' can be accessed by the derived class.
upvoted 0 times
...
...
Jerry
2 months ago
Haha, looks like someone forgot to initialize the variables. I bet the code will just print a bunch of zeros, right? Option B for the win!
upvoted 0 times
...
Stephen
2 months ago
The correct answer is C. The code will print 23 because the 'y' variable is protected and can be accessed by the derived class 'B', and the 'z' variable is public and can be accessed by any class.
upvoted 0 times
...
Bernadine
2 months ago
So the correct answer is C) It prints: 23. Got it!
upvoted 0 times
...
Adria
2 months ago
I agree with Alesia. The Print function will then output the values of y and z, which are 2 and 3 respectively.
upvoted 0 times
...
Alesia
2 months ago
I think it prints: 23 because the set function initializes y to 2 and z to 3.
upvoted 0 times
...

Save Cancel