CPA-21-02 practice question 26 of 35

What happens when you attempt to compile and run the following code? #include using namespace std; int f(int a, int b); int main() { float b; b = f(20,10); cout

Full set: 257 questions
Answer options
A: It prints: 2 Correct answer
B: It prints: 5
C: It prints: 10
D: It prints: 0
Correct Answer:
A. It prints: 2
Explanation

The function f takes two integer parameters and returns an integer. When f(20, 10) is called, the expression a / b performs integer division (20 / 10), which evaluates to the integer 2. This integer is returned and then implicitly converted to a float (value 2.0f) when assigned to the variable b in main. The std::cout stream formats the float 2.0f and prints it as 2 without trailing decimal zeros by default.

Why Incorrect

B: Integer division of 20 by 10 results in 2, not 5. C: Integer division of 20 by 10 results in 2, not 10. D: Integer division of 20 by 10 results in 2, not 0.

References

- ISO/IEC 14882:2017 (C++17 Standard), Section 8.5.4 [expr.mul], Paragraph 4 (Integer division).

- ISO/IEC 14882:2017 (C++17 Standard), Section 7.3.8 [conv.fpint] (Floating-integral conversions).

- Bjarne Stroustrup, "The C++ Programming Language" (4th Edition), Chapter 10.5 (Implicit Type Conversion).

Scroll to Top