Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multicast Delegates in 70-536 exam?

Tags:

c#

.net

delegates

I am going through some exam questions for the 70-536 exam and an actual question one developer posted on his blog has popped up in my exam questions. I cannot remember what his answer was .... but below is the question:

You need to write a multicast delegate that accepts a DateTime argument and returns a bool value. Which code segment should you use?

A: public delegate int PowerDeviceOn(bool, DateTime)

B: public delegate bool PowerDeviceOn(Object, EventArgs)

C: public delegate void PowerDeviceOn(DateTime)

D: public delegate bool PowerDeviceOn(DateTime)

The answer is A.

Can someone please explain why? As I already did some research into this question a while ago and so I was sure that it was C, obviously now looking back at the question its clear that I did not read properly. As i was sure I had seen the same one before so I jumped to the most obvious one.

A variation on this question:

You need to write a multicast delegate that accepts a DateTime argument. Which code segment should you use?

A: public delegate int PowerDeviceOn(bool, DateTime)

B: public delegate bool PowerDeviceOn(Object, EventArgs)

C: public delegate void PowerDeviceOn(DateTime)

D: public delegate bool PowerDeviceOn(DateTime)

Now this is another variation on this question, it still has the same bogus sample answers, as they still kind work in throwing the exam taker off. Notice how by simply keeping the sample answers the same and by removing a small portion of the question text, the answer is C and not A.

The variation has no official answer as I just conjured it up using the exam question as a baseplate. The answer is definitely C. This time round its easy to see why C is correct but the very first question I have an inkling but as you know an inkling is not good enough in passing exams.

like image 217
IbrarMumtaz Avatar asked May 03 '10 16:05

IbrarMumtaz


2 Answers

This is not a very well worded question. A multicast delegate is when you have combined separate delegates into one:

delegate int Foo();
Foo a = () => 5;
Foo b = () => 9;
Foo c = a + b; // c is a multicast delegate

When you call c, it invokes a, then b. It returns the return value of the last delegate invoked, so the return value for c is 9.

In my opinion, the answer should be

public delegate void PowerDeviceOn(DateTime d, CancelEventArgs e)

And if one of the methods the delegate is pointing to wants to tell you "false", they should set e.Cancel to true. The delegate can't just return a boolean, because then you'd only get the last delegate's answer.

like image 64
Matt Greer Avatar answered Sep 28 '22 07:09

Matt Greer


First of all, by definition, all delegate instances in .NET are multicast delegates, even with 0 or 1 actual functions attached to them.

Strictly speaking, the only delegate (multicast being superfluous) that fits the actual description for problem 1 is D. That is the only functions that accepts a DateTime parameter and returns a bool.

In fact, answer A doesn't meet the requirements explicitly or even conceptually. If the bool parameter were a ref parameter, it would at least be capable of returning a boolean value to the calling code. As it stands, you'd have to check that the return value was > 0.

like image 35
Adam Robinson Avatar answered Sep 28 '22 07:09

Adam Robinson