Challenge - 5 Problems
Checked Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of checked arithmetic overflow
What is the output of this C# code snippet?
C Sharp (C#)
using System; class Program { static void Main() { try { int max = int.MaxValue; int result = checked(max + 1); Console.WriteLine(result); } catch (OverflowException) { Console.WriteLine("Overflow caught"); } } }
Attempts:
2 left
💡 Hint
Think about what happens when you add 1 to the maximum int value inside a checked block.
✗ Incorrect
The checked keyword causes the runtime to throw an OverflowException when the arithmetic operation exceeds the range of the int type. Adding 1 to int.MaxValue causes overflow, so the exception is caught and "Overflow caught" is printed.
❓ Predict Output
intermediate2:00remaining
Unchecked arithmetic overflow result
What is the output of this C# code snippet?
C Sharp (C#)
using System; class Program { static void Main() { int max = int.MaxValue; int result = unchecked(max + 1); Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
Consider what unchecked does when an integer overflows.
✗ Incorrect
The unchecked keyword disables overflow checking, so the addition wraps around. Adding 1 to int.MaxValue results in int.MinValue due to overflow wrap-around.
🧠 Conceptual
advanced2:00remaining
Behavior difference between checked and unchecked
Which statement best describes the difference between checked and unchecked arithmetic in C#?
Attempts:
2 left
💡 Hint
Think about what happens when an integer operation exceeds its limits in each mode.
✗ Incorrect
The checked keyword enables overflow checking and throws exceptions on overflow. The unchecked keyword disables overflow checking and allows wrap-around without exceptions.
❓ Predict Output
advanced2:00remaining
Output of mixed checked and unchecked blocks
What is the output of this C# program?
C Sharp (C#)
using System; class Program { static void Main() { int x = int.MaxValue; try { unchecked { x = x + 1; } checked { x = x + 1; } Console.WriteLine(x); } catch (OverflowException) { Console.WriteLine("Overflow caught"); } } }
Attempts:
2 left
💡 Hint
Consider the effect of unchecked and checked blocks on the variable x.
✗ Incorrect
The first addition in unchecked wraps x from int.MaxValue to int.MinValue (-2147483648). The second addition in checked tries to add 1 to int.MinValue, which is valid and results in -2147483647. However, since the first addition was unchecked, no exception occurs there. The second addition is within checked and does not overflow, so no exception is thrown. The program prints -2147483647.
❓ Predict Output
expert2:00remaining
Output of checked arithmetic in a method with unchecked context
What is the output of this C# program?
C Sharp (C#)
using System; class Program { static int AddOneChecked(int value) { checked { return value + 1; } } static void Main() { unchecked { int max = int.MaxValue; try { int result = AddOneChecked(max); Console.WriteLine(result); } catch (OverflowException) { Console.WriteLine("Overflow caught"); } } } }
Attempts:
2 left
💡 Hint
Remember that checked and unchecked contexts are scoped and can be nested.
✗ Incorrect
Even though Main is in an unchecked context, the AddOneChecked method uses a checked block. The addition of 1 to int.MaxValue inside checked triggers an OverflowException, which is caught and prints "Overflow caught".