HOTSPOT
You are developing an ASP.NET MVC 4 application that includes the following class. Line numbers are
included for reference only.
For each of the following statements, select Yes if the statement is true. Otherwise, select No.
Hot Area:
Explanation:
Box 1: No
Line 7 executes fine as Name is the string Contoso.Box 2: Yes
The assertion of Line 16 fails as the Boolean expression net|=0 evaluates to false (500-500 equals 0).
Note: An assertion verifies an assumption of truth for compared conditions. The assertion is central to the unit
test. The Assert class provides many static methods for verifying suppositions of truth. If the condition being
verified is not true, the assertion fails. The Assert class throws an AssertFailedException to signal a failure.
Box 3: No
The Boolean expression on line 22, balance >= 0.0f, evaluates to 0 >=0.0f (true), as balance is set to 0.0f at
line 21, and because Contract.ensures is a postcondition.
Note: The Contract.Ensures method specifies a postcondition contract for the enclosing method or property.
https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.aspx
http://stackoverflow.com/questions/7052640/how-does-contract-ensures-work
For No3: Ans is Yes,
Since the balance is -1(not 0). The check fails(-1 is less than 0.0).
No, the answer for number 3 is correct.
Contract.Ensures specifies a postcondition contract, which means that balance only has to be greater than 0.0f when the method itself returns. Since line 23 sets balance to 0.0f if it’s less than 0.0f, it’s impossible for line 22 to throw an exception.
See the links below:
“Postconditions express a condition that must be true upon normal termination of the method.”:
https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/code-contracts
“[Contract.Ensures] Specifies a postcondition contract for the enclosing method or property.”:
https://msdn.microsoft.com/en-us/library/system.diagnostics.contracts.contract.ensures(v=vs.110).aspx
Post conditions check the error only when the method is terminating, at the return statement and since the condition sets the balance back to 0.0f if it is less , it will never throw an exception