Q6 calculate method with 2D int array parameter |
Answer C. Incorrect. Because the algorithm uses a for-each loop to traverse the rows, the row index is not being stored. |
Answer E. Correct. Prior to the start of the nested for loops, the value of found is initialized to the element at row 0, column 0 in values and result is initialized to 0.The outer for loop iterates across all rows in values. The inner for loop iterates across the column indices of each row. When an element is located in the two-dimensional array that is larger than the current value stored in found, then this value is assigned to found and the column index is assigned to result. When the nested loop structure stops executing, the largest element in values is stored in found and the column in which that element was located is stored in result. |
Q7 combine method |
Answer A. Incorrect. This would be the result for the method call combine("00000", "11111"), for example. |
Answer B. Correct. The combine method compares corresponding substrings of length 1 from input strings one and two. If the substrings are the same, the substring is appended to res; otherwise, "0" is appended to res. The first and second characters of res are "0" because the characters in position 0 and the characters in position 1 of one and two differ. The third character of res is "1" because the characters in position 2 of one and two are both "1". The fourth character in res is "0" because the characters in position 3 of one and two differ. The fifth character in res is "0" because the last characters of one and two are both "0". The value "00100" is returned. |
Q33 Print values in 2D int array |
Answer C. Incorrect. This loop iterates over every row of numbers and prints the value returned by calling toString rather than the elements contained in the row. To traverse a 2D array, you need to use nested loops to have row and column index values to access the elements. The proper way to access an element of a 2D array would be numbers[row][column] . |
Answer A. Correct. 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. |
Q39 remove even ArrayList elements |
Answer C. Incorrect. The code segment returns a list with fewer elements than intended because it fails to consider the last element of myList. |
Answer E. Correct. The code segment skips some elements of myList because the indexes of all subsequent elements change by one when a list element is removed. |
Q40 Remove names from a List |
Answer D. Incorrect. II and III only. |
Answer C. Correct. III only. |
Q42 Remove zeros from ArrayList by index |
Answer B. Incorrect. [4, 2, 5, 3] |
Answer D. Correct. [0, 4, 2, 5, 3] |
Q45 strArrMethod - last day of the school year |
Answer D. Incorrect. This would be the result if the method had no if statement and sm was updated once for each pair arr[j] and arr[k] encountered in the nested for loops. |
Answer A. Correct. Line 12 is executed each time the variable sm is updated because a new smallest value is found. When j has the value 0, sm is updated for "day" and "of". When j has the value 1, sm is updated for "of". When j has the value 4, sm is updated for "year". When j has any of the values 2, 3, or 5, sm is not updated. Line 12 is executed four times. |
Q46 StudentInfo class and averageInMajor method |
Answer C. Incorrect. Instance variable major is private to StudentInfo and can only be accessed by calling the accessor method getMajor. |
Answer B. Correct. To calculate the average age in a given major, you have to find all the students in the given major, add up their ages, and divide by the total number of students in the major. Since theMajor is a String, the if statement needs to use .equals to compare theMajor with the major of k, found by calling the getMajor() method on k. If this boolean expression is true, we need to add the age of k, found by calling the getAge() method on k to sum and increase count by 1. |
Q48 sum of ArrayList values |
Answer C. Incorrect. Option I is correct. The code segment uses a for loop to traverse the valueList array. The statement inside the loop calls the get method to access a Value object and then calls the getNum method to access the num instance variable. Option II is correct. The code segment uses an enhanced for loop to traverse the valueList array. The statement inside the loop calls the getNum method to access the num instance variable. Option III is incorrect. The code segment causes a compilation error because the getNum method must be called using the dot operator, not by passing the object reference as an argument. |
Answer D. Correct. Option I is correct. The code segment uses a for loop to traverse the valueList array. The statement inside the loop calls the get method to access a Value object and then calls the getNum method to access the num instance variable. Option II is correct. The code segment uses an enhanced for loop to traverse the valueList array. The statement inside the loop calls the getNum method to access the num instance variable. Option III is incorrect. The code segment causes a compilation error because the getNum method must be called using the dot operator, not by passing the object reference as an argument. |
Q51 Use 1D array method on 2D array |
Answer D. Incorrect. II and III only |
Answer E. Correct. I, II, and III |