Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA. two multiple choice questions. 1.Consider the following method. public sta

ID: 3814066 • Letter: J

Question

JAVA. two multiple choice questions.

1.Consider the following method.

public static int f (int x, int[] arr) {
for (int k = arr.length - 1; k >= 0; k--)
    if (arr[k] < x) return k;
return -1;
}

Initially, suppose that arr is an arbitrary (unsorted) array and n is
an arbitrary number. Which of the following best describes the
contents of arr after the following statement has been executed?

int m = f (n, arr);

a. The smallest value is at position m.

b. All values in positions m+1 through arr.length-1 are greater than or equal to n.

c. All values in positions 0 through m are less than n.

d. All values in positions m+1 through arr.length-1 are less than n.

e. The largest value that is smaller than n is at position m.

2.Consider the following method with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended.

For example, if the array arr1 is [7,10,10,15,15,15,15,10,10,10,15,10,10], the call findLongest(10, arr1) should return 3, the length of the longest consecutive block of 10s.

As another example, if the array arr2 is [7,10,10,15,15,15,15,10,10,15,10,10], the call findLongest(10, arr2) should return 2.

1 public static int findLongest(int target, int[] nums)
2 {
3   int lenCount = 0;
4   int maxLen = 0;
5   for (int i = 0; i < nums.length; i++) {
6     if (nums[i] == target) {
7       lenCount++;
8     } else {
9       if (lenCount > maxLen) {
10         maxLen = lenCount;
11       }
12     }
13   }
14   if (lenCount > maxLen) {
15     maxLen = lenCount;
16   }
17   return maxLen;
18 }

Which of the following changes should be made so that method findLongest will work as intended?

a. Insert “lenCount = 0;” between lines 11 and 12

b. Insert “lenCount = 0;” between lines 9 and 10

c. Insert “lenCount = 0;” between lines 8 and 9

d. Insert “lenCount = 0;” between lines 5 and 6

e. Insert “lenCount = 0;” between lines 10 and 11

Explanation / Answer

public static int f (int x, int[] arr) {
for (int k = arr.length - 1; k >= 0; k--)
    if (arr[k] < x) return k; //this loop breaks as soon as arr[k] is less than x
//so this function returns the largest value that is smaller than n is at position m.
//(largest because the loop is decreasing from arr.length-1 through 0
return -1;
}

Therefore the answer is e) The largest value that is smaller than n is at position m.

for this problem, we need to reset the lenCount once we find an integer in the array that is not equal to the provided integer(i.e. target). We also need to record the maxLength before we reset the lenCount.

1 public static int findLongest(int target, int[] nums)
2 {
3   int lenCount = 0;
4   int maxLen = 0;
5   for (int i = 0; i < nums.length; i++) {
6     if (nums[i] == target) {
7       lenCount++;
8     } else { // <- we need to reset lenCount here in this else block, but if we reset before if block, we cannot record maxlength
9       if (lenCount > maxLen) { //we cannot reset lenCount here, because this is a special case, and might not be true always
10         maxLen = lenCount;
11       }
11a //this is the best place to insert the code to reset lenCount
12     }//end of else
13   }//end of for
14   if (lenCount > maxLen) {
15     maxLen = lenCount;
16   }
17   return maxLen;
18 }

So the correct option is a) Insert “lenCount = 0;” between lines 11 and 12

I inserted comments in the code to make your life easy. I hope you like the solution and explanation. Incase you face any difficulty understanding the problem, feel free to comment below. I shall be glad to help you solve the problem.