Professor Mustard Programming

Programming 101 - Unit 03 - Modularity

03z) Review
To keep these concepts fresh in your mind, answer the following questions, and then check your answers at the bottom. For best results as a memory tool, answer these questions a few hours after reading the original material.

1) Which of the following statements is false?

The "return" keyword...
a) ...may be used in any method, even if the method's return type is "void".
b) always required in a method that returns a value, even if an exception is thrown.
c) permitted to occur more than once in the same method.
d) ...allows methods to send information back to the code that called them.

2) Which of the following are valid ways to overload a method?
a) Change the method's return type.
b) Change the number of parameters accepted by the method.
c) Change the method's parameter types.
d) Change the method's name.

3) Explain the difference between the "out" and "ref" keywords.

1) b)
If you hit an exception, and the method doesn't catch it, you'll be "blown back" to the calling code, still looking for a "catch" statement. If this happens, the flow of the program has already been diverted away from the method call, so we no longer care what the return value was.

2) b) and c)
Parameters are the key difference in method overloads, whether it's changing the number of parameters or their type. Changing the method's return type won't work, because C# can't assume which return type you're asking for (even if you're assigning the result directly to a variable). Changing the method name is just plain wrong, because a set of overloaded methods, by definition, share the same name.

3) "ref" is used so that a method has access to the original variable passed into it. In contrast, "out" is used so that a method can assign a value to the original variable passed into it. So, both keywords provide access to the original variable, but "out" basically assumes that the variable's original value is irrelevant.