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:
Suggested Answer:
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. References: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.aspx http://stackoverflow.com/questions/7052640/how-does-contract-ensures-work
It should be No, Yes, No
The Contract.Ensure method does not throw an exception:
https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.contracts.contract.ensures?view=netframework-4.8#System_Diagnostics_Contracts_Contract_Ensures_System_Boolean_
However about the Contract.Assert method it is said that only throws an exception if it set to do so:
https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.contracts.contract.assert?view=netframework-4.8#System_Diagnostics_Contracts_Contract_Assert_System_Boolean_
The confusing thing about Contract.Ensures is that you write it at the start of the method, but it executes at the end of the method. The condition for balance >= 0 the balance value is changed to 0 and ensures runs at the end and the answer is (no,yes,no)
The confusing thing about Contract.Ensures is that you write it at the start of the method, but it executes at the end of the method. The condition for balance < 0 is true as balance is -1 and then balance is changed to 0 and ensures runs and the result is true as balance = 0 and no error is thrown so (yes, no, yes)
Odd, after testing it i get no, no, no. The logic says no, yes, no, but it seems like Contract.Assert doesn't throw an exception just like the normal assert does.
But i tested it on the .net core, so there may by some differences to old MVC versions.
This is quite odd, just like mr_ mentioned.
Either I do not understand it completely or if I am correct the answer should be: No, No, No. Why? Because none of these methods throw exceptions, regardless of the condition. They display messages only. In order to make them throwing exceptions you would need to utilize other overloads but not the ones presented in the code. For example in order to make Requires method throwing exception you would have to use the following generic version: Requires<TException>(Boolean).
Another important thing is that Assert method of Contract class has nothing to do with Assert class of MSTest toolkit. I do not understand why someone mentioned it in the explanation.
Hopefully I did not make anyone wrong.
https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.contracts.contract.requires?view=netcore-3.1
This page is invalid. "https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.aspx http://stackoverflow.com/questions/7052640/how-does-contract-ensures-work"
And I did not understand how to set 0 on line 21.
Answer is No, Yes, Yes.
Why No for part 1?
- .Requires(name != null) needs to resolve as false to throw an error. but name = "contoso" so no error is thrown.
Why Yes for part 2?
- .Assert(net != 0) needs to resolve false to throw an error. net = 500 - 500, so; 0 == 0. Error is thrown.
Why Yes for part 3?
- .Ensure(balance >= 0.0f) needs to resolve false to throw an error. balance = -1, so ; 0 < 0. Error is thrown. The original answer given is based on a balance of 0. which is not what is asked in the question.
Correction, part 3 = No.
.Ensures checks after all following code in the method. Since balance is set to 0.0f by the end of the method it satisfies the condition.
assert : if the condition is false,
Ensures : if the condition is false,
Requires : if the condition is false
throws an exception
upvoted 3 times
...
...
...
This section is not available anymore. Please use the main Exam Page.70-486 Exam Questions
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
hungry_ape9000
Highly Voted 5 years, 1 month agonhilendarov
Highly Voted 5 years, 3 months agoMicha3l
Most Recent 4 years, 5 months agoMicha3l
4 years, 5 months agoMicha3l
4 years, 5 months agoelevator44
4 years, 9 months agoelathir
4 years, 10 months agopizzaHawai
5 years agomr_
5 years agogokhan
5 years agohungry_ape9000
5 years, 1 month agoGods_Kenny
5 years, 3 months agorhysabray
5 years, 3 months agorhysabray
5 years, 3 months agofounderDev
5 years, 4 months agomajco333
5 years, 4 months agomajco333
5 years, 4 months ago