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

Performance implications of boxing in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Boxing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of boxing with value types
What is the output of this C# code snippet involving boxing and unboxing?
C Sharp (C#)
int x = 10;
object o = x; // boxing
int y = (int)o; // unboxing
Console.WriteLine(y + 5);
A10
B15
CRuntime error
DCompilation error
Attempts:
2 left
💡 Hint
Remember that boxing copies the value type into an object and unboxing extracts it back.
Predict Output
intermediate
2:00remaining
Effect of boxing on performance counters
What will be printed by this code that counts boxing operations?
C Sharp (C#)
int count = 0;
object Box(int x) { count++; return x; }

int a = 5;
object o1 = Box(a);
object o2 = Box(a + 1);
Console.WriteLine(count);
A2
B1
C0
DCompilation error
Attempts:
2 left
💡 Hint
Each time an int is assigned to object, boxing occurs.
🔧 Debug
advanced
2:00remaining
Identify the performance issue caused by boxing
Which line in this code causes boxing and may degrade performance?
C Sharp (C#)
List<int> numbers = new List<int> {1, 2, 3};
foreach (object obj in numbers) {
    Console.WriteLine(obj);
}
AList<int> numbers = new List<int> {1, 2, 3};
BConsole.WriteLine(obj)
CNo boxing occurs
Dforeach (object obj in numbers)
Attempts:
2 left
💡 Hint
Check the type used in the foreach loop variable.
Predict Output
advanced
2:00remaining
Output when boxing affects method overload resolution
What is the output of this code involving method overloads and boxing?
C Sharp (C#)
void Print(int x) { Console.WriteLine("Int: " + x); }
void Print(object o) { Console.WriteLine("Object: " + o); }

int val = 7;
Print(val);
AObject: 7
BRuntime error
CInt: 7
DCompilation error
Attempts:
2 left
💡 Hint
Method overload resolution prefers exact type matches over boxing.
🧠 Conceptual
expert
3:00remaining
Why does boxing impact performance negatively?
Which of the following best explains why boxing can degrade performance in C#?
ABoxing creates a new object on the heap and copies the value, causing extra memory and CPU overhead.
BBoxing allows direct modification of the original value type, improving speed.
CBoxing converts a value type to a reference type without copying, so it uses less memory.
DBoxing compiles the code to machine language, which slows down execution.
Attempts:
2 left
💡 Hint
Think about what happens when a value type is boxed.