ComSci Chapter 7 Study Guide:
Consider the following code segment. ArrayList<String> arrList = new ArrayList<String>(); arrList.add("A"); arrList.add("B"); arrList.add("C"); arrList.add("D"); for (int i = 0; i < arrList.size(); i++) { System.out.print(arrList.remove(i)); } What, if anything, is printed as a result of executing the code segment? A) AC B) BD C) ABC D) ABCD E) Nothing is printed.
A) AC
In the code segment below, numList is an ArrayList of integers that is sorted in descending order. The code segment is intended to insert the integer value val into numList so that numList is still sorted in descending order. int j = 0; while (val != numList.get(j)) { j++; } numList.add(j, val); The code segment does not always work as intended. Assuming that numList has been initialized to {3, 2, 1, 0}, for which value of val does the code segment NOT produce the expected result? A) 4 B) 3 C) 2 D) 1 E) 0
A) 4
public static int sequentialSearch(ArrayList<Integer> elements, int target) { for (int j = 0; j < elements.size(); j++) // Line 3 { if (elements.get(j) == target) { return j; } } return -1; } Which of the following explains how replacing Line 3 with for (int j = (elements.size() - 1); j >= 0; j--) will affect the behavior of sequentialSearch? A) The modification has no effect: the modified method will continue to return the index of the first appearance of the target value in the list, or -1 if the target value does not appear in the list. B) The modified method will return the index of the last appearance of the target value in the list, or -1 if the target value does not appear in the list. C) The modified method will throw an IndexOutOfBoundsException. D) The modified method will return -1 regardless of the inputs. E) The modified method will not compile.
B) The modified method will return the index of the last appearance of the target value in the list, or -1 if the target value does not appear in the list.
Consider the following code segment. ArrayList<String> syllables = new ArrayList<String>(); syllables.add("LA"); syllables.add(0, "DI"); syllables.set(1, "TU"); syllables.add("DA"); syllables.add(2, syllables.get(0)); syllables.remove(1); System.out.println(syllables.toString()); What is printed as a result of executing the code segment? A) [DI, DA, DI] B) [DI, DI, DA] C) [LA, LA, DA] D) [TU, DI, DA] E) [TU, TU, DA]
B) [DI, DI, DA]
Consider the following code segment. ArrayList<String> words = new ArrayList<String>(); words.add("mat"); words.add("new"); words.add("open"); words.add("pet"); int i = 0; while (i < words.size()) { words.remove(i); i++; } System.out.println(words.toString()); What is printed when the code segment is executed? A) [ ] B) [new, pet] C) [open, pet] D) [new, open, pet] E) [mat, new, open, pet]
B) [new, pet]
Consider the following statement, which is intended to create an ArrayList named a to store only elements of type Thing. Assume that the Thing class has been properly defined and includes a no-parameter constructor. ArrayList<Thing> a = /* missing code */; Which of the following can be used to replace /* missing code */ so that the statement works as intended? A) new Thing() B) new ArrayList<Thing>() C) new ArrayList(Thing) D) new ArrayList(<Thing>) E) new ArrayList<>(Thing)
B) new ArrayList<Thing>()
Consider the following statement, which is intended to create an ArrayList named numbers that can be used to store Integer values. ArrayList<Integer> numbers = /* missing code */; Which of the following can be used to replace /* missing code */ so that the statement works as intended? I. new Integer() II. new ArrayList<Integer> III. new ArrayList<Integer>() A) I and III only B) I and II only C) III only D) II and III only E) I, II, and III
C) III only
Consider the following search method. public static int search(int[] arr, int target) { int result = -1; for (int j = 0; j < arr.length; j++) { if (arr[j] == target) { result = j; // Line 8 } } return result; } Which of the following describes the effect of replacing the statement in line 8 of the method with result = arr[j]; ? A) The modified method will return the index of the first occurrence of target in arr. B) The modified method will return the index of the last occurrence of target in arr. C) The modified method will return target if target appears in arr and will return -1 otherwise. D) The modified method will return -1 if target appears in arr and will return target otherwise. E) The modified method will return -1 for all possible inputs.
C) The modified method will return target if target appears in arr and will return -1 otherwise.
Consider the following method definition. The method isReversed is intended to return true if firstList and secondList contain the same elements but in reverse order, and to return false otherwise. public static boolean isReversed(ArrayList<Integer> firstList, ArrayList<Integer> secondList) { for (int j = 0; j < firstList.size() / 2; j++) { if (firstList.get(j) != secondList.get(secondList.size() - 1 - j)) { return false; } } return true; } The method does not always work as intended. For which of the following inputs does the method NOT return the correct value? A When firstList is {1, 3, 3, 1} and secondList is {1, 3, 3, 1} B) When firstList is {1, 3, 3, 1} and secondList is {3, 1, 1, 3} C) When firstList is {1, 3, 5, 7} and secondList is {5, 5, 3, 1} D) When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 1} E) When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 3}
C) When firstList is {1, 3, 5, 7} and secondList is {5, 5, 3, 1}
In the code segment below, myList is an ArrayList of integers. The code segment is intended to remove all elements with the value 0 from myList. int j = 0; while (j < myList.size()) { if (myList.get(j) == 0) { myList.remove(j); } j++; } The code segment does not always work as intended. For which of the following lists does the code segment NOT produce the correct result? A) {0, 1, 2, 3} B) {0, 1, 0, 2} C) {1, 0, 0, 2} D) {1, 2, 3, 0} E) {1, 2, 3, 4}
C) {1, 0, 0, 2}
Consider the following code segment. ArrayList<Integer> myList = new ArrayList<Integer>(); for (int i = 0; i < 4; i++) { myList.add(i + 1); } for (int i = 0; i < 4; i++) { if (i % 2 == 0) { System.out.print(myList.get(i) + " "); } } What output is produced as a result of executing the code segment? A) 0 1 2 3 B) 1 2 3 4 C) 0 2 D) 1 3 E) 2 4
D) 1 3
Consider the following code segment. ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(3); nums.add(2); nums.add(1); nums.add(0); nums.add(0, 4); nums.set(3, 2); nums.remove(3); nums.add(2, 0); Which of the following represents the contents of nums after the code segment has been executed? A) [2, 4, 3, 2, 0] B) [3, 2, 0, 1, 0] C) [4, 2, 0, 2, 0] D) [4, 3, 0, 2, 0] E) [4, 3, 0, 3, 0]
D) [4, 3, 0, 2, 0]
Consider the following statement, which is intended to create an ArrayList named arrList to store elements only of type String. /* missing code */ = new ArrayList<String>(); Which of the following can be used to replace /* missing code */ so that the statement works as intended? A) ArrayList arrList() B) ArrayList arrList C) ArrayList<> arrList D) ArrayList arrList<String> E) ArrayList<String> arrList
E) ArrayList<String> arrList
Consider the following code segment. ArrayList<Integer> vals = new ArrayList<Integer>(); vals.add(vals.size(), vals.size()); vals.add(vals.size() - 1, vals.size() + 1); vals.add(vals.size() - 2, vals.size() + 2); System.out.println(vals.toString()); What is printed as a result of executing the code segment? A) [0, 1, 2] B) [0, 2, 4] C) [1, 2, 3] D) [2, 1, 0] E) [4, 2, 0]
E) [4, 2, 0]
public class UserName { // The list of possible user names, based on a user's first & last names & initialized by the constructor. private ArrayList<String> possibleNames; public UserName(String firstName, String lastName) { /* to be implemented in part (a) */ } /** Returns true if arr contains name, & false otherwise. */ public boolean isUsed(String name, String[] arr) { /* implementation not shown */ } /** Removes strings from possibleNames that are found in usedNames as described in part (b). */ (a) Write the constructor for the UserName class. The constructor initializes & fills possibleNames with possible user names based on the firstName and lastName parameters. The possible user names are obtained by concatenating lastName with different substrings of firstName. The substrings begin with the first character of firstName and the lengths of the substrings take on all values from 1 to the length of firstNam
public UserName(String firstName, String lastName) { possibleNames = new ArrayList<String>( ); for(int i = 0; i < firstName.length( ); i++); { possibleNames.add(lastName + firstName.substring(0, i + 1) ); } }