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 8 Question 29 Discussion

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

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

#include

using namespace std;

int fun(int x) {

return 2*x;

}

int main(){

int i;

i = fun(1) || fun(2);

cout << i;

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

Leontine
2 months ago
I'm not sure, but I think it might be a compilation error because you can't use logical OR with integers.
upvoted 0 times
...
Noe
2 months ago
Haha, the correct answer is obviously C. Because, you know, -1 is just a classic programmer's choice for a return value. Who needs 0 or 1 when you have -1, am I right?
upvoted 0 times
Rodrigo
25 days ago
Got it, thanks for the explanation. Looks like I need to review my logical operators.
upvoted 0 times
...
Carmen
1 months ago
I see, but in this case, the return value of fun(1) is 2 and the return value of fun(2) is also 2. So, the logical OR operator won't work as expected.
upvoted 0 times
...
Miesha
1 months ago
Oh really? I thought it prints: 1 because the logical OR operator returns true if either operand is true.
upvoted 0 times
...
Helaine
2 months ago
No, the correct answer is actually D. There will be a compilation error because you can't use the logical OR operator with integers.
upvoted 0 times
...
...
Lennie
3 months ago
I disagree, I believe it prints 1 because the logical OR operator only evaluates the second operand if the first one is false.
upvoted 0 times
...
Yvonne
3 months ago
I think the answer is D. There's a compilation error because the return type of fun(x) is int, but the || operator expects a boolean expression.
upvoted 0 times
...
Carylon
3 months ago
The answer is B. The || operator short-circuits, so it will return the first non-zero value it encounters, which is fun(1) = 2.
upvoted 0 times
Mariann
1 months ago
D) Compilation error
upvoted 0 times
...
Carisa
1 months ago
fun(1) returns 2.
upvoted 0 times
...
Aron
2 months ago
C) It prints: -1
upvoted 0 times
...
Carisa
2 months ago
The || operator short-circuits.
upvoted 0 times
...
Carisa
2 months ago
It prints: 1
upvoted 0 times
...
Erick
2 months ago
B) It prints: 1
upvoted 0 times
...
Beckie
2 months ago
A) It prints: 0
upvoted 0 times
...
...
Viola
3 months ago
I think it prints 1 because the logical OR operator returns true if either operand is true.
upvoted 0 times
...

Save Cancel