13.4. Preview Questions#

Complete these questions before class to prepare for the chapter. Use the dropdowns to check your answers.

13.4.1. True or False#

1. A try block must always be paired with a catch block.

Answer

Falsetry can be paired with finally alone, without catch.

2. A finally block executes only when an exception is thrown.

Answer

Falsefinally always runs — whether an exception occurred or not.

3. Catching Exception (the base type) is best practice for all error handling.

Answer

False — Catching specific exception types gives more control and clearer intent.

4. A NullReferenceException occurs when you access a member on a null reference.

Answer

True — Calling a method or property on a null object throws this exception.

5. Breakpoints pause program execution at a specific line during debugging.

Answer

True — Breakpoints let you inspect program state at exact points in the code.

13.4.2. Multiple Choice#

1. Which keyword manually throws an exception in C#?

a) raise
b) error
c) throw
d) except

Answer

c) throwthrow new ExceptionType() raises an exception programmatically.

2. Which block always executes regardless of whether an exception was thrown?

a) try
b) catch
c) finally
d) else

Answer

c) finallyfinally is guaranteed to run — ideal for cleanup code.

3. What exception is thrown when dividing an integer by zero?

a) ArithmeticException
b) DivideByZeroException
c) ZeroException
d) MathException

Answer

b) DivideByZeroExceptionDivideByZeroException is thrown for integer division by zero.

4. Best practice for exception handling is to:

a) Always catch Exception to handle everything
b) Avoid try/catch entirely
c) Catch the most specific exception type possible
d) Re-throw every exception

Answer

c) Catch the most specific exception type possible — Specific catches make error handling intentional and easier to debug.

5. During debugging in VS Code, which panel shows variable values at a breakpoint?

a) Terminal
b) Variables panel in the Run & Debug view
c) Source Control panel
d) Extensions panel

Answer

b) Variables panel in the Run & Debug view — The Variables pane in the Run & Debug view shows current values.

Footnotes


13.4.3. Unit Testing — True or False#

1. A unit test should test multiple units of code together to save time.

Answer

False — Unit tests test one small, isolated piece of functionality.

2. Assert.AreEqual(expected, actual) checks whether two values are the same.

Answer

True — If the values differ, the assertion fails and the test is marked as failed.

3. Test-Driven Development (TDD) means writing tests after the code is complete.

Answer

False — In TDD you write the test first, then write code to make it pass.

4. An MSTest test method is marked with the [TestMethod] attribute.

Answer

True[TestMethod] marks a test method in MSTest; [Fact] is used in xUnit.

5. A test that always passes regardless of the implementation is a useful test.

Answer

False — A test must be able to fail to be meaningful.

13.4.4. Unit Testing — Multiple Choice#

1. What is the main purpose of a unit test?

a) Test the full application end-to-end
b) Test one small, isolated piece of code
c) Test the user interface
d) Test network connectivity

Answer

b) Test one small, isolated piece of code — Unit tests isolate a single method or behavior to verify correctness.

2. Which attribute marks a test method in MSTest?

a) [Test]
b) [Fact]
c) [TestMethod]
d) [Check]

Answer

c) [TestMethod][TestMethod] is MSTest’s attribute; [Fact] belongs to xUnit.

3. What does a failing test indicate?

a) The test framework is broken
b) The code does not meet the stated expectation
c) The test should be deleted
d) Nothing important

Answer

b) The code does not meet the stated expectation — A failing test reveals a discrepancy between expected and actual behavior.

4. Which MSTest method verifies that a specific exception is thrown?

a) Assert.IsTrue()
b) Assert.ThrowsException<T>()
c) Assert.Fails()
d) Assert.Catch<T>()

Answer

b) Assert.ThrowsException<T>() — Passes only if the code block throws exception type T.

5. The Red-Green-Refactor cycle in TDD means:

a) Test passes, code fails, then you refactor
b) Write a failing test, make it pass, then clean up the code
c) Write code, test it, then delete the tests
d) Run tests in red mode for performance

Answer

b) Write a failing test, make it pass, then clean up the code — Red = failing test, Green = minimal code to pass, Refactor = clean up.