BlackFriday 2024! 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 5 Question 28 Discussion

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

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

#include

#include

using namespace std;

class complex{

double re;

double im;

public:

complex() : re(1),im(0.4) {}

bool operator==(complex &t);

};

bool complex::operator == (complex &t){

if((this?>re == t.re) && (this?>im == t.im))

return true;

else

return false;

}

int main(){

complex c1,c2;

if (c1==c2)

cout << "OK";

else {

cout << "ERROR";

}

}

Show Suggested Answer Hide Answer
Suggested Answer: A

Contribute your Thoughts:

Layla
1 months ago
D, runtime error. I bet the compiler will yell at us for trying to compare complex numbers without overloading the == operator properly. Whoever wrote this code needs to go back to Complex Numbers 101!
upvoted 0 times
Zona
5 days ago
Definitely a compilation error. The code needs to be fixed before it can run properly.
upvoted 0 times
...
Glendora
11 days ago
Yeah, I agree. The code is trying to compare complex numbers without the proper operator overloading.
upvoted 0 times
...
Adolph
22 days ago
I think it will print ERROR because the == operator is not properly overloaded for complex numbers.
upvoted 0 times
...
...
Mitsue
1 months ago
I'm not sure, but I think it will print ERROR because the operator== function is not correctly implemented.
upvoted 0 times
...
Joni
2 months ago
Easy peasy, the answer is A! The code will print 'OK' because the default constructor sets the values for c1 and c2, and the overloaded == operator compares them correctly.
upvoted 0 times
...
Beckie
2 months ago
Hmm, I'm not sure. The code looks a bit complex (no pun intended), but I'll go with C, compilation error. There's something about the overloaded == operator that doesn't look quite right.
upvoted 0 times
Callie
21 days ago
Looks like we all think it's a compilation error. Let's see if that's the case.
upvoted 0 times
...
Wilford
1 months ago
I agree with User1 and User2. The code will not compile due to the missing return type in the overloaded operator.
upvoted 0 times
...
Marylou
1 months ago
User1, you're correct. The code will print 'ERROR' because the overloaded == operator is missing a return type.
upvoted 0 times
...
Bobbye
2 months ago
It prints: ERROR
upvoted 0 times
...
...
Stevie
2 months ago
I think the answer is B, it prints ERROR. The constructor of the complex class initializes the real part to 1 and the imaginary part to 0.4, so c1 and c2 will not be equal.
upvoted 0 times
Derick
2 months ago
B) It prints: ERROR
upvoted 0 times
...
Luann
2 months ago
A) It prints: OK
upvoted 0 times
...
...
Nu
2 months ago
I agree with Pura, because the code compares the values of c1 and c2.
upvoted 0 times
...
Pura
2 months ago
I think it will print OK.
upvoted 0 times
...

Save Cancel