The object type is the universal base type in C#. Every type, whether value or reference, directly or indirectly inherits from object.
Click to reveal answer
beginner
Can a variable of type object hold any data type in C#?
Yes, a variable of type object can hold any data type because all types derive from object. This allows storing any value or reference type in an object variable.
Click to reveal answer
intermediate
What happens when you assign a value type to an object variable?
The value type is 'boxed' — it is wrapped inside an object on the heap so it can be treated as a reference type.
Click to reveal answer
intermediate
How do you get the original value back from an object variable holding a value type?
You perform 'unboxing' by casting the object back to the original value type.
Click to reveal answer
beginner
Why is object useful as a universal base type?
Because it allows writing flexible code that can work with any data type, enabling polymorphism and generic programming.
Click to reveal answer
Which type is the universal base type for all types in C#?
Adynamic
Bint
Cstring
Dobject
✗ Incorrect
In C#, object is the universal base type from which all types derive.
What is boxing in C#?
AConverting a reference type to a value type
BWrapping a value type inside an object
CCasting an object to a string
DConverting a string to an int
✗ Incorrect
Boxing is wrapping a value type inside an object so it can be treated as a reference type.
How do you retrieve the original value type from an object variable?
ABy calling ToString()
BBy boxing
CBy unboxing (casting)
DBy using var keyword
✗ Incorrect
Unboxing is casting the object back to the original value type.
Can an object variable hold a string value?
AYes, because string is a reference type derived from object
BOnly if explicitly cast
CNo, only value types
DNo, string is a separate type
✗ Incorrect
Strings are reference types and derive from object, so they can be stored in an object variable.
Why might you use object as a variable type?
ATo allow storing any type of data
BTo restrict the variable to integers only
CTo improve performance
DTo avoid casting
✗ Incorrect
Using object allows storing any type of data because all types derive from it.
Explain how the object type acts as a universal base type in C# and what happens when you assign a value type to an object variable.
Think about how value types are wrapped when assigned to object.
You got /3 concepts.
Describe the difference between boxing and unboxing in C# with examples.
Boxing and unboxing are about converting between value types and object.
You got /3 concepts.
Practice
(1/5)
1. What is the role of the object type in C#?
easy
A. object is used only for reference types, not value types.
B. object is used only for strings.
C. object is a special type for numeric calculations.
D. object is the universal base type for all data types.
Solution
Step 1: Understand the base type concept
In C#, object is the root type from which all other types derive, including value and reference types.
Step 2: Analyze the options
Only object is the universal base type for all data types. correctly states that object is the universal base type for all data types.
Final Answer:
object is the universal base type for all data types. -> Option D
Quick Check:
object = universal base type [OK]
Hint: Remember: all types inherit from object in C# [OK]
Common Mistakes:
Thinking object is only for reference types
Confusing object with string or numeric types
Assuming object is a special numeric type
2. Which of the following is the correct way to store an integer value in an object variable?
easy
A. int obj = new object(42);
B. object obj = 42;
C. object obj = new int(42);
D. object obj = "42";
Solution
Step 1: Understand boxing of value types
In C#, value types like int can be stored in an object variable by implicit boxing, e.g., object obj = 42;.
Step 2: Check each option
object obj = 42; correctly assigns an integer to an object variable. Options B and C use invalid syntax. object obj = "42"; stores a string, not an integer.
Final Answer:
object obj = 42; -> Option B
Quick Check:
Boxing int to object = object obj = 42; [OK]
Hint: Assign value directly to object for boxing [OK]
Common Mistakes:
Trying to use new with int type
Confusing string and int assignments
Using invalid constructors for value types
3. What will be the output of this code?
object obj = 10;
int x = (int)obj + 5;
Console.WriteLine(x);
medium
A. Runtime error
B. 10
C. 15
D. Compilation error
Solution
Step 1: Understand boxing and unboxing
The integer 10 is boxed into obj. Casting back to int unboxes it correctly.
Step 2: Calculate the expression
Unboxed value 10 plus 5 equals 15, which is printed.
Final Answer:
15 -> Option C
Quick Check:
Unbox 10 + 5 = 15 [OK]
Hint: Cast object back to int before arithmetic [OK]
Common Mistakes:
Forgetting to cast before adding
Expecting string output
Confusing compile and runtime errors
4. Identify the error in this code snippet:
object obj = "hello";
int num = (int)obj;
Console.WriteLine(num);
medium
A. Invalid cast exception at runtime
B. Compilation error due to type mismatch
C. No error, prints 'hello'
D. Syntax error in variable declaration
Solution
Step 1: Analyze the cast operation
The object contains a string "hello" but is cast to int, which is invalid.
Step 2: Identify the error type
This causes an InvalidCastException at runtime, not a compile-time error.
Final Answer:
Invalid cast exception at runtime -> Option A
Quick Check:
Wrong cast string to int = runtime error [OK]
Hint: Cast only to the original stored type [OK]
Common Mistakes:
Assuming compile-time error instead of runtime
Casting string to int directly
Ignoring exception possibility
5. Given this code, what is the best way to safely retrieve the integer value stored in obj?
object obj = 25;
// Retrieve integer value safely here
hard
A. int value = obj is int i ? i : 0;
B. int value = obj as int;
C. int value = Convert.ToInt32(obj);
D. int value = (int)obj;
Solution
Step 1: Understand safe casting options
Direct cast (A) can throw exceptions. as operator (B) cannot be used with value types like int.
Step 2: Evaluate options for safety
int value = obj is int i ? i : 0; uses pattern matching to check type before assignment, avoiding exceptions. int value = Convert.ToInt32(obj); converts but may throw if obj is not convertible.
Final Answer:
int value = obj is int i ? i : 0; -> Option A
Quick Check:
Use pattern matching for safe unboxing [OK]
Hint: Use pattern matching to safely cast object [OK]
Common Mistakes:
Using 'as' with value types
Not checking type before casting
Assuming Convert.ToInt32 is always safe
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.