Ch 8
When a method header uses the params keyword, two restrictions apply:
- Only one params keyword is permitted in a method declaration - If a method declares multiple parameters, the params -qualified parameter must be the last one in the list
A parameter array ______________________.
- is declared using the keyword params - can accept any number of arguments of the same data type
TryParse() requires two parameters:
- the value to be converted - an out parameter to hold the result
Which of the following pairs of method declarations represent correctly overloaded methods? 1. private static void Method(int a) 2. private static void Method(int b) 1. private static void Method(double d) 2. private static void Method() 1. private static double Method(string e) 2. private static int Method(string f)
1. private static void Method(double d) 2. private static void Method()
Assume that you declare a variable as int x = 100; and correctly pass it to a method with the declaration private static void IncreaseValue(int x). There is a single statement within the IncreaseValue() method: x = x + 25;. Back in the Main() method, after the method call, what is the value of x?
100
Assume you have declared a method as follows: private static double CalculateDiscount(int acct = 0, double price = 0, double discount = 0) Which of the following is a legal method call? - CalculateDiscount(); - CalculateDiscount(200.00); - CalculateDiscount(3000.00, 0.02);
CalculateDiscount();
Assume you have declared a method as follows: private static double ComputeBill(int acct, double price, double discount = 0) . Which of the following is a legal method call? - ComputeBill(); - ComputeBill(1001); - ComputeBill(1001, 200.00);
ComputeBill(1001, 200.00);
TryParse() accepts an out parameter. TryParse() method converts a string to the correct data type and stores the result in a passed variable if possible.
If conversion is not possible, the method assigns 0 to the variable.
Which type of method parameter receives the address of the variable passed in?
Output and Reference parameters
method signature
The name of a method, along with its number and type of parameters.
leaving out unnamed arguments:
When calling a method with optional parameters and you are using unnamed arguments, leave out any arguments to the right of the last one you use - i.e. once an argument is left out, you must leave out all the arguments that would otherwise follow.
When you declare a value parameter, you precede its name with ______________________.
a data type
When one of a method's parameters is optional, it means that ______________________.
a default value will be assigned to the parameter if no argument is sent for it
Assume that you have declared a method with the following header: private static void DisplayScores(params int[] scores) . Which of the following method calls is valid? - DisplayScores(20); - DisplayScores(20, 33); - DisplayScores(20, 30, 90);
all of the above
Assume you have declared a method as follows: private static double DisplayData(string name = "XX", double amount = 10.0) Which of the following is an illegal method call? - DisplayData(name : "Albert"); - DisplayData(amount : 200, name : "Albert"); - DisplayData(amount : 900.00);
all of these are legal
Methods are ambiguous when they _____________________
are indistinguishable to the compiler
Which is not a type of method parameter in C#? - value - reference - forensic - output
forensic
Correctly overloaded methods must have the same ______________________.
identifier
optional parameter
make a parameter optional by providing a value for it in the method declaration - ONLY VALUE PARAMETERS CAN BE GIVEN DEFAULT VALUES - any optional parameters must follow all mandatory parameters
reference parameters, output parameters, and parameter arrays all:
pass memory addresses to the method, allowing the method to alter the original variables
Which of the following is an illegal method declaration? - private static void CreateStatement(int acctNum = 0, double balance) - private static void CreateStatement(int acctNum, double balance = 0.0) - private static void CreateStatement(int acctNum = 0, double balance = 0)
private static void CreateStatement(int acctNum = 0, double balance)
A mandatory parameter ______________________.
requires an argument to be sent from a method call
A reference parameter differs from an output parameter in that a reference parameter ___________ but an output parameter does not.
requires an initial value
The process of determining which overloaded version of a method to execute is overload ______________________.
resolution
If two signatures are equally good, the signature that does not omit optional parameters is considered better
rules for betterness on argument conversions only apply when arguments are given explicitly
ambiguous method
situation where compiler cannot determine which method to use, can happen when you overload methods ex) private static void Method(int a) private static void Method(int apple)
methods are overloaded properly when:
they have the same identifier and different parameter lists - parameter lists are different when the number and order of types within the list are unique
both reference and output parameters represent memory addresses that are passed to a method, which allows the method to alter the original values
true
a reference can be returned from any method
false, there are some restrictions. for example, you can return a reference from a method if the reference was passed to the method, but not if the value was passed into the method
when you declare a reference parameter in a method header, the method must not have an assigned value
false, when you declare a reference parameter in a method header, the parameter must have an assigned value
