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);
- Which of the following statements about exception handling is true?
a. You can nest a try-catch-finally block inside a try, catch, or finally section.
b. A try-catch-finally block must include at least one catch section and one finally section.
c. An exception is handled by the catch section that has the most specific matching exception type.
d. The code in a finally section executes if the code finishes without an error or if a catch section handles an exception but not if the code executes a return statement.
a. You can nest a try-catch-finally block inside a try, catch, or finally section.
- Which of the following methods can you use to catch integer overflow exceptions?
a. Use a try-catch-finally block.
b. Use a checked block and a try-catch-finally block.
c. Check the Advanced Build Settings dialog’s overflow/underflow box, and use a try-catch-finally block.
d. Either b or c.
d. Either b or c.
- Which of the following returns true if variable result holds the value float.PositiveInfinity?
a. result == float.PositiveInfinity
b. float.IsInfinity(result)
c. float.IsPositiveInfinity(result)
d. All of the above.
d. All of the above.
- Which of the following statements about throwing exceptions is false?
a. If you catch an exception and throw a new one to add more information, you should include the original exception in the new one’s InnerException property.
b. If you rethrow the exception ex with the statement throw, the exception’s call stack is reset to start at the current line of code.
c. If you rethrow the exception ex with the statement throw ex, the exception’s call stack is reset to start at the current line of code.
d. Before a method throws an exception, it should clean up as much as possible, so the calling code has to deal with the fewest possible side effects.
b. If you rethrow the exception ex with the statement throw, the exception’s call stack is reset to start at the current line of code.
- Which of the following should you not do when building a custom exception class?
a. Derive it from the System.Exception class, and end its name with Exception.
b. Give it event handlers with parameters that match those defined by the System.Exception class.
c. Make it implement IDisposable.
d. Give it the Serializable attribute.
c. Make it implement IDisposable.
E1. Which of the following statements creates a delegate type named MyFunc that represents a method that takes a parameter that is an array of decimal values and returns a decimal result?
a. delegate decimal(decimal[] values) MyFunc
b. delegate decimal MyFunc(decimal values[])
c. delegate decimal[] MyFunc(decimal values)
d. delegate decimal MyFunc(decimal[] values)
d. delegate decimal MyFunc(decimal[] values)
E2. If rate is a delegate variable that takes a float parameter and returns a float result, which of the following statements will work assuming x is a float variable?
a. int y = rate(x)
b. float y = rate(x)
c. rate(y, x)
d. double y = rate(x)
b. float y = rate(x)
d. double y = rate(x)
E3. If rate is a delegate variable that refers to the method Rate, then what is the practical difference between calling rate or Rate?
a. You can omit the parameters when calling rate.
b. You must save the result of rate into a variable, but you can use the result of Rate in an expression.
c. You can save Rate in another delegate variable, but you cannot save rate in another delegate variable.
d. There is no practical difference.
d. There is no practical difference.