3.6. Chapter Review

  • Prepare this assignment using a Word document.

  • Write answers under each question.

  • Paste code screenshots when required.

  1. Write the method definition for a static method called Q1 which has two int parameters, x and y, and returns a double.

  2. The method above must have what kind of statement in its body? (see Statements for a formal description of different types of statements for a formal answer or just use what you have learned so far to answer this question.)

  3. Each of these lines below has a call to the method above, Q1. Which are legal? Explain:

    double d = Q1(2, 5);
    
    int x = Q1(2, 5);
    
    double y = Q1(2) + 5.5;
    
    Console.WriteLine(Q1("2", "5"));
    
    Console.WriteLine(Q1(2.5, 5.5));
    
    Q1(10, 20);
    
  4. Suppose Q1 does nothing except produce the value to return, like most methods returning a double. Which line in the previous problem is legal (does not produce error), but has no effect?

  5. Write the method header for a static method called Q4 which has one string parameter, s, and returns nothing.

  6. Which of these lines with a call to the method above, Q4, is legal? Explain:

    Q4("hi");
    
    string t = Q4("hi");
    
    Console.WriteLine(Q4("hi"));
    
    Q4("hi" + "ho");
    
    Q4("hi", "ho");
    
    Q4(2);
    
  7. Can you have more than one function/method in the same class definition with the same name? Explain why. (Google this one for an answer. This question will be covered in the future but is directly related to our topic here.)

  8. What is a function/method signature? Can you have more than one function/method declared in the same class definition with the same signature? (Same note as above)

  9. In each part, is this a legal program? If so, what is printed? If not, why not?

    For the code blocks below, each version uses the same code, except for different versions of Main. Here is the common code with the body of Main omitted:

    using System;
    class Local1
    {
       static int Q(int a)  // 1
       {                    // 2
          int x = 3;        // 3
          x = x + a;        // 4
          return x;         // 5
       }                    // 6
    
       static void Main()
       {
          // see each version
       }
    }
    
    1. Insert:

      static void Main()
      {
         Q(5);
         Console.WriteLine(x);
      }
      
    2. Insert instead:

      static void Main()
      {
         int x= 1;
         Q(5);
         Console.WriteLine(x);
      }
      
    3. Insert instead:

      static void Main()                   // 7
      {                                    // 8
         int x = 1, y = 2;                 // 9
         y = Q(5);                         // 10
         Console.WriteLine(x + " " + y);   // 11
      }                                    // 12
      
  10. In the previous problem consider the common code with part c. Note the line numbers as comments.

    1. In what line(s) is Q being defined?

    2. In what line(s) is Q called?

    3. What is the return type of Q?

    4. What is a formal parameter to Q?

    5. What is used as an actual parameter to Q?

    6. What is the scope of the x in line 3?

    7. What is the scope of the x in line 9?