Skip to main content

Section 10.2 Arrays-WE1-P1

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 10.2.1

Exercises Exercises

1.
Q1: Assume the following declarations:
String [] alpha;
alpha = new String[4];
Evaluate these statements:
alpha[0] = "apple";
alpha[1] = "Banana";
alpha[2] = alpha[0].substring(2);
alpha[3] = alpha[alpha[1].indexOf('n')];
Give the contents of array alpha after the execution of the above statements:
alpha[0] =
alpha[1] =
alpha[2] =
alpha[3] =
2.
Q2: Assume the following declarations:
int [] beta;
beta = new int[3];
Evaluate these statements:
beta[1] = 22;
beta[0] = beta[1] - 11;
beta[2] = beta[0] + beta[1];
Give the contents of array beta after the execution of the above statements:
beta[0] =
beta[1] =
beta[2] =
3.
    Q3: Which line produces the first compiler error?
  • double [] gamma = new double[5];
  • gamma[0] = 14;
  • gamma[1] = gamma[0];
  • gamma[gamma[0]] = 42;
  • gamma[5] = 22;
You have attempted of activities on this page.