C++ Institute CPA-21-02 Exam - Topic 8 Question 29 Discussion
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
B) It prints: 1
A) It prints: 0
C) It prints: -1
D) Compilation error
C++ Institute CPA-21-02 Exam - Topic 8 Question 29 Discussion
Actual exam question for
C++ Institute's
CPA-21-02 exam
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.
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.
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.
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.
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?
Miles
10 months agoKimi
10 months agoAlida
11 months agoTijuana
11 months agoTyra
11 months agoParis
11 months agoSheron
12 months agoJovita
12 months agoTyisha
12 months agoDyan
12 months agoLenna
12 months agoMaxima
12 months agoRaul
1 year agoVilma
1 year agoLeontine
2 years agoNoe
2 years agoRodrigo
2 years agoCarmen
2 years agoMiesha
2 years agoHelaine
2 years agoLennie
2 years agoYvonne
2 years agoCarylon
2 years agoMariann
2 years agoCarisa
2 years agoAron
2 years agoCarisa
2 years agoCarisa
2 years agoErick
2 years agoBeckie
2 years agoViola
2 years ago