Skip to main content

Section 9.24 Assessment: Arrays 3

Subsection 9.24.1 Subgoals for Evaluating Arrays

  1. Set up array from 0 to size-1

  2. Evaluate data type of statements against array

  3. Trace statements, updating slots as you go

    1. Remember assignment subgoals

Exercises 9.24.2 Exercises

1.

    Q1: Considering the following code segment, what is printed after execution?

    String str = "abcdef";
    for (int k = 0; k < str.length() - 1; k++)
       System.out.print(str.substring(k, k+2);
    
  • abcdef

  • aabbccddeeff

  • abbccddeef

  • abcbcdcdedef

  • Nothing is printed because an IndexOutOfBoundedsException is thrown.

2.

    Q2: 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)

3.

    Q3: The following method is intended to return a String formed by concatenating elements from the parameter words. The elements to be concatenated start with startIndex and continue through the last element of words and should appear in reverse order in the resulting string.

    // Assume that words.length > 0 and startIndex >= 0
    public String concatWords (String [] words, int startIndex) {
       String result = "";
       /* missing code */
       return result;
    }
    

    For example, the execution of the following code segment should result in “CarHouseGorilla” being printed.

    String [] things = {"Bear", "Apple", "Gorilla", "House", "Car"};
    System.out.println(concatWords(things, 2));
    

    Which of the following code segments is a correct replacement for /* missing code */ so that the method will work as intended?

    Figure 9.24.1.
  • I only

  • II only

  • III only

  • I and II only

  • II and III only

4.

    Q4: Consider the following two methods that occur in the same class. What is printed as a result to the call start()?

    public void changeIt (int [] list, int num) {
       list = new int[5];
       num = 0;
       for (int x = 0; x < list.length; x++)
          list[x] = 0;
    }
    
    public void start() {
       int [] nums = {1, 2, 3, 4, 5};
       int value = 6;
       changeIt(nums, value);
       for (int k = 0; k < nums.length; k++)
          System.out.print(nums[k] + " ");
       System.out.print(value);
    }
    
  • 0 0 0 0 0 0

  • 0 0 0 0 0 6

  • 1 2 3 4 5 6

  • 1 2 3 4 5 0

  • No output, an exception is thrown

5.

    Q5: Consider the following two methods that occur in the same class. What is printed as a result to the call start()?

    public void changeAgain (int [] arr, int val, String word) {
       arr = new int[5];
       val = 0;
       word = word.substring(0,5);
       for (int k = 0; k < arr.length; k++)
          arr[k] = 0;
    }
    
    public void start() {
       int [] nums = {1, 2, 3, 4, 5};
       int value = 6;
       String name = "blackboard";
       changeAgain(nums, value, name);
       for (int x = 0; x < nums.length; x++)
          System.out.print(nums[x] + " ");
       System.out.print(value + " ");
       System.out.print(name);
    }
    
  • 0 0 0 0 0 0 black

  • 0 0 0 0 0 6 blackboard

  • 1 2 3 4 5 6 black

  • 1 2 3 4 5 0 black

  • 1 2 3 4 5 6 blackboard

You have attempted of activities on this page.