CPA-21-02 practice question 12 of 35

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(0); cout

Full set: 257 questions
Answer options
A: It prints: 0 Correct answer
B: It prints: 1
C: It prints: -1
D: Compilation error
Correct Answer:
A. It prints: 0
Explanation

The program defines a function fun(int x) that returns 2 x. In main(), the expression fun(1) & fun(0) is evaluated. fun(1) returns 2 (which is 10 in binary). fun(0) returns 0 (which is 00 in binary). The & operator performs a bitwise AND operation on these two integer results. Comparing the binary representations (10 & 00), no corresponding bits are 1 in both operands, so the result evaluates to 0. This value is assigned to i and printed.

Why Incorrect

Option B is incorrect because the bitwise AND of 2 and 0 evaluates to 0, not 1.
Option C is incorrect because there are no negative numbers or bitwise NOT operations involved to produce a -1.
Option D is incorrect because the code is syntactically valid and will compile successfully without errors.

References

ISO/IEC 14882:2017 (C++17 Standard), Section 7.6.10 [expr.bit.and] (Bitwise AND operator).

Stanford University CS106L Standard C++ Programming, Course Reader, Chapter on "Operators and Expressions".

Scroll to Top