Chapter 6 Flashcards
- Which of the following is a valid delegate definition?
a. private delegate float MyDelegate(float);
b. private delegate MyDelegate(x);
c. private delegate MyDelegate(float x);
d. private delegate void MyDelegate(float x);
d. private delegate void MyDelegate(float x);
- Which of the following statements is not true of delegate variables?
a. You need to use a cast operator to execute the method to which a delegate variable refers.
b. A struct or class can contain fields that are delegate variables.
c. You can make an array or list of delegate variables.
d. You can use addition to combine delegate variables into a series of methods and use subtraction to remove a method from a series.
a. You need to use a cast operator to execute the method to which a delegate variable refers.
- If the Employee class inherits from the Person class, covariance lets you do which of the following?
a. Store a method that returns a Person in a delegate that represents methods that return an Employee.
b. Store a method that returns an Employee in a delegate that represents methods that return a Person.
c. Store a method that takes a Person as a parameter in a delegate that represents methods that take an Employee as a parameter.
d. Store a method that takes an Employee as a parameter in a delegate that represents methods that take a Person as a parameter.
b. Store a method that returns an Employee in a delegate that represents methods that return a Person.
- If the Employee class inherits from the Person class, contravariance lets you do which of the following?
a. Store a method that returns a Person in a delegate that represents methods that return an Employee.
b. Store a method that returns an Employee in a delegate that represents methods that return a Person.
c. Store a method that takes a Person as a parameter in a delegate that represents methods that take an Employee as a parameter.
d. Store a method that takes an Employee as a parameter in a delegate that represents methods that take a Person as a parameter.
c. Store a method that takes a Person as a parameter in a delegate that represents methods that take an Employee as a parameter.
- In the variable declaration Action processor, the variable processor represents which of the following?
a. Methods that take no parameters and return an Order object.
b. Methods that take an Order object as a parameter and return void.
c. Methods that take an Order object as a parameter and return an Order object.
d. Methods provided by the Action class that take no parameters and return void.
b. Methods that take an Order object as a parameter and return void.
- In the variable declaration Func processor, the variable processor represents which of the following?
a. Methods that take no parameters and return an Order object.
b. Methods that take an Order object as a parameter and return void.
c. Methods that take an Order object as a parameter and return an Order object.
d. Methods provided by the Action class that take no parameters and return void.
a. Methods that take no parameters and return an Order object.
- Suppose F is declared by the statement Func F. Then which of the following correctly initializes F to an anonymous method?
a. F = (float x) { return x * x; };
b. F = delegate { return x * x; };
c. F = float Func(float x) { return x * x; };
d. F = delegate(float x) { return x * x; };
d. F = delegate(float x) { return x * x; };
- Suppose the variable note is declared by the statement Action note. Then which of the following correctly initializes note to an expression lambda?
a. note = { return x * x; };
b. note = () { return x * x; };
c. note = () => MessageBox.Show(“Hi”);
d. note = MessageBox.Show(“Hi”);
c. note = () => MessageBox.Show(“Hi”);
- Suppose the variable result is declared by the statement Func result. Which of the following correctly initializes result to an expression lambda?
a. result = (float x) => x * x;
b. result = (x) => return x * x;
c. result = x => x * x;
d. Both a and c are correct.
d. Both a and c are correct.
- Which of the following statements about statement lambdas is false?
a. A statement lambda can include more than one statement.
b. A statement lambda cannot return a value.
c. A statement lambda must use braces, { }.
d. If a statement lambda returns a value, it must use a return statement.
b. A statement lambda cannot return a value.
- Suppose the MovedEventHandler delegate is defined by the statement delegate void MovedEventHandler(). Which of the following correctly declares the Moved event?
a. public MovedEventHandler MovedEvent;
b. public event MovedEventHandler MovedEvent;
c. public event Action MovedEvent;
d. Both b and c are correct.
d. Both b and c are correct.
- Suppose the Employee class is derived from the Person class and the Person class defines an AddressChanged event. Which of the following should you not do to allow an Employee object to raise this event?
a. Create an OnAddressChanged method in the Person class that raises the event.
b. Create an OnAddressChanged method in the Employee class that raises the event.
c. Make the Employee class call OnAddressChanged as needed.
d. Make the code in the Person class that used to raise the event call the OnAddressChanged method instead.
b. Create an OnAddressChanged method in the Employee class that raises the event.
- Which of the following statements subscribes the myButton_Click event handler to catch the myButton control’s Click event?
a. myButton.Click += myButton_Click;
b. myButton_Click += myButton.Click;
c. myButton_Click handles myButton.Click;
d. myButton.Click = myButton_Click;
a. myButton.Click += myButton_Click;
- Suppose the Car class provides a Stopped event that takes as parameters sender and StoppedArgs objects. Suppose also that the code has already created an appropriate StoppedArgs object named args. Then which of the following code snippets correctly raises the event?
a. if (!Stopped.IsEmpty) Stopped(this, args);
b. if (Stopped) Stopped(this, args);
c. if (Stopped != null) Stopped(this, args);
d. raise Stopped(this, args);
c. if (Stopped != null) Stopped(this, args);
- Which of the following statements about events is false?
a. If an object subscribes to an event twice, its event handler executes twice when the event is raised.
b. If an object subscribes to an event twice and then unsubscribes once, its event handler executes once when the event is raised.
c. If an object subscribes to an event once and then unsubscribes twice, its event handler throws an exception when the event is raised.
d. In a Windows Forms application, you can use the Properties window to subscribe and unsubscribe events, and to create empty event handlers.
c. If an object subscribes to an event once and then unsubscribes twice, its event handler throws an exception when the event is raised.
- Which of the following statements about inheritance and events is false?
a. A derived class can raise a base class event by using code similar to the following:
if (base.EventName != null) base.EventName(this, args);
b. A derived class cannot raise an event defined in an ancestor class.
c. A class can define an OnEventName method that raises an event to allow derived classes to raise that event.
d. A derived class inherits the definition of the base class’s events, so a program can subscribe to a derived object’s event.
a. A derived class can raise a base class event by using code similar to the following: if (base.EventName != null) base.EventName(this, args);