You work as an application developer at Domain.com. Domain.com uses an application that calculates monthly payments based upon client input.
You are currently debugging this application using the Microsoft Visual Studio 2005 IDE.
The application contains the following code:
public double CalculateMonthlyPayment (Single rate, double principal) {
//Implementation code
}
You have discovered that unexpected results are being returned by the application. You would like to pause execution and display a message box containing an error message in the event of a negative or zero rate value is Passed to the CalculateMonthlyPayment method.
You need to ensure that this only occurs during debugging mode.
What should you do?
A.
Add the following code to the beginning of the CalculateMonthlyPayment method:
Debug.Assert(rate > 0, “Rate Error”, “Rate must be > zero”);
B.
Add the following code to the beginning of the CalculateMonthlyPayment method:
if (rate <= 0)
MessageBox.Show(“Rate is” + rate, Error);
C.
Add the following code to the beginning of the CalculateMonthlyPayment method:
if (rate <= 0)
Debug.WriteLine(“Error Rate is” + rate);
D.
Add the following code to the beginning of the CalculateMonthlyPayment method:
Debug.WriteLineIf(rate <= 0, "Error Rate is" & rate);
Explanation:
This code makes the debugging assertion that the rate argument is greater than zero.
If it is not, then a message box will be display with the message Rate Error along with the detailed description “Rate must be > zero”.
The Assert method of the debug class is an overloaded method that provides you the ability to test assumptions
made in your programming logic.
The Assert method accepts three arguments, the first of which is required. This first argument represents a condition that is assumed to be true for your programming logic and will evaluate to a Boolean value.
The other two arguments represent optional string messages. When the Assert method is invoked with three arguments, the condition is evaluated. If the condition evaluates to true, then the program continues to execute. If the condition evaluates to false, the program execution is halted, and by default a modal dialog box is displayed.
This dialog box displays the first string on a single line, the second string on the second line, and then the location at which the assertion failed.
From this dialog box, you can invoke the debugger, continue execution, or exit the application.
Output generated using the Debug class is stripped out when creating a release version build of an application.
Incorrect Answers:
B: In this scenario, you want the dialog box to display only for debug builds of the application.
Using this code, the message box would display in a release build of the application. C, D: The output of these options will not display in a message box.
A.
This code makes the debugging assertion that the rate argument is greater than zero.
If it is not, then a message box will be display with the message Rate Error along with the detailed description “Rate must be > zero”.
The Assert method of the debug class is an overloaded method that provides you the ability to test assumptions
made in your programming logic.
The Assert method accepts three arguments, the first of which is required. This first argument represents a condition that is assumed to be true for your programming logic and will evaluate to a Boolean value.
The other two arguments represent optional string messages. When the Assert method is invoked with three arguments, the condition is evaluated. If the condition evaluates to true, then the program continues to execute. If the condition evaluates to false, the program execution is halted, and by default a modal dialog box is displayed.
This dialog box displays the first string on a single line, the second string on the second line, and then the location at which the assertion failed.
From this dialog box, you can invoke the debugger, continue execution, or exit the application.
Output generated using the Debug class is stripped out when creating a release version build of an application.
Correct answer is A