Skip to main content

Section 11.10 Arrays-WE4-P1

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.

Subsection 11.10.1

Exercises Exercises

Given
int [] alpha = {2, 4, 6, 8, 10, 12, 14};
int sum = 0;
for (int k = 0; k < alpha.length; k+=2)
    sum = sum + alpha[k];
1.
Q16: Given the above code, what is the value in sum after execution?
Given
int [] beta = new int [5];
int sum = 0;
for (int k = 0; k < beta.length; k++)
    beta[k] = 2 * k + 1;
int x = 3;
for (int k = 1; k < beta.length; k++)
    sum += beta[k];
2.
Q17: Given the above code, what is the value in sum after execution?
3.
Q18: Look at the code below:
int [] alpha = {2, 4, 6, 8, 10, 12, 14};
for (int k = 0; k < alpha.length; k+=2) {
    int sum = 0;
    sum = sum + alpha[k];
}
System.out.println(sum);
What is the output of the above code?
  • There is a compiler error so the code does not run. The variable sum is not in scope for the println statement.
  • Correct
  • The output is 0 because the variable is re-initialized every time in the loop.
  • Incorrect
  • The output is 14 because the variable sum only contains the last value in the array.
  • Incorrect
  • The output is 56 which is the sum of all the values in the array.
  • Incorrect
4.
Q19: Look at the code below:
int [] alpha = {2, 4, 6, 8, 10, 12, 14};
int sum;

for (int k = 0; k < alpha.length; k+=2) {
    sum = 0;
    sum = sum + alpha[k];
}
System.out.println(sum);
What is the output of the above code?
  • There is a compiler error so the code does not run. The variable sum is not in scope for the println statement.
  • Incorrect
  • The output is 0 because the variable is re-initialized every time in the loop.
  • Incorrect
  • The output is 14 because the variable sum only contains the last value in the array.
  • Correct
  • The output is 56 which is the sum of all the values in the array.
  • Incorrect
You have attempted of activities on this page.