Deal of The Day! 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

Posted by Avery on Sep 17, 2024 at 06:35 PM

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;

}

Suggested Answer: B

Get Premium CPA-21-02 Questions as Interactive Web-Based Practice Test or PDF Real Exam Format Detailed Explanations Instant Access Get Your Practice Test
31 comments · newest first
MI
Miles 11 months ago
Definitely not a compilation error, that's for sure.
KI
Kimi 11 months ago
The logical OR operator returns the first true value, so yeah, 1!
AL
Alida 11 months ago
Wait, how does it print 1? I'm confused.
TI
Tijuana 11 months ago
I thought it would be 0, but I guess I was wrong!
TY
Tyra 11 months ago
It prints: 1
PA
Paris 12 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.
SH
Sheron 12 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.
JO
Jovita 12 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.
TY
Tyisha 1 year ago
I think the `fun` function returns an integer, so there shouldn't be a compilation error.
DY
Dyan 1 year 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.
LE
Lenna 1 year 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.
MA
Maxima 1 year 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.
RA
Raul 1 year 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.
VI
Vilma 1 year ago
This one seems pretty straightforward. I'm pretty confident I can figure out the right CSS code to center the image horizontally.
LE
Leontine 2 years ago
I'm not sure, but I think it might be a compilation error because you can't use logical OR with integers.
NO
Noe 2 years 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?
RO
Rodrigo 2 years ago
↳ replying to Noe
Got it, thanks for the explanation. Looks like I need to review my logical operators.
CA
Carmen 2 years ago
↳ replying to Noe
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.
MI
Miesha 2 years ago
↳ replying to Noe
Oh really? I thought it prints: 1 because the logical OR operator returns true if either operand is true.
HE
Helaine 2 years ago
↳ replying to Noe
No, the correct answer is actually D. There will be a compilation error because you can't use the logical OR operator with integers.
LE
Lennie 2 years ago
I disagree, I believe it prints 1 because the logical OR operator only evaluates the second operand if the first one is false.
YV
Yvonne 2 years 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.
CA
Carylon 2 years 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.
MA
Mariann 2 years ago
↳ replying to Carylon
D) Compilation error
CA
Carisa 2 years ago
↳ replying to Carylon
fun(1) returns 2.
AR
Aron 2 years ago
↳ replying to Carylon
C) It prints: -1
CA
Carisa 2 years ago
↳ replying to Carylon
The || operator short-circuits.
CA
Carisa 2 years ago
↳ replying to Carylon
It prints: 1
ER
Erick 2 years ago
↳ replying to Carylon
B) It prints: 1
BE
Beckie 2 years ago
↳ replying to Carylon
A) It prints: 0
VI
Viola 2 years ago
I think it prints 1 because the logical OR operator returns true if either operand is true.
0/2000 characters

Save Cancel