52 CB MC Tri 2 Corrections + Reflection
- Total score: 44/52
- Q14 count 2D array columns
- Q16 count words in ArrayList wordList
- Q24 loop on string abcdef
- Q25 manipulate method and animals List
- Q26 mystery method with String parameter and substring
- Q30 print from 2D array
- Q37 processWords method
- Q41 remove some ArrayList elements
Q14 count 2D array columns
Answer B is incorrect. The enhanced for loop in line 8 is intended to traverse the rows of the two-dimensional array arr. Each row o f arr is a one-dimensional array of int values, int[] row. Answer C is correct. Two-dimensional arrays are stored as arrays of one-dimensional arrays. Line 8 is intended to assign to row, a one-dimensional array of int values, a single row of the two-dimensional array arr. The original version of line 8 attempts to assign a row of col, but col is not a two-dimensional array.
Q16 count words in ArrayList wordList
Answer B is incorrect. This result would be printed if the if statement was changed to if (word.indexOf("a") < 0). Answer D is correct. The code segment uses an enhanced for loop to traverse wordList. The value of count is incremented each time a word contains the character "a". There are four elements of wordList that contain an "a", so 4 is printed.
Q25 manipulate method and animals List
Answer E is incorrect. List is an interface, which an ArrayList implements. Please note that List is no longer tested as part of the AP CSA exam and ArrayList will be used instead. This would be the result if when words that started with “b” were found, they were added to the end instead of inserted at index animals.size() – k such as if the statement in the for loop was animals.add(animals.remove(k));. Answer B is correct. List is an interface, which an ArrayList implements. Please note that List is no longer tested as part of the AP CSA exam and ArrayList will be used instead. The manipulate method contains a for loop with a loop control variable k that starts at the right most index of animals, decrements by 1 each time, until k is equal to 0. In the first iteration, when k is 5, if the element of animals at 5 (“baboon”) starts with a “b”, which it does, then this value is removed from the list and inserted at index 1. The list would then be {“bear”, “baboon”, “zebra”, “bass”, “cat”, “koala”}.
Q26 mystery method with String parameter and substring
Answer D is incorrect. The reverse of “nono” is “onon”, since these two strings are not equal, the method will return false. Answer E is correct. This algorithm assigns temp the letters of str in reverse by extracting each letter of str starting at the end and moving to the beginning. Each letter is appended to the end of temp. So, if str was “abc”, “c” would be appended followed by “b”, and then “a”, making temp equal to “cba”. This method will return true if str is equal to temp, which is a string with the letters of str in reverse. The string “noon” is the only string that is the same in reverse.
Q41 remove some ArrayList elements
Answer D is incorrect. The expression num % key == 0 is correctly used to identify values of num that are divisible by key. Answer E is correct. When the element at position i is removed from numList, subsequent elements are shifted left. After the removal, the element that used to be at position i + 1 is now at position i. Because the method increments i regardless of whether the element at position i was removed, the method does not always work as intended. For example, if two adjacent elements are both divisible by key, only the first element is removed. The method could be corrected by incrementing i only when the element at position i is not removed or by decrementing i when an element is removed.