i believe the answer to the first one is Yes, although you could technically change the value after it had already been set, the intention of the code is clearly for that to be impossible, and the technique pointed out by Difdoor (there may be others) seems too advanced for the scope of this exam.
side note: defining only a get would ensure that the property _name can only be set in the constructor.
private string _name { get; }
and there are more robust ways to also ensure the consistency of a property than if(_name != null) that should also be used alongside this approach.
- You can change Key from "player" to "racer"
Key is a constant so you can't change that value, that's clear. But the other two answers are debatable:
- You can only set the players ranking once.
That depends, if the name was previously set by passing it to the SetResult() method you can only set it once, but if you pass null to the "name" argument of SetResult() you can set it as many times as you want.
- If you call the MatchName method, the code will throw an exception.
That also depends on whether or the name was set using SetResult(). If it wasn't MatchName() will throw a NullPointerException, but if the name has been set that method will not throw an exception.
wouldn't this be yes, no, no?
MatchName can trow an exception if name is not set, but we don't have the code where it's called so we don't know if the name is not set. it will always return false because == is not the correct tool to use for strings, but it's not guaranteed to throw an exception.
(also isn't ranking set at any time SetResults is called?)
As for the first question, it is obviously meant to be impossible to set the ranking twice, however doing so multiple times is way easier than Difdoor's suggesting: You just have to pass null as a name and don't need any exception handling to pass multiple rankings.
For the second question: Of course the bool MatchName won't throw an exception if there is a name present. However, if no name has been passed using SetResult yet it will throw a NullReferenceException.
Question 3 is a no-brainer, consts cannot be changed.
All in all this is a terrible question with a lot of speculation possible. In the end I think they want us to answer 'Yes, No, No'.
I rewrote the class and ran the following snippet to fact-check my speculations:
Player player = new Player();
player.SetResult(5, null);
player.SetResult(10, "Foo");
player.SetResult(15, "Will return");
Console.WriteLine(player.Ranking);
Console.WriteLine(player.MatchName("foo"));
Returns:
10
True
On question 1, I think you could reset the Ranking but only if you initially called SetResult with an int and a null string which (I think) would throw an exception after assigning the rank. You would need to catch this exception and then call SetResult with valid values resetting the values of rank and name. (Very contrived but you almost always call SetResult in a try-catch.) So No for question 1.
upvoted 1 times
...
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.
cdc
Highly Voted 2 years agoHashino
1 year, 4 months agoBirdy
Highly Voted 2 years agoLets123
6 months, 2 weeks agooneup
Most Recent 2 months agovinx909
2 months, 2 weeks agoKisima
4 months, 3 weeks agoPomphard
9 months, 1 week agoDifdoor
1 year, 11 months ago