Practice mode C#

What is the output of: int m = int.MaxValue; m++; Console.WriteLine(m); (default project settings)?

Correct answer B. -2147483648

Explanation

By default, non-constant integer arithmetic in C# runs in an unchecked context, so overflow silently wraps around using two's complement. Adding 1 to int.MaxValue (2,147,483,647) gives int.MinValue, -2,147,483,648. To make overflow throw OverflowException, wrap the expression in checked or enable the CheckForOverflowUnderflow compiler option. Constant expressions that overflow are compile-time errors instead.

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.