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 questionsThe 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.
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.
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".
Keep practicing
Take the free CPA-21-02 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other CPA-21-02 practice questions
- Question 1What happens when you attempt to compile and run the following code? #include using names…
- Question 2What happens when you attempt to compile and run the following code? #include #include us…
- Question 3Which of the following statements are correct about an array? int tab[10];
- Question 4What happens when you attempt to compile and run the following code? #include #include us…
- Question 5What is the output of the program if characters 'h', 'e', 'l', 'l' , 'o' and enter are su…
- Question 6What happens when you attempt to compile and run the following code? #include using names…
