Skip to main content

Section 11.21 Assessment: Arrays 2

Subgoals for Evaluating Arrays.

  1. Declaring and initialization of array
    1. Set up a one dimensional table (i.e., one row) with 0 to (size - 1) elements
    2. Upon instantiation of an array object, all elements contain default value for datatype stored in array OR values from the initializer list
  2. Determine access or change of element, or action on entire array object, and update slots as needed (remembering assignment subgoals)
  3. Accessing array element
    1. Evaluate expression within [] which will be the index for element to be accessed
    2. arrayName[index] returns value stored at that index
    3. index must be between 0 and arrayName.length - 1, inclusive otherwise IndexOutOfBounds exception occurs
  4. Changing value of an array element
    1. Evaluate expression within [] which will be the index for element to be accessed
    2. arrayName[index] will now contain the value on the RHS of assignment statement
    3. (remember the assignment subgoals for verifying data types and evaluating expressions)
    4. (remember rules for index values)
  5. Whole array actions
    1. Pass as argument - a copy of the reference to the instantiated array is passed to the method. This means that any changes made to the array elements inside the method are persistent. The one exception to this is if you assign the argument to reference a different array in memory.
    2. Assignment - changes the reference to point to the array on the RHS of the assignment operator.

Exercises Exercises

    1.

      Q1: The following code is intended to store the sum of all the values in the integer array arr in the variable total. Which of the following code segments can be used to replace /* missing code */ so that the code works as intended?
      int total = 0;
      /* missing code */
      System.out.println(total);
      
      Figure 11.21.1.
    • I only
    • II only
    • III only
    • I and III only
    • II and III only

    2.

      Q2: Assuming that nums has been declared and initialized as an array of integer values, which of the following best describes what this code does?
      int index = 0;
      int count = 0;
      int m = -1;
      for (int outer = 0; outer < nums.length; outer++) {
         count = 0;
         for (int inner = outer + 1; inner < nums.length; inner++) {
            if (nums[outer] == nums[inner])
               count++;
         }
         if (count > m) {
            index = outer;
            m = count;
         }
      } // end outer for
      System.out.println(index);
      
    • Prints the maximum value that occurs in the array nums
    • Prints the index of the maximum value that occurs in the array nums
    • Prints the number of times that the maximum value occurs in the array nums
    • Prints the value that occurs most often in the array nums
    • Prints the index of the value that occurs the most often in the array nums

    3.

      Q3: The following code is intended to store the largest value in the integer array arr in the variable maxVal. Which of the following best describes the conditions under which the code will not work as intended?
      int maxVal = 0;
      for (int val : arr) {
         if (val > maxVal)
            maxVal = val;
      }
      
    • The largest value in arr occurs only once and is in arr[0].
    • The largest value in arr occurs only once and is in arr[arr.length-1].
    • The largest value in arr is negative.
    • The largest value in arr is 0.
    • The largest value in arr occurs more than once.

    4.

      Q4: The following code segments are supposed to find the maximum value in an array of integers. Assuming that the array arr has been declared and contains valid integer values, which of the following code segments will correctly assign the maximum value in the array to the variable max?
      Figure 11.21.2.
    • I only
    • II only
    • III only
    • II and III
    • I, II, and III

    5.

      Q5: The following method is intended to return the index of the first occurrence of the value val beyond the position start in the array arr.
      // returns index of first occurrence of val in arr after
      // position start;
      // returns arr.length if val is not found
      public int findNext (int [] arr, int val, int start) {
         int pos = start + 1;
         while ( /* condition */)
            pos++;
         return pos;
      }
      
      For example, the execution of the following code segment should result in the value 4 being printed:
      int [] arr = {11, 22, 100, 33, 100, 11, 44, 100};
      System.out.println(findNext(arr, 100, 2));
      
      Which of the following expressions could be used to replace /* condition */ so that findNext will work as intended?
    • (pos < arr.length) && (arr[pos] != val)
    • (arr[pos] != val) && (pos<arr.length)
    • (pos<arr.length) || (arr[pos] != val)
    • (arr[pos] == val)&& (pos<arr.length)
    • (pos<arr.length) || (arr[pos] == val)
You have attempted of activities on this page.