Skip to main content

Section 11.24 Assessment: Arrays 3

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 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 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 11.24.1.
    • I only
    • II only
    • III only
    • I and II only
    • II and III only

    3.

      Q3: 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

    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 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.