34/39

image

Q4

image Answer should have been c. Since maxVal is initialized to a value that is greater than all values in arr, maxVal will never be updated and 0 will be returned. To correct this, maxVal could be initialized to the first element in arr or initialized to Integer.MIN_VALUE.

Q18

image Answer should have been D. The first operation that is executed is 404 / 10. Since both 404 and 10 are integers, integer division is used resulting in 40. The value 40 is then multiplied by 10, resulting in 400, and finally 1 is added, meaning 401 is printed.

Q20

image Answer should have been E. The outer loop starts at 0 and loops through all the indices in arr. The inner loop starts at the index that is one more than outer and iterates through all indices to the right of this element. For each iteration of the inner loop, the element at the current value of outer is compared with each subsequent element. If the elements are equal, then count is incremented. This results in counting the number of occurrences of each value in the arr. After the inner loop terminates, if the number of occurrences of the current value is greater than previous highest count, the new count is assigned to m and the index of this element is stored in index. The method then returns the value of index, which represents the index of a value that occurs most often in nums.

Q22

image Answer should have been A. The outer for loop iterates over every row of numbers and assigns each row to the array row. The inner loop iterates over the array row accessing each element and assigning it to n. Then n is printed to the screen. In the first iteration of the outer loop, row is equal to {1, 2, 3}, and the inner loop will assign each successive value in row to n and print it to the screen, meaning 123 will be printed. For the second iteration of the outer loop, row is equal to {4, 5, 6}, and the inner loop will assign each successive value in row to n and print it to the screen, meaning 456 will be printed after 123, giving us the output 123456.

Q33

image Answer should have been E. Choice I sets max to Integer.MIN_VALUE, which is the smallest possible integer value. Then it accesses each element in arr and assigns them value. If value is greater than max, max is assigned value since it is now the largest value so far. Choice II uses an if statement inside the for loop to check and see if value is the first element in arr or not. Once the first element is identified, max is initialized to the first element and first is set to false. For all subsequent elements in arr, if value is greater than max, max is assigned value since it is now the largest value so far. Choice III sets max to the first value in arr. Then it accesses each subsequent value in arr checking to see if the value is greater than max, if it is max is assigned this element since it is now the largest value so far.