Self-Review Exercises

  1. 7.1 Fill in the blanks in each of the following statements:

    1. A method is invoked with a(n)                .

    2. A variable known only within the method in which it’s declared is called a(n)                .

    3. The                 statement in a called method can be used to pass the value of an expression back to the calling method.

    4. The keyword                 indicates that a method does not return a value.

    5. Data can be added to or removed from only the                 of a stack.

    6. Stacks are known as                 data structures—the last item pushed (inserted) on the stack is the first item popped off (removed from) the stack.

    7. The three ways to return control from a called method to a caller are                , and                .

    8. An object of class                 produces pseudorandom numbers.

    9. The program-execution stack contains the memory for local variables on each invocation of a method during an app’s execution. This data, stored as a portion of the program-execution stack, is known as the                 or                 of the method call.

    10. If there are more method calls than can be stored on the program-execution stack, an error known as a(n)                 occurs.

    11. The                 of a declaration is the portion of an app that can refer to the entity in the declaration by its unqualified name.

    12. It’s possible to have several methods with the same name that each operate on different types or numbers of arguments. This feature is called method                .

    13. The program-execution stack is also referred to as the                 stack.

    14. A method that calls itself either directly or indirectly is a(n)                 method.

    15. A recursive method typically has two components: one that provides a means for the recursion to terminate by testing for a(n)                 case and one that expresses the problem as a recursive call for a slightly simpler problem than does the original call.

    16. In an expression-bodied method or read-only property, the symbol                 introduces the method’s or read-only-property get accessor’s body.

    17. Types are either                 types or                 types.

  2. 7.2 For the class Craps in Fig. 7.8, state the scope of each of the following entities:

    1. the variable randomNumbers.

    2. the variable die1.

    3. the method RollDice.

    4. the method Main.

    5. the variable sumOfDice.

  3. 7.3 Write an app that tests whether the examples of the Math class method calls shown in Fig. 7.2 actually produce the indicated results.

  4. 7.4 Give the method header for each of the following methods:

    1. Method Hypotenuse, which takes two double-precision, floating-point arguments side1 and side2 and returns a double-precision, floating-point result.

    2. Method Smallest, which takes three integers x, y and z and returns an integer.

    3. Method Instructions, which does not take any arguments and does not return a value. [Note: Such methods are commonly used to display instructions to a user.]

    4. Method IntToDouble, which takes integer argument number and returns a double value.

  5. 7.5 Find the error(s) in each of the following code segments. Explain how to correct the error.

    1. 
      void G()
      {
         Console.WriteLine("Inside method G");
         void H()
         {
            Console.WriteLine("Inside method H");
         }
      }
    2. 
      int Sum(int x, int y)
      {
         int result;
         result = x + y;
      }
    3. 
      void F(float a);
      {
         float a;
         Console.WriteLine(a);
      }
    4. 
      void Product()
      {
         int a = 6, b = 5, c = 4, result;
         result = a * b * c;
         Console.WriteLine("Result is " + result);
         return result;
      }
  6. 7.6 Write a complete C# app to prompt the user for the double radius of a sphere, and call method SphereVolume to calculate and display the volume of the sphere. Write an expression-bodied method containing the following expression to calculate the volume:

    
    (4.0 / 3.0) * Math.PI * Math.Pow(radius, 3)
    
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset