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
2 months 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
Yoko
21 days ago
No way, it prints: 22. Trust me on this one.
upvoted 0 times
...
Tula
29 days ago
I think you're mistaken. It actually prints: 42
upvoted 0 times
...
...
Vincenza
2 months 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
2 months 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
Kina
23 hours ago
Yes, and since z is public, its value can also be changed.
upvoted 0 times
...
Bethanie
5 days ago
I agree, the protected member y can be modified in the derived class.
upvoted 0 times
...
Celestine
5 days ago
I think the output will be 44.
upvoted 0 times
...
...
Avery
2 months ago
I'm not sure, but I think it prints: 44 as well
upvoted 0 times
...
Pearly
2 months 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
2 months ago
I think it prints: 44
upvoted 0 times
...
Shayne
2 months 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
2 months 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
2 months 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
Yvonne
1 months ago
Actually, it prints: 42.
upvoted 0 times
...
Laticia
1 months ago
Actually, it prints: 44.
upvoted 0 times
...
Ricarda
1 months ago
No, I believe it prints: 42.
upvoted 0 times
...
Amber
1 months ago
No, I believe it prints: 42.
upvoted 0 times
...
Willodean
2 months ago
I think the answer is B) It prints: 44.
upvoted 0 times
...
Brynn
2 months ago
I think the answer is B) It prints: 44.
upvoted 0 times
...
...

Save Cancel