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

Boxing and unboxing execution in C Sharp (C#) - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to box the integer value into an object.

C Sharp (C#)
int num = 10;
object boxedNum = [1];
Drag options to blanks, or click blank then click option'
Anum.ToString()
B(object)num
CConvert.ToObject(num)
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Using ToString() instead of boxing.
Trying to use Convert.ToObject which does not exist.
2fill in blank
medium

Complete the code to unbox the object back to an integer.

C Sharp (C#)
object boxedNum = 20;
int unboxedNum = [1];
Drag options to blanks, or click blank then click option'
A(int)boxedNum
BConvert.ToInt32(boxedNum)
CboxedNum as int
Dint.Parse(boxedNum.ToString())
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as int' which returns null for value types.
Using Convert.ToInt32 which works but is not unboxing.
3fill in blank
hard

Fix the error in the unboxing code to avoid InvalidCastException.

C Sharp (C#)
object boxedValue = 15;
long unboxedValue = [1];
Drag options to blanks, or click blank then click option'
A(long)(int)boxedValue
B(long)boxedValue
C(int)boxedValue
DConvert.ToInt64(boxedValue)
Attempts:
3 left
💡 Hint
Common Mistakes
Casting directly to long causes InvalidCastException.
Using Convert.ToInt64 does not unbox but converts.
4fill in blank
hard

Fill both blanks to create a dictionary with boxed integers and unbox them safely.

C Sharp (C#)
var dict = new Dictionary<string, object> {
    {"one", [1],
    {"two", [2]
};
Drag options to blanks, or click blank then click option'
A1
B"two"
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using string values instead of integers for values.
Using non-string keys.
5fill in blank
hard

Fill all three blanks to unbox values from the dictionary and sum them.

C Sharp (C#)
var dict = new Dictionary<string, object> {
    {"a", 5},
    {"b", 10}
};
int sum = (int)dict[[1]] + (int)dict[[2]] + [3];
Drag options to blanks, or click blank then click option'
A"a"
B"b"
C0
D15
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer keys instead of string keys.
Adding a wrong number instead of zero.

Practice

(1/5)
1. What happens during boxing in C#?
easy
A. A value type is converted to an object type.
B. An object type is converted to a value type.
C. A reference type is converted to a value type.
D. A value type is converted to a string.

Solution

  1. Step 1: Understand boxing concept

    Boxing means wrapping a value type (like int) inside an object so it can be treated as an object.
  2. Step 2: Identify the conversion direction

    Boxing converts a value type to an object type, not the other way around.
  3. Final Answer:

    A value type is converted to an object type. -> Option A
  4. Quick Check:

    Boxing = value type to object [OK]
Hint: Boxing wraps value types as objects [OK]
Common Mistakes:
  • Confusing boxing with unboxing
  • Thinking boxing converts to string
  • Mixing up value and reference types
2. Which of the following is the correct syntax to unbox an object to an int in C#?
easy
A. int x = obj.ToInt();
B. int x = obj as int;
C. int x = Convert.ToInt32(obj);
D. int x = (int)obj;

Solution

  1. Step 1: Recall unboxing syntax

    Unboxing requires an explicit cast from object to the original value type.
  2. Step 2: Check each option

    int x = (int)obj; uses explicit cast (int)obj which is correct. int x = obj as int; uses 'as' which works only for reference types. int x = Convert.ToInt32(obj); converts but is not unboxing. int x = obj.ToInt(); is invalid syntax.
  3. Final Answer:

    int x = (int)obj; -> Option D
  4. Quick Check:

    Unboxing = explicit cast (int)obj [OK]
Hint: Unboxing needs explicit cast (int)obj [OK]
Common Mistakes:
  • Using 'as' keyword for unboxing
  • Using Convert.ToInt32 instead of cast
  • Calling non-existent methods like ToInt()
3. What is the output of the following C# code?
int a = 10;
object obj = a; // boxing
int b = (int)obj; // unboxing
b += 5;
Console.WriteLine(a);
medium
A. 15
B. 10
C. 5
D. Compilation error

Solution

  1. Step 1: Understand boxing and unboxing here

    Variable 'a' is boxed into 'obj'. Then 'obj' is unboxed into 'b'. 'b' is a separate copy.
  2. Step 2: Check value changes

    Incrementing 'b' by 5 does not affect 'a'. So 'a' remains 10.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    Boxing copies value; original stays 10 [OK]
Hint: Boxing copies value; original unchanged [OK]
Common Mistakes:
  • Assuming 'a' changes when 'b' changes
  • Confusing reference and value types
  • Thinking boxing links variables
4. Identify the error in this C# code snippet:
object obj = 123;
long l = (long)obj;
Console.WriteLine(l);
medium
A. Invalid cast exception at runtime
B. Compilation error due to missing cast
C. No error, prints 123
D. Null reference exception

Solution

  1. Step 1: Analyze boxing and unboxing types

    Boxing stores an int (123) in obj. Unboxing must cast back to int, not long.
  2. Step 2: Understand runtime behavior

    Trying to unbox as long causes InvalidCastException because obj holds int, not long.
  3. Final Answer:

    Invalid cast exception at runtime -> Option A
  4. Quick Check:

    Unboxing type mismatch causes runtime error [OK]
Hint: Unbox only to original value type [OK]
Common Mistakes:
  • Casting to different value type than boxed
  • Expecting compile-time error instead of runtime
  • Ignoring boxing type stored
5. Given a list of integers boxed as objects, how can you sum their values correctly in C#?
List<object> list = new List<object> { 1, 2, 3, 4 };

Which code snippet correctly sums the integers?
hard
A. int sum = 0; foreach (int x in list) sum += x;
B. int sum = 0; foreach (object o in list) sum += (o as int);
C. int sum = 0; foreach (object o in list) sum += (int)o;
D. int sum = list.Sum();

Solution

  1. Step 1: Understand list content and types

    List contains objects boxed from ints. To sum, unbox each object to int.
  2. Step 2: Evaluate each option

    A: int sum = 0; foreach (int x in list) sum += x; compile error - cannot implicitly convert object to int.
    B: int sum = 0; foreach (object o in list) sum += (o as int); compile error - 'as' cannot be used with non-nullable value types.
    C: int sum = list.Sum(); fails because list is List<object>, no Sum() overload.
    D: int sum = 0; foreach (object o in list) sum += (int)o; unboxes correctly with (int)o.
  3. Final Answer:

    int sum = 0; foreach (object o in list) sum += (int)o; -> Option C
  4. Quick Check:

    Unbox each object with (int)o to sum [OK]
Hint: Unbox each object with (int)o before summing [OK]
Common Mistakes:
  • Trying to foreach as int directly
  • Using Sum() on List<object>
  • Using 'as' operator for unboxing
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