Unit 7 Cp2 Programing

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Edudzi4k
E
Edudzi4k
Community Contributor
Quizzes Created: 4 | Total Attempts: 4,234
Questions: 20 | Attempts: 311

SettingsSettingsSettings
Programming Quizzes & Trivia

Questions and Answers
  • 1. 

    At most, a class can contain___________________ method(s).

    • A.

      0

    • B.

      1

    • C.

      2

    • D.

      Any Number of

    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".

    Rate this question:

  • 2. 

    What is the most important reason for creating methods within a program?

    • A.

      Methods are easily reusable.

    • B.

      Because all methods must be stored in the same class, they are easy to find.

    • C.

      Th e Main() method becomes more detailed

    • D.

      All of these are true

    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.

    Rate this question:

  • 3. 

    In C#, a method must include all of the following except a______.

    • A.

      Return type

    • B.

      Parameter list

    • C.

      Body

    • D.

      Closing curly brace

    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.

    Rate this question:

  • 4. 

    A method declaration might contain

    • A.

      Declared accessibility

    • B.

      a nonstatic modifier

    • C.

      Multiple return types

    • D.

      parameters separated by dots

    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.

    Rate this question:

  • 5. 

    A method declaration must contain

    • A.

      a statement of purpose

    • B.

      Declared accessibility

    • C.

      The static modifier

    • D.

      A return type

    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.

    Rate this question:

  • 6. 

    If you want to create a method that other methods can access without limitations, you declare the method to be _________.

    • A.

      Unlimited

    • B.

      Public

    • C.

      Shared

    • D.

      Unrestricted

    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.

    Rate this question:

  • 7. 

    If you use the keyword modifier static in a method header, you indicate that the method

    • A.

      Can be called without referring to an object

    • B.

      Cannot be copied

    • C.

      Can be called only once

    • D.

      Cannot require parameters

    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.

    Rate this question:

  • 8. 

    A method’s type is also its

    • A.

      Accessibility

    • B.

      Parameter type

    • C.

      Return Type

    • D.

      Scope

    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".

    Rate this question:

  • 9. 

    When you use a method, you do not need to know how it operates internally. Th is feature is called

    • A.

      Scope management

    • B.

      selective ignorance

    • C.

      Privacy

    • D.

      Implementation hiding

    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.

    Rate this question:

  • 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____________.

    • A.

      A pair of parentheses

    • B.

      the type of the parameter

    • C.

      A local name for the parameter

    • D.

      An initial value for the parameter

    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.

    Rate this question:

  • 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)? 

    • A.

      AMethod(int 55);

    • B.

      AMethod(myAge);

    • C.

      AMethod(int myAge);

    • D.

      AMethod();

    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.

    Rate this question:

  • 12. 

    Suppose you have declared a method named public static void CalculatePay(double rate). When a method calls the CalculatePay() method, the calling method 

    • A.

      Must contain a declared double named rate

    • B.

      Might contain a declared double named rate

    • C.

      Cannot contain a declared double named rate

    • D.

      Cannot contain any declared double variables

    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.

    Rate this question:

  • 13. 

    In the method call PrintTheData(double salary); , salary is the_________ parameter

    • A.

      Formal

    • B.

      Actual

    • C.

      Proposed

    • D.

      Preferred

    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.

    Rate this question:

  • 14. 

    A program contains the method call PrintTheData(salary);. In the method definition, the name of the formal parameter must be

    • A.

      Salary

    • B.

      Any legal identifi er other than salary

    • C.

      Any legal identifi er

    • D.

      omitted

    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.

    Rate this question:

  • 15. 

    What is a correct declaration for a method that receives two double arguments and calculates and displays the difference between them?

    • A.

      Public static void CalcDifference(double price1, price2)

    • B.

      Public static void CalcDifference(double price1, double price2)

    • C.

      Both of these are correct.

    • D.

      None of these are correct.

    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.

    Rate this question:

  • 16. 

    What is a correct declaration for a method that receives two double arguments and sums them?

    • A.

      Public static void CalcSum(double fi rstValue, double secondValue)

    • B.

      Public static void CalcSum(double price1, double price2)

    • C.

      Both of these are correct.

    • D.

      None of these are correct

    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.

    Rate this question:

  • 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?

    • A.

      Hours = CalcPay();

    • B.

      hours = Main.CalcPay();

    • C.

      Pay = CalcPay(hoursWorked);

    • D.

      Pay = CalcPay(hours);

    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.

    Rate this question:

  • 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?

    • A.

      Only the method isRateOK() executes.

    • B.

      Only the method isQuantityOK() executes.

    • C.

      Both methods execute.

    • D.

      Neither method executes.

    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.

    Rate this question:

  • 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?

    • A.

      Only the method isRateOK() executes.

    • B.

      Only the method isQuantityOK() executes

    • C.

      Both methods execute.

    • D.

      Neither method executes.

    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.

    Rate this question:

  • 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

    • A.

      Reference

    • B.

      Value

    • C.

      Alias

    • D.

      Orientation

    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.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Apr 04, 2017
    Quiz Created by
    Edudzi4k
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.