Answers to Self-Review Exercises

E.1

a) arrays.

b) variables, type.

c) enhanced for statement.

d) index (or subscript or position number).

e) two-dimensional.

f) for ( double d : numbers ).

g) an array of Strings, called args by convention.

E.2

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

b) False. An array index must be an integer or an integer expression.

c) For individual primitive-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.

E.3

a) final int ARRAY_SIZE = 10;

b) double[] fractions = new double[ ARRAY_SIZE ];

c) fractions[ 4 ]

d) fractions[ 9 ] = 1.667 ;

e) fractions[ 6 ] = 3.333 ;

f) double total = 0.0 ;

   for ( int x = 0 ; x < fractions.length; x++ )
      total += fractions[ x ];

E.4

a) int[][] table = new int[ ARRAY_SIZE ][ ARRAY_SIZE ];

b) Nine.

c) for ( int x = 0 ; x < table.length; x++ )
      for ( int y = 0 ; y < table[ x ].length; y++ )
             table[ x ][ y ] = x + y;

E.5

a) Error: Assigning a value to a constant after it has been initialized.

Correction: Assign the correct value to the constant in a final int ARRAY_SIZE declaration or declare another variable.

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

Correction: Change the <= operator to <.

c) 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