MCTS Application Development & Programming 2 — Questions and Answers
Question 1: In C#, which keyword is used to prevent a class from being inherited?
- abstract
- sealed (Correct answer)
- static
- readonly
Correct answer: sealed
The 'sealed' keyword prevents a class from being used as a base class for inheritance.
Question 2: Which ADO.NET object is used to execute a SQL command that returns a single scalar value?
- SqlDataReader
- SqlDataAdapter
- SqlCommand.ExecuteScalar() (Correct answer)
- SqlCommand.ExecuteNonQuery()
Correct answer: SqlCommand.ExecuteScalar()
ExecuteScalar() returns the first column of the first row from the result set, ideal for aggregate queries.
Question 3: What is the purpose of the 'yield return' statement in C#?
- Returns a value and exits the method
- Produces the next value in an iterator without stopping execution (Correct answer)
- Throws an exception if the value is null
- Forces garbage collection of the returned object
Correct answer: Produces the next value in an iterator without stopping execution
'yield return' pauses execution of an iterator method and returns a value to the caller, resuming on the next iteration.
Question 4: In ASP.NET Web Forms, which event fires before the Page_Load event?
- Page_Init
- Page_PreRender
- Page_Unload
- Page_PreInit (Correct answer)
Correct answer: Page_PreInit
Page_PreInit fires first in the ASP.NET page lifecycle, before Page_Init, and is used to set master pages and themes dynamically.
Question 5: Which .NET collection class provides a thread-safe, first-in-first-out data structure?
- Queue<T>
- ConcurrentQueue<T> (Correct answer)
- Stack<T>
- LinkedList<T>
Correct answer: ConcurrentQueue<T>
ConcurrentQueue<T> in System.Collections.Concurrent is designed for thread-safe FIFO operations without explicit locking.
Question 6: What does the 'using' statement in C# ensure when working with managed resources?
- The object is cached for reuse
- Dispose() is called even if an exception occurs (Correct answer)
- The object is shared across threads
- The object is serialized to disk
Correct answer: Dispose() is called even if an exception occurs
The 'using' statement guarantees that Dispose() is called on an IDisposable object when the block exits, even if an exception is thrown.
Question 7: In Visual Basic .NET, which statement is used to handle multiple values of a variable similar to a switch statement?
- If...Then...Else
- Select Case (Correct answer)
- For Each
- Do While
Correct answer: Select Case
Select Case in VB.NET evaluates an expression and branches to the matching Case block, equivalent to switch in C#.
In C#, which keyword is used to prevent a class from being inherited?