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 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
1 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
1 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
Carmen
2 days 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
13 days ago
Oh really? I thought it prints: 1 because the logical OR operator returns true if either operand is true.
upvoted 0 times
...
Helaine
23 days 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
2 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
2 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
2 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
12 days ago
D) Compilation error
upvoted 0 times
...
Carisa
13 days ago
fun(1) returns 2.
upvoted 0 times
...
Aron
16 days ago
C) It prints: -1
upvoted 0 times
...
Carisa
19 days ago
The || operator short-circuits.
upvoted 0 times
...
Carisa
21 days ago
It prints: 1
upvoted 0 times
...
Erick
28 days ago
B) It prints: 1
upvoted 0 times
...
Beckie
1 months ago
A) It prints: 0
upvoted 0 times
...
...
Viola
2 months ago
I think it prints 1 because the logical OR operator returns true if either operand is true.
upvoted 0 times
...

Save Cancel