Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Integer types and their ranges in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Integer Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of integer overflow in C#
What is the output of the following C# code snippet?
C Sharp (C#)
checked
{
    byte b = 255;
    b = (byte)(b + 1);
    Console.WriteLine(b);
}
AOverflowException
B255
C0
D256
Attempts:
2 left
💡 Hint
The checked block throws an exception on overflow.
❓ Predict Output
intermediate
2:00remaining
Range of Int16 type
What is the output of this C# code?
C Sharp (C#)
short min = short.MinValue;
short max = short.MaxValue;
Console.WriteLine($"{min} {max}");
A-65536 65535
B-32767 32768
C-32768 32767
D-128 127
Attempts:
2 left
💡 Hint
Short is a 16-bit signed integer.
❓ Predict Output
advanced
2:00remaining
Unsigned integer max value output
What does this C# code print?
C Sharp (C#)
uint max = uint.MaxValue;
Console.WriteLine(max);
A4294967295
B-1
C2147483647
D0
Attempts:
2 left
💡 Hint
uint is a 32-bit unsigned integer.
❓ Predict Output
advanced
2:00remaining
Behavior of integer underflow in unchecked context
What is the output of this C# code?
C Sharp (C#)
unchecked
{
    int min = int.MinValue;
    int result = min - 1;
    Console.WriteLine(result);
}
AOverflowException
B-2147483649
C-2147483648
D2147483647
Attempts:
2 left
💡 Hint
Unchecked context allows silent overflow/underflow.
🧠 Conceptual
expert
2:00remaining
Size and range of long type in C#
Which of the following correctly describes the size and range of the C# long type?
A32-bit signed integer ranging from -2,147,483,648 to 2,147,483,647
B64-bit signed integer ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
C64-bit unsigned integer ranging from 0 to 18,446,744,073,709,551,615
D16-bit signed integer ranging from -32,768 to 32,767
Attempts:
2 left
💡 Hint
long is a 64-bit signed integer in C#.

Practice

(1/5)
1. Which C# integer type can store values from -128 to 127?
easy
A. int
B. byte
C. short
D. sbyte

Solution

  1. Step 1: Recall the range of sbyte

    The sbyte type stores signed 8-bit integers from -128 to 127.
  2. Step 2: Compare with other types

    byte stores 0 to 255 (unsigned), short stores -32,768 to 32,767, and int stores much larger values.
  3. Final Answer:

    sbyte -> Option D
  4. Quick Check:

    Range -128 to 127 = sbyte [OK]
Hint: Remember sbyte is signed 8-bit: -128 to 127 [OK]
Common Mistakes:
  • Confusing byte (unsigned) with sbyte
  • Choosing short which has a larger range
  • Assuming int is smallest
2. Which of the following is the correct way to declare a variable of type uint in C#?
easy
A. uint number = -1;
B. uint number = 4294967295;
C. uint number = 4294967296;
D. uint number = 3.14;

Solution

  1. Step 1: Understand uint range

    uint is unsigned 32-bit integer with range 0 to 4,294,967,295.
  2. Step 2: Check each assignment

    uint number = -1; assigns -1 (negative) which is invalid. uint number = 4294967295; assigns max valid value 4,294,967,295. uint number = 4294967296; assigns 4,294,967,296 which is too large. uint number = 3.14; assigns a decimal which is invalid for integer type.
  3. Final Answer:

    uint number = 4294967295; -> Option B
  4. Quick Check:

    uint max = 4294967295 [OK]
Hint: uint stores 0 to 4,294,967,295 only [OK]
Common Mistakes:
  • Assigning negative values to unsigned types
  • Using values larger than max range
  • Assigning decimal numbers to integer types
3. What will be the output of this C# code?
short a = 30000;
short b = 10000;
int c = a + b;
Console.WriteLine(c);
medium
A. 40000
B. -25536
C. OverflowException
D. Compile-time error

Solution

  1. Step 1: Understand variable types and addition

    Variables a and b are short (16-bit signed), but addition promotes them to int automatically.
  2. Step 2: Calculate the sum and output

    30000 + 10000 = 40000 fits in int range, so c stores 40000 and prints it.
  3. Final Answer:

    40000 -> Option A
  4. Quick Check:

    30000 + 10000 = 40000 [OK]
Hint: short addition promotes to int, no overflow here [OK]
Common Mistakes:
  • Expecting overflow because sum exceeds short max
  • Thinking addition stays in short type
  • Confusing runtime overflow with compile error
4. Identify the error in this C# code snippet:
byte x = 255;
byte y = 1;
byte z = (byte)(x + y);
Console.WriteLine(z);
medium
A. Output is 0 due to overflow wrap-around
B. Compile-time error due to overflow
C. No error, output is 256
D. Runtime exception thrown

Solution

  1. Step 1: Analyze addition and casting

    Addition of x + y is done as int (255 + 1 = 256). Casting to byte truncates to 8 bits.
  2. Step 2: Understand overflow wrap-around

    Since byte max is 255, 256 wraps around to 0 due to overflow without exception.
  3. Final Answer:

    Output is 0 due to overflow wrap-around -> Option A
  4. Quick Check:

    256 mod 256 = 0 [OK]
Hint: Casting int > 255 to byte wraps around [OK]
Common Mistakes:
  • Expecting output 256 which is invalid for byte
  • Thinking compile or runtime error occurs
  • Ignoring overflow wrap-around behavior
5. You need to store the population of a city which can be up to 5 billion. Which C# integer type should you use to avoid overflow?
hard
A. int
B. uint
C. long
D. short

Solution

  1. Step 1: Check the required range

    5 billion is 5,000,000,000 which exceeds int max (2,147,483,647) and uint max (4,294,967,295).
  2. Step 2: Choose type with sufficient range

    long is a signed 64-bit integer with range ±9 quintillion, enough to store 5 billion safely.
  3. Final Answer:

    long -> Option C
  4. Quick Check:

    5 billion < long max [OK]
Hint: Use long for numbers bigger than int or uint max [OK]
Common Mistakes:
  • Choosing int or uint which are too small
  • Using short which is very small
  • Not considering signed vs unsigned range
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard