Chapter 6 Flashcards

1
Q
  1. 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);
A

d. private delegate void MyDelegate(float x);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. 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

a. You need to use a cast operator to execute the method to which a delegate variable refers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. 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.
A

b. Store a method that returns an Employee in a delegate that represents methods that return a Person.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. 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.
A

c. Store a method that takes a Person as a parameter in a delegate that represents methods that take an Employee as a parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. 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.
A

b. Methods that take an Order object as a parameter and return void.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. 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

a. Methods that take no parameters and return an Order object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. 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; };
A

d. F = delegate(float x) { return x * x; };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. 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”);
A

c. note = () => MessageBox.Show(“Hi”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. 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.
A

d. Both a and c are correct.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. 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.
A

b. A statement lambda cannot return a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. 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.
A

d. Both b and c are correct.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. 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.
A

b. Create an OnAddressChanged method in the Employee class that raises the event.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. 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

a. myButton.Click += myButton_Click;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. 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);
A

c. if (Stopped != null) Stopped(this, args);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. 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.
A

c. If an object subscribes to an event once and then unsubscribes twice, its event handler throws an exception when the event is raised.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. 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.	A derived class can raise a base class event by using code similar to the following:
if (base.EventName != null) base.EventName(this, args);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
  1. 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

a. You can nest a try-catch-finally block inside a try, catch, or finally section.

18
Q
  1. 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.
A

d. Either b or c.

19
Q
  1. 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.
A

d. All of the above.

20
Q
  1. 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.
A

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.

21
Q
  1. 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.
A

c. Make it implement IDisposable.

22
Q

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)

A

d. delegate decimal MyFunc(decimal[] values)

23
Q

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)

A

b. float y = rate(x)

d. double y = rate(x)

24
Q

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.

A

d. There is no practical difference.

25
Q

E4. Which of the following can you do with delegate variables?

a. Make a generic List containing delegate values.
b. Make an array containing delegate values.
c. Use + to add two delegate variables to create a third that represents executing the other two in sequence.
d. Pass the result of executing one delegate variable as a parameter to another delegate variable.

A

A,B,C,D

26
Q

E5. Which of the following statements about delegates is true?

a. You can only set a delegate variable equal to a static method.
b. If you set a delegate variable equal to an instance method, then you must set it equal to a specific object’s instance of a method.
c. If you set a delegate variable equal to an instance method, then it executes in the context of the specific object for which it was created.
d. You can set a delegate variable equal to an instance method and then later set it equal to a static method.

A

b. If you set a delegate variable equal to an instance method, then you must set it equal to a specific object’s instance of a method.
c. If you set a delegate variable equal to an instance method, then it executes in the context of the specific object for which it was created.
d. You can set a delegate variable equal to an instance method and then later set it equal to a static method.

27
Q

E6. Which of the following is a built-in delegate type that represents a method that returns void?

a. Method
b. Static
c. Action
d. Act

A

c. Action

28
Q

E7. Which of the following is a built-in delegate type that represents a method that returns a value?

a. Function
b. Func
c. Returns
d. Action

A

b. Func

29
Q

E8. If variable note is declared as a delegate variable that takes no parameters and returns no value, which of the following statements could you use to initialize note?

a. note = () => MessageBox.Show(“Hi”);
b. note = MessageBox.Show(“Hi”);
c. note(string) = () MessageBox.Show(string);
d. note(s) = () => MessageBox.Show(s);

A

a. note = () => MessageBox.Show(“Hi”);

30
Q

E9. If variable note is declared as a delegate variable that takes a string parameter and returns no value, which of the following statements could you use to initialize note?

a. note = () => MessageBox.Show(string);
b. note = () => MessageBox.Show(“Hi”);
c. note = (s) => MessageBox.Show(s);
d. note = (string a) => MessageBox.Show(s);

A

c. note = (s) => MessageBox.Show(s);

d. note = (string a) => MessageBox.Show(s);

31
Q

E10. Which of the following statements about expression lambdas is true?

