Chapter 5 Arrays

            1.            See the section "Declaring and Creating Arrays."

 

            2.            You access an array using its index.

 

3.         No memory is allocated when an array is declared. The memory is allocated when creating the array.

 

            4.            Indicate true or false for the following statements:

 

                        1.            Every element in an array has the same type.

                                    Answer: True

 

                        2.            The array size is fixed after it is declared.

                                    Answer: False

 

                        3.            The array size is fixed after it is created.

                                    Answer: True

 

                        4.            The element in the array must be of primitive data type.

                                    Answer: False

 

5.            Which of the following statements are valid array declarations?

 

                        int i = new int(30);

                        Answer: Invalid

 

                        double d[] = new double[30];

                        Answer: Valid

 

                        Rational[] r = new Rational(1..30);

                        Answer: Invalid

 

                        int i[] = (3, 4, 3, 2);

                        Answer: Invalid

 

          float f[] = {2.3, 4.5, 5.6};

                        Answer: Valid

 

                        char[] c = new char();

                        Answer: Invalid

 

                        Rational[][] r = new Rational[2];

                        Answer: Invalid

 

            6.            The array index type is int and its lowest index is 0.

 

7.     a[2]

                        The method could be as follows:

 

int find()

{ 

  for (int i=0; i<a.length; i++)

    if (a[i] == 2) return i;

  return -1;

}

 

8.         A runtime exception occurs.

 

9.         The semicolon (;) at the end of the for loop heading should be removed. r(i) should be r[i].

 

            10.            System.arraycopy(source, 0, t, 0, source.length);

 

            11.            int[][] m =  new int[4][5];

 

12.       The second assignment statement myList = new int[20] creates a new array and assigns its reference to myList.

 

13.       Yes. They are ragged array.