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 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:

0/2000 characters
Miles
3 months ago
Definitely not a compilation error, that's for sure.
upvoted 0 times
...
Kimi
3 months ago
The logical OR operator returns the first true value, so yeah, 1!
upvoted 0 times
...
Alida
3 months ago
Wait, how does it print 1? I'm confused.
upvoted 0 times
...
Tijuana
4 months ago
I thought it would be 0, but I guess I was wrong!
upvoted 0 times
...
Tyra
4 months ago
It prints: 1
upvoted 0 times
...
Paris
4 months ago
I practiced a similar question, and I think the result of `i` will be 1 because of the short-circuit evaluation in the `||` operator.
upvoted 0 times
...
Sheron
4 months ago
If I recall correctly, `fun(1)` returns 2 and `fun(2)` returns 4, so the expression should evaluate to true, which is 1.
upvoted 0 times
...
Jovita
4 months ago
I'm a bit unsure, but I remember something about logical operators. I think `||` evaluates to true if either side is non-zero.
upvoted 0 times
...
Tyisha
5 months ago
I think the `fun` function returns an integer, so there shouldn't be a compilation error.
upvoted 0 times
...
Dyan
5 months ago
Okay, let me think this through. The || operator will return the first non-zero operand, or 0 if both operands are 0. So the key will be understanding the return values of the two function calls.
upvoted 0 times
...
Lenna
5 months ago
Ah, I think I've got this! The || operator will short-circuit, so the first function call that returns a non-zero value will determine the final result. I'll need to consider the return values of the two function calls.
upvoted 0 times
...
Maxima
5 months ago
Hmm, I'm a bit unsure about this one. The || operator can be confusing, and I'm not sure how it interacts with the function calls. I'll need to work through it step-by-step.
upvoted 0 times
...
Raul
5 months ago
This looks like a tricky logical operation with the || operator. I'll need to carefully think through the order of evaluation and the return values of the two function calls.
upvoted 0 times
...
Vilma
5 months ago
This one seems pretty straightforward. I'm pretty confident I can figure out the right CSS code to center the image horizontally.
upvoted 0 times
...
Leontine
1 year 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 year 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
1 year ago
Got it, thanks for the explanation. Looks like I need to review my logical operators.
upvoted 0 times
...
Carmen
1 year 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 year ago
Oh really? I thought it prints: 1 because the logical OR operator returns true if either operand is true.
upvoted 0 times
...
Helaine
1 year 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
1 year 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
1 year 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
1 year 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 year ago
D) Compilation error
upvoted 0 times
...
Carisa
1 year ago
fun(1) returns 2.
upvoted 0 times
...
Aron
1 year ago
C) It prints: -1
upvoted 0 times
...
Carisa
1 year ago
The || operator short-circuits.
upvoted 0 times
...
Carisa
1 year ago
It prints: 1
upvoted 0 times
...
Erick
1 year ago
B) It prints: 1
upvoted 0 times
...
Beckie
1 year ago
A) It prints: 0
upvoted 0 times
...
...
Viola
1 year ago
I think it prints 1 because the logical OR operator returns true if either operand is true.
upvoted 0 times
...

Save Cancel