1.
At most, a class can contain___________________ method(s).
Correct Answer
D. Any Number of
Explanation
A class in programming can contain any number of methods. This means that there is no limit to the number of methods that can be defined within a class. The number of methods can vary depending on the requirements of the program and the functionality needed. Therefore, the correct answer is "any Number of".
2.
What is the most important reason for creating methods within a program?
Correct Answer
A. Methods are easily reusable.
Explanation
Creating methods within a program is important because they are easily reusable. By encapsulating a set of instructions within a method, it can be called multiple times throughout the program, reducing code duplication and improving code organization. This enhances the maintainability and readability of the program. Additionally, reusing methods promotes modular programming and allows for easier debugging and testing. Therefore, the ease of reusability is the most important reason for creating methods within a program.
3.
In C#, a method must include all of the following except a______.
Correct Answer
B. Parameter list
Explanation
A method in C# must include a return type, body, and closing curly brace. The parameter list is optional and not required for every method.
4.
A method declaration might contain
Correct Answer
B. a nonstatic modifier
Explanation
A method declaration might contain a nonstatic modifier. This modifier is used to specify that the method belongs to the instance of the class rather than the class itself. It allows the method to access instance variables and methods of the class. Methods with a nonstatic modifier can only be called on an instance of the class.
5.
A method declaration must contain
Correct Answer
D. A return type
Explanation
In order to declare a method, it is necessary to include a return type. The return type specifies the type of value that the method will return after its execution. Without a return type, the method declaration would be incomplete and the compiler would generate an error. The return type is crucial for the proper functioning and usage of the method in a program.
6.
If you want to create a method that other methods can access without limitations, you declare the method to be _________.
Correct Answer
B. Public
Explanation
In order to create a method that can be accessed by other methods without any limitations or restrictions, you declare the method as "public". This means that the method can be accessed from anywhere within the program, allowing other methods to call and use it freely.
7.
If you use the keyword modifier static in a method header, you indicate that the method
Correct Answer
A. Can be called without referring to an object
Explanation
By using the keyword modifier "static" in a method header, it indicates that the method can be called without referring to an object. This means that the method belongs to the class itself rather than an instance of the class. Static methods can be accessed directly using the class name, without the need to create an object of that class. Therefore, the method can be called independently without any object reference.
8.
A method’s type is also its
Correct Answer
C. Return Type
Explanation
The correct answer is "return Type". This means that the type of a method is also determined by its return type. In other words, the data type that the method returns is the type of the method itself. For example, if a method returns an integer, the type of the method would be "int".
9.
When you use a method, you do not need to know how it operates internally. Th is feature is called
Correct Answer
D. Implementation hiding
Explanation
The concept of implementation hiding refers to the ability of using a method without needing to know the details of how it works internally. It allows the user to interact with the method's interface without being concerned about the implementation specifics. This feature promotes encapsulation and abstraction in programming, making the code more modular and easier to maintain.
10.
When you write the method declaration for a method that can receive a parameter, you need to include all of the following items except____________.
Correct Answer
D. An initial value for the parameter
Explanation
When writing a method declaration for a method that can receive a parameter, it is not necessary to include an initial value for the parameter. The initial value is optional and can be assigned within the method body if needed. The other items that need to be included are a pair of parentheses to enclose the parameter, the type of the parameter to specify its data type, and a local name for the parameter to reference it within the method.
11.
Suppose you have declared a variable as int myAge = 21;.Which of the following is a legal call to a method with the declarationpublic static void AMethod(int num)?
Correct Answer
B. AMethod(myAge);
Explanation
The correct answer is AMethod(myAge). This is because the variable myAge is of type int, which matches the parameter type of the AMethod method. Therefore, it is a legal call to pass the value of myAge as an argument to the AMethod method.
12.
Suppose you have declared a method named public static void CalculatePay(double rate). When a method calls the CalculatePay() method, the calling method
Correct Answer
B. Might contain a declared double named rate
Explanation
When a method calls the CalculatePay() method, it is not mandatory for the calling method to have a declared double named rate. It is possible that the calling method might contain a declared double named rate, but it is not a requirement. Therefore, the calling method might or might not contain a declared double named rate.
13.
In the method call PrintTheData(double salary); , salary is the_________ parameter
Correct Answer
B. Actual
Explanation
In the method call PrintTheData(double salary), the parameter "salary" is referred to as the "actual" parameter. This is because it represents the actual value that is passed into the method when it is called. The term "actual" is used to distinguish it from the "formal" parameter, which is the parameter defined in the method signature.
14.
A program contains the method call PrintTheData(salary);. In the method definition, the name of the formal parameter must be
Correct Answer
C. Any legal identifi er
Explanation
In the method definition, the name of the formal parameter can be any legal identifier. This means it can be any combination of letters, digits, and underscores, as long as it starts with a letter or an underscore. It cannot be a reserved word or a number. Therefore, the formal parameter can be named anything that follows these rules, allowing for flexibility in choosing a suitable name for the parameter.
15.
What is a correct declaration for a method that receives two double arguments and calculates and displays the difference between them?
Correct Answer
C. Both of these are correct.
Explanation
Both of these declarations are correct because they both have the correct method name, "CalcDifference," and they both receive two double arguments, "price1" and "price2." The difference between the two arguments can be calculated and displayed within the method body.
16.
What is a correct declaration for a method that receives two double arguments and sums them?
Correct Answer
C. Both of these are correct.
Explanation
Both of these declarations are correct because they both declare a method named CalcSum that receives two double arguments and sums them. The names of the arguments (firstValue/price1 and secondValue/price2) are different, but this does not affect the correctness of the declarations. As long as the method signature matches the requirement of receiving two double arguments and returning void, both declarations are valid.
17.
A method is declared as public static double CalcPay(int hoursWorked). Suppose you write a Main() method in the same class that contains the declarations int hours = 35; and double pay;. Which of the following represents a correct way to call the CalcPay() method from the Main() method?
Correct Answer
D. Pay = CalcPay(hours);
Explanation
The correct way to call the CalcPay() method from the Main() method is "pay = CalcPay(hours)". This is because the CalcPay() method is declared as public static double, meaning it can be accessed directly without creating an instance of the class. The method takes an int parameter called hoursWorked, and in this case, the variable hours is being passed as an argument to the method. The return value of the CalcPay() method is then assigned to the variable pay.
18.
Suppose the value of isRateOK() is true and the value of isQuantityOK() is false. When you evaluate the expression isRateOK() || isQuantityOK(), which of the following is true?
Correct Answer
A. Only the method isRateOK() executes.
Explanation
When evaluating the expression isRateOK() || isQuantityOK(), the "||" operator is a logical OR operator. It returns true if at least one of the conditions is true. In this case, since isRateOK() is true, the overall expression will be true. Therefore, only the method isRateOK() executes.
19.
Suppose the value of isRateOK() is true and the value of isQuantityOK() is false. When you evaluate the expression isRateOK() && isQuantityOK(), which of the following is true?
Correct Answer
C. Both methods execute.
Explanation
When evaluating the expression "isRateOK() && isQuantityOK()", the "&&" operator requires both operands to be true in order for the whole expression to be true. In this case, even though "isQuantityOK()" is false, both methods will still execute because the "&&" operator evaluates both operands regardless of the first operand being false. Therefore, the correct answer is that both methods execute.
20.
When an array is passed to a method, the method has access to the array’s memory address. Th is means an array is passed by
Correct Answer
A. Reference
Explanation
When an array is passed to a method, the method receives the memory address of the array. This means that any changes made to the array within the method will also affect the original array outside of the method. Therefore, the array is passed by reference, allowing the method to directly access and modify the original array.