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.
Q1: The following code is intended to store the sum of all the values in the integer array arr in the variable total. Which of the following code segments can be used to replace /* missing code */ so that the code works as intended?
int index = 0;
int count = 0;
int m = -1;
for (int outer = 0; outer < nums.length; outer++) {
count = 0;
for (int inner = outer + 1; inner < nums.length; inner++) {
if (nums[outer] == nums[inner])
count++;
}
if (count > m) {
index = outer;
m = count;
}
} // end outer for
System.out.println(index);
Prints the maximum value that occurs in the array nums
Q3: The following code is intended to store the largest value in the integer array arr in the variable maxVal. Which of the following best describes the conditions under which the code will not work as intended?
Q4: The following code segments are supposed to find the maximum value in an array of integers. Assuming that the array arr has been declared and contains valid integer values, which of the following code segments will correctly assign the maximum value in the array to the variable max?
// 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: