I'm studying for the 70-536 exam, which is about a开发者_JS百科pplication development for the .NET framework.
I've bought a book to help me study, this book includes a CD which has test exams on it. Taking on the exam I encountered the following question:
Given the following C# code sample, how could you display the contents of the host string in the
ProcessDnsInformation
method?
Code sample:
AsyncCallback callBack = new AsyncCallback(ProcessDnsInformation);
string host = Console.ReadLine();
Dns.BeginGetHostEntry(host, callBack, host);
And then I had 4 options and only one could be the correct answer(A, B, C, D):
static void ProcessDnsInformation(IAsyncResult result)
{
Console.WriteLine(result.AsyncState);
}
static void ProcessDnsInformation(IAsyncResult result)
{
Console.WriteLine((string)result.AsyncState);
}
static void ProcessDnsInformation(IAsyncResult result)
{
Console.WriteLine(result.ToString());
}
static void ProcessDnsInformation(IAsyncResult result, string host)
{
Console.WriteLine(host);
}
I went for A, because C and D are obviously wrong. To my surprise A was also wrong, the program told me the correct answer should've been B. Which answer would you pick as correct, and why?
Assuming result.AsyncState is declared as an object but actually contains a string, then I would say that both A and B are both correct.
But if you are only allowed one correct answer, then you pick the one which is best and I would say that, in general, if you are returned an object, you would normally cast it back to its actual type before using it. In some cases you have to do that because the function you are calling will not accept an object, or because you need to access one of its properties, and so I guess this is probably why they reckon B is the 'correct' answer.
I have not done this particular exam, but I have done another Microsoft exam and I found quite a few of the questions fell into the subjective category where I had to pick one out of two answers neither of which I really considered better. In the end, if you do enough practice tests, you learn what answer they want.
精彩评论