a. The lambda’s code must be a single C# statement.
b. The lambda can return a value by simply evaluating an expression as in x * x (hence the name expression lambda).
c. The lambda can return a value by using a return statement.
d. The lambda’s code must be enclosed in braces.
e. The lambda must return a value.

A

a. The lambda’s code must be a single C# statement.

b. The lambda can return a value by simply evaluating an expression as in x * x (hence the name expression lambda).

32
Q

E11. Which of the following statements about statement lambdas is true?

a. The lambda’s code must be a single C# statement.
b. The lambda can return a value by simply evaluating an expression as in x * x.
c. The lambda can return a value by using a return statement.
d. The lambda’s code must be enclosed in braces.
e. The lambda must return a value.

A

c. The lambda can return a value by using a return statement.
d. The lambda’s code must be enclosed in braces.

33
Q

E12. Which of the following correctly declares a variable notify so that it can hold a reference to a method that takes no parameters and returns no result?

a. Action notify;
b. Action notify;
c. Action notify;
d. void notify();

A

a. Action notify;

34
Q

E13. Which of the following correctly declares a variable fxy so that it can hold a reference to a method that takes two floats as parameters and returns a float result?

a. Func fxy(float, float);
b. Func float fxy(float, float);
c. Func fxy;
d. float fxy(float, float);

A

c. Func fxy;

35
Q

E14. When you declare an event, which of the following is always required?

a. The public keyword
b. The event keyword
c. The event’s name
d. A parameter list

A

c. The event’s name

36
Q

E15. When you define an event, Microsoft recommends which of the following?

a. The event handler’s first parameter should be the object raising the event.
b. The event handler’s second parameter should an object of a class derived from EventArgs.
c. The event handler’s third parameter should provide additional information about the event.
d. The name of the second parameter’s class should be the event’s name followed by EventArgs.

A

A,B,D

37
Q

E16. If you have defined an appropriate LostEventType delegate type and the LostEventArgs class, which of the following statements could you use to declare the Lost event?

a. event LostEventType Lost;
b. EventHandler Lost;
c. EventHandler Lost;
d. Action Lost;

A

A,B,D

38
Q

E17. The finally section in a try-catch-finally block executes under which of the following conditions?

a. The code executes without any exceptions.
b. The code throws an exception and a catch section catches it.
c. The code throws an exception and no catch section catches it.
d. The code executes without any exceptions and executes a return statement.
e. The code throws an exception, a catch section catches it, and that catch section executes a return statement.
f. The code throws an exception, a catch section catches it, and that catch section’s code throws an exception.

A

A,B,C,D,E,F

39
Q

E18. If a try-catch-finally block’s catch section throws an exception, which of the following might occur (if the appropriate code is in place)?

a. The block’s finally section executes.
b. A catch section in a method higher up the call stack catches the new exception.
c. Another catch section in the try-catch-finally block catches the exception.
d. The program crashes.

A

A,B,D

40
Q

E19. In a try-catch-finally block, the catch sections are examined in which order?

a. The catch sections with the most specific or derived exception class are examined first.
b. The catch sections’ exception classes are considered in alphabetical order.
c. The catch sections’ exception classes are considered in priority order.
d. The catch sections’ exception classes are considered in numeric order.
e. The catch sections are considered in the order in which they appear in the code.

A

e. The catch sections are considered in the order in which they appear in the code.

41
Q

E20. Which of the following could happen after a catch section executes (assuming the appropriate code is in place)?

a. The next catch section executes if appropriate.
b. The finally section executes.
c. The program resumes execution after the try-catch-finally block.
d. The try section executes again.

A

b. The finally section executes.

c. The program resumes execution after the try-catch-finally block.

42
Q

E21. Which of the following constructs could you use to make a particular piece of code to execute repeatedly until it succeeds?

a. A while loop containing a try-catch-finally block
b. A try-catch-finally block with a while loop in the try section
c. A try-catch-finally block with a while loop in a catch section
d. A try-catch-finally block that contains a catch-while section

A

a. A while loop containing a try-catch-finally block