MCSD Programming in C# 1 — Questions and Answers
Question 1: Which keyword in C# is used to prevent a class from being inherited?
- sealed (Correct answer)
- static
- abstract
- readonly
Correct answer: sealed
The `sealed` keyword prevents a class from being used as a base class.
Question 2: What does the `async` keyword do when applied to a method in C#?
- Runs the method on a new thread
- Marks the method as asynchronous, allowing use of await (Correct answer)
- Compiles the method ahead-of-time
- Makes the method static
Correct answer: Marks the method as asynchronous, allowing use of await
The `async` modifier marks a method as asynchronous, enabling the `await` keyword within it.
Question 3: Which collection in C# is thread-safe and designed for concurrent scenarios?
- List<T>
- Dictionary<K,V>
- ConcurrentDictionary<K,V> (Correct answer)
- ArrayList
Correct answer: ConcurrentDictionary<K,V>
`ConcurrentDictionary<K,V>` in the `System.Collections.Concurrent` namespace is designed for thread-safe access.
Question 4: What is the purpose of the `using` statement in C#?
- Imports a namespace
- Ensures IDisposable objects are properly disposed (Correct answer)
- Declares a variable
- Defines an alias for a type
Correct answer: Ensures IDisposable objects are properly disposed
The `using` statement ensures that `IDisposable` objects are disposed when the block exits, even on exceptions.
Question 5: Which access modifier makes a member accessible only within its own class?
- protected
- internal
- private (Correct answer)
- public
Correct answer: private
The `private` modifier restricts access to the containing class only.
Question 6: What does LINQ stand for in C#?
- Language Integrated Query (Correct answer)
- List Integration Queue
- Linked Input Query
- Language Input Notation Query
Correct answer: Language Integrated Query
LINQ stands for Language Integrated Query, allowing SQL-like queries directly in C# code.
Which keyword in C# is used to prevent a class from being inherited?