New Year Sale 2026! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CPA-21-02 Exam - 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:

0/2000 characters
Roosevelt
3 months ago
Yeah, it won't compile because of that typo.
upvoted 0 times
...
Marquetta
3 months ago
Definitely prints ERROR if it compiles, but it won't!
upvoted 0 times
...
Earleen
3 months ago
Wait, is that really a compilation error? I thought it might run.
upvoted 0 times
...
Hassie
4 months ago
Totally agree, that "this?>" is a syntax mistake!
upvoted 0 times
...
Leonida
4 months ago
Looks like a compilation error due to the operator overload.
upvoted 0 times
...
Francesco
4 months ago
I'm leaning towards option C, but I can't recall if the constructor initializes the values properly.
upvoted 0 times
...
Viki
4 months ago
I practiced a similar question where the operator was overloaded correctly, and it printed "OK". But this one seems different.
upvoted 0 times
...
Refugia
4 months ago
I think there might be a compilation error because of the "this?>" part. It doesn't look right to me.
upvoted 0 times
...
Nieves
5 months ago
I remember something about operator overloading, but I'm not sure if the syntax is correct here.
upvoted 0 times
...
Sherita
5 months ago
This seems straightforward. The `operator==` function checks if the `re` and `im` members of the two `complex` objects are equal. Since `c1` and `c2` are default-constructed, their `re` and `im` values will be different, so the comparison will fail, and "ERROR" will be printed.
upvoted 0 times
...
Lucy
5 months ago
Okay, I think I've got this. The `complex` class has a default constructor that initializes `re` to 1 and `im` to 0.4. In the `main` function, we create two `complex` objects `c1` and `c2`, and then compare them using the `==` operator. Based on the implementation of `operator==`, I believe the correct answer is B - it will print "ERROR".
upvoted 0 times
...
Avery
5 months ago
I'm a bit confused by the `this->` syntax in the `operator==` function. I'll need to double-check the proper way to access class member variables.
upvoted 0 times
...
Alline
5 months ago
Hmm, this looks like it's testing our understanding of operator overloading and class member access. I'll need to carefully read through the code and think about how the comparison operator is being used.
upvoted 0 times
...
Gerald
5 months ago
This one seems pretty straightforward. I think the best approach is to focus on the key phrase "Don't Have a Complicated Signup" and choose the option that aligns with that.
upvoted 0 times
...
Layla
1 year 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
1 year ago
Definitely a compilation error. The code needs to be fixed before it can run properly.
upvoted 0 times
...
Glendora
1 year ago
Yeah, I agree. The code is trying to compare complex numbers without the proper operator overloading.
upvoted 0 times
...
Adolph
1 year ago
I think it will print ERROR because the == operator is not properly overloaded for complex numbers.
upvoted 0 times
...
...
Mitsue
1 year ago
I'm not sure, but I think it will print ERROR because the operator== function is not correctly implemented.
upvoted 0 times
...
Joni
1 year 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
1 year 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
1 year ago
Looks like we all think it's a compilation error. Let's see if that's the case.
upvoted 0 times
...
Wilford
1 year 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 year ago
User1, you're correct. The code will print 'ERROR' because the overloaded == operator is missing a return type.
upvoted 0 times
...
Bobbye
1 year ago
It prints: ERROR
upvoted 0 times
...
...
Stevie
1 year 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
1 year ago
B) It prints: ERROR
upvoted 0 times
...
Luann
1 year ago
A) It prints: OK
upvoted 0 times
...
...
Nu
1 year ago
I agree with Pura, because the code compares the values of c1 and c2.
upvoted 0 times
...
Pura
1 year ago
I think it will print OK.
upvoted 0 times
...

Save Cancel