0
0
C Sharp (C#)programming~20 mins

Checked and unchecked arithmetic in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Checked Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
    }
  }
}
A-2147483648
B2147483648
COverflow caught
DCompilation error
Attempts:
2 left
💡 Hint
Think about what happens when you add 1 to the maximum int value inside a checked block.
Predict Output
intermediate
2: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);
  }
}
A-2147483648
B2147483648
COverflow caught
DCompilation error
Attempts:
2 left
💡 Hint
Consider what unchecked does when an integer overflows.
🧠 Conceptual
advanced
2:00remaining
Behavior difference between checked and unchecked
Which statement best describes the difference between checked and unchecked arithmetic in C#?
Achecked disables overflow exceptions; unchecked enables overflow exceptions.
Bchecked causes overflow exceptions; unchecked ignores overflow and wraps around silently.
Cchecked and unchecked both throw exceptions on overflow.
Dchecked and unchecked both ignore overflow and wrap around silently.
Attempts:
2 left
💡 Hint
Think about what happens when an integer operation exceeds its limits in each mode.
Predict Output
advanced
2: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");
    }
  }
}
A2147483648
BOverflow caught
C-2147483648
D-2147483647
Attempts:
2 left
💡 Hint
Consider the effect of unchecked and checked blocks on the variable x.
Predict Output
expert
2: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");
      }
    }
  }
}
AOverflow caught
B-2147483648
CCompilation error
D2147483648
Attempts:
2 left
💡 Hint
Remember that checked and unchecked contexts are scoped and can be nested.