8.9. Easy Multiple Choice QuestionsΒΆ

These problems are easier than most of those that you will usually see on the AP CS A exam.

    7-9-1: Which index is the last element in an array called nums at?
  • nums.length
  • Since the first element in an array is at index 0 the last element is the length minus 1.
  • nums.length - 1
  • Since the first element in an array is at index 0 the last element is the length minus 1.
    7-9-2: Which of the following declarations will cause a compile time error?
  • int[] scores = null;
  • You can initialize an array reference to null to show that it doesn't refer to any array yet.
  • int[] scoreArray = {50,90,85};
  • You can provide the values for an array when you declare it.
  • String[] nameArray = new String[10];
  • You can declare and array and create the array using the new operator in the same statement.
  • String[] nameArray = {5, 3, 2};
  • You can not put integers into an array of String objects.
  • int[] scores = new int[5];
  • You can declare and array and create it in the same statement. Use the new operator to create the array and specify the size in square brackets.
    7-9-3: What is returned from arr[3] if arr={6, 3, 1, 2}?
  • 1
  • This would be returned from arr[2].
  • 2
  • This returns the value in arr at index 3. Remember that the first item in an array is at index 0.
  • 3
  • This would be returned from arr[1].
  • 6
  • This would be returned from arr[0].
  • 4
  • This would be returned from arr.length

    7-9-4: What is returned from mystery when it is passed {10, 30, 30, 60}?

    public static double mystery(int[] arr)
    {
       double output = 0;
       for (int i = 0; i < arr.length; i++)
       {
          output = output + arr[i];
       }
       return output / arr.length;
    }
    
  • 17.5
  • This would be true if the loop stopped at arr.length - 1.
  • 30.0
  • This would be true if the loop started at 1 instead of 0.
  • 130
  • This would be true if it returned output rather than output / arr.length
  • 32
  • This would be true if output was declared to be an int rather than a double.
  • 32.5
  • This sums all the values in the array and then returns the sum divided by the number of items in the array. This is the average.

You can step through the code above using the Java Visualizer by clicking on the following link Prob-7-9-4.

    7-9-5: Given the following values of a and the method doubleLast what will the values of a be after you execute: doubleLast()?

    private int[ ] a = {-10, -5, 1, 4, 8, 30};
    
    public void doubleLast()
    {
    
       for (int i = a.length / 2; i < a.length; i++)
       {
          a[i] = a[i] * 2;
       }
    }
    
  • {-20, -10, 2, 8, 16, 60}
  • This would true if it looped through the whole array. Does it?
  • {-20, -10, 2, 4, 8, 30}
  • This would be true if it looped from the beginning to the middle. Does it?
  • {-10, -5, 1, 8, 16, 60}
  • It loops from the middle to the end doubling each value. Since there are 6 elements it will start at index 3.
  • {-10, -5, 1, 4, 8, 30}
  • This would be true if array elements didn't change, but they do.

You can step through the code above using the Java Visualizer by clicking on the following link Prob-7-9-5.

Next Section - 8.10. Medium Multiple Choice Questions