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:

Ashley
8 days 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
...
Jerry
9 days 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
13 days 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
14 days ago
So the correct answer is C) It prints: 23. Got it!
upvoted 0 times
...
Adria
17 days 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
18 days ago
I think it prints: 23 because the set function initializes y to 2 and z to 3.
upvoted 0 times
...

Save Cancel
az-700  pass4success  az-104  200-301  200-201  cissp  350-401  350-201  350-501  350-601  350-801  350-901  az-720  az-305  pl-300  

Warning: Cannot modify header information - headers already sent by (output started at /pass.php:70) in /pass.php on line 77