Cyber Monday 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
2 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
1 months ago
Definitely a compilation error. The code needs to be fixed before it can run properly.
upvoted 0 times
...
Glendora
1 months ago
Yeah, I agree. The code is trying to compare complex numbers without the proper operator overloading.
upvoted 0 times
...
Adolph
2 months ago
I think it will print ERROR because the == operator is not properly overloaded for complex numbers.
upvoted 0 times
...
...
Mitsue
2 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
3 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
3 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
2 months ago
Looks like we all think it's a compilation error. Let's see if that's the case.
upvoted 0 times
...
Wilford
2 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
2 months ago
User1, you're correct. The code will print 'ERROR' because the overloaded == operator is missing a return type.
upvoted 0 times
...
Bobbye
3 months ago
It prints: ERROR
upvoted 0 times
...
...
Stevie
3 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
3 months ago
B) It prints: ERROR
upvoted 0 times
...
Luann
3 months ago
A) It prints: OK
upvoted 0 times
...
...
Nu
3 months ago
I agree with Pura, because the code compares the values of c1 and c2.
upvoted 0 times
...
Pura
3 months ago
I think it will print OK.
upvoted 0 times
...

Save Cancel
az-700  pass4success  az-104  200-301  200-201  cissp  350-401  350-201  350-501  350-601  350-801  350-901  az-720  az-305  pl-300  

Warning: Cannot modify header information - headers already sent by (output started at /pass.php:70) in /pass.php on line 77