Practice mode C++

Assuming standard headers and using namespace std, what does this C++ code print? int a = 7, b = 2; cout << a / b << ” ” << a % b << ” ” << 7.0 / b;

Correct answer C. 3 1 3.5

Explanation

When both operands of / are int, C++ performs integer division and discards the fractional part, so 7 / 2 is 3, and 7 % 2 gives the remainder 1. In 7.0 / b the left operand is a double, so b is converted to double and the result is 3.5. Mixing one floating-point operand into an arithmetic expression is enough to switch it to floating-point arithmetic.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.