MCSD Programming in C# 2 — Questions and Answers
Question 1: In C#, what is boxing?
- Wrapping a class in an interface
- Converting a value type to a reference type (object) (Correct answer)
- Casting a reference type to a derived type
- Allocating memory on the stack
Correct answer: Converting a value type to a reference type (object)
Boxing converts a value type to the `object` reference type, storing it on the heap.
Question 2: Which C# feature allows a method to accept a variable number of parameters?
- ref
- out
- params (Correct answer)
- optional
Correct answer: params
The `params` keyword allows a method to accept a variable number of arguments as an array.
Question 3: What is the default value of a bool field in a C# class?
- true
- null
- false (Correct answer)
- 0
Correct answer: false
Uninitialized bool fields in C# default to `false`.
Question 4: Which interface must a class implement to use it in a `foreach` loop in C#?
- IComparable
- IEnumerable (Correct answer)
- ICollection
- IList
Correct answer: IEnumerable
A class must implement `IEnumerable` (or `IEnumerable<T>`) to support `foreach` iteration.
Question 5: What does the `virtual` keyword allow in C#?
- Prevents method overriding
- Allows a method to be overridden in derived classes (Correct answer)
- Creates an abstract method
- Makes a method thread-safe
Correct answer: Allows a method to be overridden in derived classes
The `virtual` keyword marks a method so that derived classes can override it using the `override` keyword.
Question 6: Which statement correctly describes a C# delegate?
- A class that wraps an interface
- A type-safe function pointer (Correct answer)
- A static method that returns void
- A struct that holds event data
Correct answer: A type-safe function pointer
A delegate is a type-safe reference to a method, functioning as a first-class function pointer in C#.
In C#, what is boxing?