Preview

13 - Traversing Arrays

 1. In Java, arrays are treated as ________ You can create an array using the new keyword similar to objects and populate it using the indices as ?
int myArray[] = new int[7];
myArray[0] = 1254;
myArray[1] = 1458;
myArray[2] = 5687;
myArray[3] = 1457;
myArray[4] = 4554;
myArray[5] = 5445;
myArray[6] = 7524;

  referenced types.

  classes

  instance variables

  direct values.

 2. It is NOT possible to directly assign values with in flower braces separating them with commas (,) as shown below.
int myArray = { 1254, 1458, 5687, 1457, 4554, 5445, 7524};

  FALSE

  TRUE

 3. Which of the following statements best describes the following code?
public class IteratingArray {
   public static void main(String args[]) {
      //Creating an array
      int myArray[] = new int[7];
      //Populating the array
      myArray[0] = 1254;
      myArray[1] = 1458;
      myArray[2] = 5687;
      myArray[3] = 1457;
      myArray[4] = 4554;
      myArray[5] = 5445;
      myArray[6] = 7524;
      //Printing Contents using for loop
      System.out.println("Contents of the array: ");
      for(int i=0; i

  The array has been set up incorrectly as it has a maximum size of 7 and only 6 elements

  A for loop has been used to iterate through the index numbers starting with 0 to the length of the array

  A for loop has been used to add 1 to each index number of the array

  A for loop has been used but a while loop is the only iteration that can be used with an array

 4. Since JDK 1.5, Java introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially ___________________.
 System.out.println("Contents of the array: ");
      for (int element: myArray) {
         System.out.println(element);

  by using a nested for loop

  without using an index variable

  by using a for loop and println command alone

  by using a direct index variable

 5. What does the following code print out?
// highScores array declaration
int[] highScores = { 10, 9, 8, 8};
// use a variable for the index
int index = 3;
// modify array value at index
highScores[index] = 11;
// print array value at index
System.out.println(  highScores[index] );
System.out.println(  highScores[index - 1] );

  11 and then 8

  8 and then 8 again

  8 and then 11

  8 and then 9

 6. Four names are printed when we run this code. What is the last name to be printed?
public class Test1
{
   public static void main(String[] args)
   {
     String[ ] names = {"Jesus", "Ephraim", "Dontay", "Matthew", "Samuel"};

     int index = 1;
     System.out.println(names[index - 1]);
     index++;
     System.out.println(names[index]);
     System.out.println(names[index/2]);
     names[index] = "Ruth";
     index--;
     System.out.println(names[index+1]);
   }
}

  Samuel

  Ruth

  Matthew

  Jesus

 7. This code seeks to print out the contents of the array, in this case, 10 and then 5. What needs to be added or changed?
public class ArrayLoop
{
    public static void main(String[] args) 
    {

        int[] highScores = { 10,5};
        for (int i = 0; i < highScores.length; i++)
        {
            System.out.println(  highScores[] );
        } 
    }
}

  Line 7's highScores.length should be highScores.length-1

  Line 7 for (int i = 0; i < highScores.length; i++) should not have i++ but length-1 instead

  Nothing is missing or erroneous. It will print the contents of the array correctly

  highScores[] on line 9 should be highScores[i]

 8. When an array is passed as an argument to a method, the name of the array refers to its address in memory.

  FALSE

  TRUE

 9. What is the output of this code?
public class ArrayLoop
{

  // What does this method do?
   public static void magic(int[] values, int amt)
   {
     for (int i = 0; i < values.length; i++)
     {
       values[i] = values[i] * amt;
     }
   }

   // What does this method do?
   public static void printValues(int[] values)
   {
     for (int i = 0; i < values.length; i++)
     {
        System.out.println(  values[i] );
     }
   }

   public static void main(String[] args)
   {
     int[] numArray =  {2,3,4,5};
     magic(numArray, 2);
     printValues(numArray);
   }
}

  4,5,,6,7

  4,6,8,10

  3,4,5,6

  2,3,4,5

 10. Which of the following statements best describes the following code that seeks to execute getIndexOfLastElementSmallerThanTarget(values,-13)?
private int[ ] values = {-20, -15, 2, 8, 16, 33};

public static int getIndexOfLastElementSmallerThanTarget(int[ ] values, int compare)
{
   for (int i = values.length - 1; i >=0; i--)
   {
      if (values[i] < compare)
         return i;
   }
   return -1; // to show none found
}

  The method loops from the back towards the front

  All of the listed options are correct

  The value -15 is the last value in the array

  The value -15 is less than -13 and it is at index 1.

 11. What is wrong with the following code? It seeks to execute getIndexOfLastElementSmallerThanTarget(values, 7);
int[ ] values = {-20, -15, 2, 8, 16, 33};

public static int getIndexOfLastElementSmallerThanTarget(int[] values, int compare)
{
   for (int i = values.length; i >=0; i--)
   {
      if (values[i] < compare)
         return i;
   }
   return -1; // to show none found
}

  There is nothing wrong with the code - it will execute correctly

   You can not start the index at the length of the array. You must start at the length of the array minus one

  The line> for (int i = values.length; i >=0; i--) should end with i-1 not i

  It is not possible to return -1 as this would cause an error

 12. Analyse the following code. What does the && I < 5 do?
  /** Doubles the first 5 elements of the array */
   public void doubleFirstFive()
   {
     // Notice: && i < 5
     for (int i = 0; i < values.length && i < 5; i++)
     {
       values[i] = values[i] * 2;
     }
   }

  None of the listed options are correct

  This is a double addition which adds 5 to the value of each element in the array

  This is a complex conditional to make sure that the loop doesn?t go beyond the length of the array

  This is a concatenation sequence which adds the number 5 to the first element

 13. Given the following values of a and the method doubleLast, the values of a after you execute: doubleLast() would be {-40, -30, 4, 8, 16, 32}.
private int[ ] a = {-20, -15, 2, 8, 16, 33};

public void doubleLast()
{

   for (int i = a.length / 2; i < a.length; i++)
   {
      a[i] = a[i] * 2;
   }
}

  TRUE

  FALSE

 14. The above code loops from the middle to the end doubling each value. Since there are 6 elements it will start at index 3.

  FALSE

  TRUE

 15. Given the following values of a and the method mystery, the values of a after you execute: mystery() would be {-40, -15, 4, 8, 16, 33}.
private int[ ] a = {-20, -15, 2, 8, 16, 33};

public void mystery()
{

   for (int i = 0; i < a.length/2; i+=2)
   {
      a[i] = a[i] * 2;
   }
}

  TRUE

  FALSE

 16. The above code loops from the middle to the end and doubles every other element (i+=2 is the same as i = i + 2).

  FALSE

  TRUE

 17. The following loop header will cause an ArrayIndexOutOfBounds error while traversing the array scores:
 for (int i = 1; i < scores.length; i++)

  TRUE

  FALSE

 18. The index cannot be equal to scores.length, since (scores.length - 1) is the index of the last element. This loop will therefore cause an error while traversing the array scores.
for (int i = 0; i <= scores.length; i++)

  FALSE

  TRUE

 19. This will cause an error because i++ will continue to increment the index past the end of the array. It should be replaced with i? to avoid this error.
for (int i = scores.length - 1; i >= 0; i++)

  FALSE

  TRUE

 20. Assume that temp is an int variable initialized to be greater than zero and that a is an array of integers. Whenever a includes a value that is less than or equal to zero _________________________.
for ( int k = 0; k < a.length; k++ )
{
   while ( a[ k ] < temp )
   {
      a[ k ] *= 2;
   }
}

  the loop will iterate through all the elements except the first

  an infinite loop will arise

  the loop will iterate through all the elements except the last

  the code will execute correctly