Answers to Self-Review Exercises

  1. 8.1 a) arrays. b) variables, type. c) foreach. d) index (or position number). e) two-dimensional. f) foreach (double d in numbers). g) an array of strings, usually called args. h) args.Length. i) test. j) params modifier. k) getter-only.

  2. 8.2 The answers to Self-Review Exercise 8.2 are:

    1. False. An array can store only values of the same type.

    2. False. An array index must be an integer or an integer expression.

    3. For individual value-type elements of an array: False. A called method receives and manipulates a copy of the value of such an element, so modifications do not affect the original value. If the reference of an array is passed to a method, however, modifications to the array elements made in the called method are indeed reflected in the original. For individual elements of a reference type: True. A called method receives a copy of the reference of such an element, and changes to the referenced object will be reflected in the original array element.

    4. False. Command-line arguments are separated by whitespace.

    5. False. As of C# 6, you can use auto-property initialization in auto-implemented property declarations.

  3. 8.3 The answers to Self-Review Exercise 8.3 are:

    1. const int ArraySize = 10;

    2. var fractions = new double[ArraySize];

    3. fractions[3]

    4. fractions[9] = 1.667;

    5. fractions[6] = 3.333;

    6.  

      
      var total = 0.0;
      for (var x = 0; x < fractions.Length; ++x)
      {
         total += fractions[x];
      }
      
    7.  

      
      var total = 0.0;
      foreach (var element in fractions)
      {
         total += element;
      }
      
  4. 8.4 The answers to Self-Review Exercise 8.4 are:

    1. var table = new int[ArraySize, ArraySize];

    2. Nine.

    3.  

      
      for (var x = 0; x < table.GetLength(0); ++x)
      {
         for (var y = 0; y < table.GetLength(1); ++y)
         {
            table[x, y] = x + y;
         }
      }
      
  5. 8.5 The answers to Self-Review Exercise 8.5 are:

    1. Error: Assigning a value to a constant after it’s been initialized.

      Correction: Assign the correct value to the constant in the const declaration.

    2. Error: Referencing an array element outside the bounds of the array (b[10]).

      Correction: Change the <= operator to <.

    3. Error: Array indexing is performed incorrectly.

      Correction: Change the statement to a[1, 1] = 5;.

..................Content has been hidden....................

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