exam questions

Exam 70-486 All Questions

View all questions & answers for the 70-486 exam

Exam 70-486 topic 1 question 80 discussion

Actual exam question from Microsoft's 70-486
Question #: 80
Topic #: 1
[All 70-486 Questions]

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:

Show Suggested Answer Hide Answer
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

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
hungry_ape9000
Highly Voted 5 years, 1 month ago
FINAL ANSWER: No Yes No
upvoted 15 times
...
nhilendarov
Highly Voted 5 years, 3 months ago
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_
upvoted 10 times
...
Micha3l
Most Recent 4 years, 5 months ago
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)
upvoted 1 times
...
Micha3l
4 years, 5 months ago
i mean (no,yes,no)
upvoted 1 times
...
Micha3l
4 years, 5 months ago
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)
upvoted 1 times
...
elevator44
4 years, 9 months ago
Tested with code https://dotnetfiddle.net/d215nm Only exception was the middle one so answer is No, Yes, No
upvoted 4 times
...
elathir
4 years, 10 months ago
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.
upvoted 2 times
...
pizzaHawai
5 years ago
Correct answer is: No Yes No
upvoted 5 times
...
mr_
5 years ago
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
upvoted 2 times
...
gokhan
5 years ago
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.
upvoted 1 times
...
hungry_ape9000
5 years, 1 month ago
FINAL ANSWER: No Yes Yes
upvoted 1 times
...
Gods_Kenny
5 years, 3 months ago
It should be No, Yes, Yes
upvoted 1 times
...
rhysabray
5 years, 3 months ago
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.
upvoted 2 times
rhysabray
5 years, 3 months ago
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.
upvoted 9 times
...
...
founderDev
5 years, 4 months ago
assert : if the condition is false, Ensures : if the condition is true, Requires : if the condition is true throws an exception
upvoted 3 times
majco333
5 years, 4 months ago
Exactly the opposite...
upvoted 1 times
majco333
5 years, 4 months ago
assert : if the condition is false, Ensures : if the condition is false, Requires : if the condition is false throws an exception
upvoted 3 times
...
...
...
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.

SaveCancel
Loading ...