Practice mode C#

What does Console.WriteLine(7 / 2); print in C#?

Correct answer B. 3

Explanation

When both operands are integers, the / operator performs integer division and discards the remainder, truncating towards zero. So 7 / 2 is 3. To get 3.5, at least one operand must be a floating-point or decimal value, for example 7 / 2.0 or (double)7 / 2. This is a common source of bugs when computing averages.

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.