Preview

14 - Enhanced For Loop for Arrays

 1. Given the following code, it is always legal (okay and permissible) to use a negative number like so: data [-1]
int[] data = new int[10];

  TRUE

  FALSE

 2. What is the output of the following code?
public class ArrayEg3
{
  public static void main ( String[] args )
  {
    double[] val = new double[4];  

    val[0] =  1.5;
    val[1] = 10.0;
    val[2] = 15.5;

    int j  = 3;
    val[j] = val[j-1] + val[j-2];   // same as val[3] = val[2] + val[1]

    System.out.println( "val[" + j + "] == " + val[j] );
 
   }
}

  val[3]==27.0

  val[3]==25.5

  val[1]==1.5

  val[2]==15.5

 3. What is the output of the following code?
public class Test1
{
   public static double getAvg(int[] values)
   {
     double total = 0;
     for (int val : values)
     {
       total  = total + val;
     }
     return total / values.length;
   }

   public static void main(String[] args)
   {
     int[ ] values = {2, 6, 7, 12, 5};
     System.out.println(getAvg(values));
   }
}

  6

  6.4

  Error - the for loop has been constructed incorrectly

  12.5

 4. Only use the for-each loop when you want to loop through all the values in an array or list. If you only want to loop through part of an array or list use a for loop instead.

  TRUE

  FALSE

 5. For-each loops access all elements and enable users to use a variable name to refer to array elements.

  TRUE

  FALSE

 6. For-each loops also allow users to modify elements directly.

  TRUE

  FALSE

 7. Analyse the code below, and fill in the blanks. Since ______ is an object field and the method getAverage is in the same class it can directly access the field ________.
public class ArrayWorker
{
   private int[ ] values;

   public ArrayWorker(int[] theValues)
   {
      values = theValues;
   }

   public double getAverage()
   {
     double total = 0;
     for (int val : values)
     {
       total  = total + val;
     }
     return total / values.length;
   }

   public static void main(String[] args)
   {
     int[] numArray =  {2, 6, 7, 12, 5};
     ArrayWorker aWorker = new ArrayWorker(numArray);
     System.out.println(aWorker.getAverage());
   }
}

  theValues

  numArray

  values

  newArrayWorker

 8. Referring to the code above, the code could have also been written as ____________ to indicate the current object?s field called values.

  class.values

  values.new

  this.values

  class.this

 9. Given that x is an array of integers and val is an integer value, which of the following best describes the conditions under which the following code segment will return true?
boolean temp = false;
for ( int i = 0; i < x.length; i++)
{
  temp = ( x[i] == val );
}
return temp;

  Whenever the first element in x is equal to val.

  Whenever only 1 element in x is equal to val.

  Whenever the last element in x is equal to val.

  Whenever x contains any element which equals val.

 10. In the above example, there is no count of the number of times the array element is equal to val.

  TRUE

  FALSE

 11. Given the following field and method, which of the following best describes the contents of myTrack after (int m = mystery(n);) has been executed?
// private field in the class
private int[ ] myTrack;

//precondition: myTrack contains
//  integers in no particular order
public int mystery(int num)
{
   for (int k = myTrack.length - 1; k >= 0; k--)
   {
       if (myTrack[k] < num)
       {
          return k;
       }
   }

   return -1;
 }

  The smallest value is at position m.

  All values in position m+1 through myTrack.length-1 are less than n.

  All values in positions m+1 through myTrack.length-1 are greater than or equal to n.

   All values in position 0 through m are less than n.

 12. Referring to the code above, mystery loops forward through the array and returns when it finds a value greater than the passed num (n).

  FALSE

  TRUE

 13. Refer to the code below and decide whether this statement is true or false. Whenever a has values larger then temp, an infinite loop is caused.
for ( int k = 0; k < a.length; k++ )
{
   while ( a[ k ] < temp )
   {
      a[ k ] *= 2;
   }
} 

Assumption
===========
Assume that temp is an int 
variable initialized to be 
greater than zero and that 
a is an array of integers.

  TRUE

  FALSE

 14. Again, referring to the above code, whenever a includes a value equal to temp, an infinite loop is caused.

  TRUE

  FALSE

 15. Refer to the code in question 13: Whenever a includes a value that is less than or equal to zero, an infinite loop is caused.

  TRUE

  FALSE