Skip to main content

Section 9.21 Arrays-WE8-P1

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

Subsection 9.21.2 Arrays-WE8-P1

Exercises Exercises

1.

    Q45: What are the contents of array alpha after this code has been executed?

    int [] alpha = {10, 20, 30, 40, 50, 60, 70};
    int start = alpha[alpha.length-1];
    for (int i = 1; i < alpha.length; i++)  {
       alpha[i] = alpha[i-1];
    }
    alpha[0] = start;
    
  • {20, 30, 40, 50, 60, 70, 70}

  • {20, 30, 40, 50, 60, 70, 10}

  • {10, 20, 30, 40, 50, 60, 70}

  • {10, 10, 20, 30, 40, 50, 60}

  • {70, 10, 10, 10, 10, 10, 10}

2.

    Q46: What are the contents of array beta after this code has been executed?

    int [] beta = {10, 20, 30, 40, 50, 60, 70};
    for (int i = 1; i < beta.length; i++)  {
       beta[i] = beta[i-1])
    }
    
  • {20, 30, 40, 50, 60, 70, 70}

  • {20, 30, 40, 50, 60, 70, 10}

  • {10, 20, 30, 40, 50, 60, 70}

  • {10, 10, 20, 30, 40, 50, 60}

  • {70, 10, 20, 30, 40, 50, 60}

3.

    Q47: What are the contents of array gamma after this code has been executed?

    int [] gamma = {10, 20, 30, 40, 50, 60, 70};
    for (int i = 0; i < gamma.length; i++)  {
       gamma[i] = gamma[i+1])
    }
    
  • {20, 30, 40, 50, 60, 70, 70}

  • {20, 30, 40, 50, 60, 70, 10}

  • {10, 20, 30, 40, 50, 60, 70}

  • {10, 10, 20, 30, 40, 50, 60}

  • IndexOutOfBounds Exception

You have attempted of activities on this page.