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 13 Discussion

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

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

#include

using namespace std;

class complex{

double re;

double im;

public:

complex() : re(0),im(0) {}

complex(double x) { re=x,im=x;};

complex(double x,double y) { re=x,im=y;}

void print() { cout << re << " " << im;}

};

int main(){

complex c1(1,2);

c1.print();

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

Cornell
8 days ago
Wait, so the answer is not 42? Darn, I thought C++ was all about the answer to the ultimate question of life, the universe, and everything.
upvoted 0 times
...
Whitney
9 days ago
Hmm, this code looks like it was written by a complex person. Get it? Complex? Ah, never mind.
upvoted 0 times
...
Jeannetta
10 days ago
I'm surprised that the compiler doesn't catch the issue with the constructors. Maybe it's a feature, not a bug!
upvoted 0 times
...
Nu
12 days ago
The output should be 1 2, as the `complex` object `c1` is initialized with 1 and 2 in the `main()` function.
upvoted 0 times
...
Francoise
13 days ago
The correct answer is D. Compilation error. The class `complex` has two private member variables `re` and `im`, and the constructors are not initializing them properly.
upvoted 0 times
...
Hyman
2 months ago
But the constructor for c1 is (1,2), so it should print 1 2, right?
upvoted 0 times
...
Juliana
2 months ago
I disagree, I believe the answer is A) It prints: 0 0
upvoted 0 times
...
Hyman
2 months ago
I think the answer is B) It prints: 1 1
upvoted 0 times
...

Save Cancel