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 1 Question 37 Discussion

Actual exam question for C++ Institute's CPA-21-02 exam
Question #: 37
Topic #: 1
[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;

A() { x=1; y=2; z=3; }

};

class B : public A {

public:

void set() {

y = 4; z = 2;

}

void Print() {

cout << y << z;

}

};

int main () {

B b;

b.set();

b.Print();

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: A

Contribute your Thoughts:

Alease
6 hours ago
Ha! This is a classic trick question. The answer is obviously C) It prints: 22. Why, you ask? Because the protected member y is set to 4, and the public member z is set to 2. So when we print them, we get 42. Easy as pie!
upvoted 0 times
...
Vincenza
3 days ago
I believe it prints: 44 too, because the Print function in class B outputs the values of y and z
upvoted 0 times
...
Claudia
10 days ago
Hmm, this is tricky. I'm going to have to think about it carefully. Wait, I've got it! The protected member y is accessible in the derived class, so we can modify it. And since z is public, we can also change its value. The output should be 44. I'll go with B) It prints: 44.
upvoted 0 times
...
Avery
11 days ago
I'm not sure, but I think it prints: 44 as well
upvoted 0 times
...
Pearly
12 days ago
I agree with Cammy, because the set function in class B changes the value of y to 4 and z to 2
upvoted 0 times
...
Cammy
14 days ago
I think it prints: 44
upvoted 0 times
...
Shayne
15 days ago
Okay, let's think this through. The default constructor of class A initializes x to 1, y to 2, and z to 3. In the set() function, we change y to 4 and z to 2. So the output should be 42. I'll go with B) It prints: 44.
upvoted 0 times
...
Daryl
16 days ago
Ah, a classic inheritance question. The protected member y is accessible in the derived class B, so we can modify it in the set() function. And since z is public, we can also change its value. The output should be 44. This is an easy one!
upvoted 0 times
...
Melina
17 days ago
Hmm, let's see. The code creates an object of class B, which inherits from class A. The set() function sets the value of the protected member y to 4 and the public member z to 2. The Print() function then prints the values of y and z. I think the answer is B) It prints: 44.
upvoted 0 times
Willodean
1 days ago
I think the answer is B) It prints: 44.
upvoted 0 times
...
Brynn
3 days ago
I think the answer is B) It prints: 44.
upvoted 0 times
...
...

Save Cancel