8.11. Multiple-Choice Exercises¶
8.11.1. Easier Multiple Choice Questions¶
- nums.length
- You can't use length on lists and the last index is one less than the size.
- nums.length - 1
- You can't use length on lists, use size instead.
- nums.size()
- Since the first element in a list is at index 0 the last element is at the size minus 1.
- nums.size() - 1
- The last element is at the size of the list minus 1.
8-11-1: Which index is the last element in a list called nums
at?
- An array has faster access to its elements than a list does.
- Since an ArrayList is implemented by an array, it has the same access time.
- An array knows it length, but a list doesn't know its length.
- Lists do know their length, but they don't make it public.
- An ArrayList can allocate more space than it needs.
- Every time an ArrayList fills up a new array is created that is twice as big. This can lead to extra space that is wasted.
8-11-2: Which of the following is a reason to use an array instead of an ArrayList?
- An ArrayList can grow or shrink as needed, while an array is always the same size.
- This is the main advantage to an ArrayList.
- You can use a for-each loop on an ArrayList, but not in an array.
- You can use a for-each loop on either an ArrayList or array.
- You can store objects in an ArrayList, but not in an array.
- Arrays can also store objects of the same type.
8-11-3: Which of the following is a reason to use an ArrayList instead of an array?
- nums[0]
- This is how you get the first value in an array, but not in a list.
- nums[1]
- This is how you get the second value in an array. Remember that this is a list and that the first item in an array is at index 0.
- nums.first()
- The
List
doesn't have afirst
method. - nums.get(0)
- Use the
get
method to get a value from a list and the first element in a list is at index 0. - nums.get(1)
- This would return the second element in a list. Remember that the first element in a list or array is at index 0.
8-11-4: Which of the following is the correct way to get the first value in a list called nums
?
- nums[1] = 5;
- This is how you set the second value in an array, but not in a list.
- nums[2] = 5;
- This is how you set the third value in an array, but not in a list.
- nums.set(5, 1);
- This would the value at index 5 to 1.
- nums.set(1, 5);
- This sets the second value in the list to 5.
- nums.set(2, 5);
- This would set the third value in the list to 5. Remember that the first value is at index 0.
8-11-5: Which of the following is the correct way to set the second value in a list called nums
to 5?
- nums.remove(3);
- This would remove the value at index 3 which is 1.
- nums.remove(0);
- This would remove the value at index 0 which is 5.
- nums.remove(1);
- This would remove the value at index 1 which is 3.
- nums.remove(2);
- This would remove the value at index 2 which is 2.
8-11-6: Which of the following is the correct way to remove the value 3 from the list nums = [5, 3, 2, 1]
?
- nums.add(2, 0);
- This would add 0 at index 2. Remember that the method is
add(index, obj)
. - nums.add(2, 1);
- This would add 1 at index 2. Remember that the method is
add(index, obj)
- nums.add(0, 2);
- This would add 2 at index 0 which would result in
[2, 1, 3, 4]
- nums.add(1, 2);
- This would add 2 at index 1 which would result in
[1, 2, 3, 4]
- nums.add(2, 2);
- This would add 2 at index 2 which would result in
[1, 3, 2, 4]
8-11-7: Which of the following is the correct way to add 2 between the 1 and 3 in the following list nums = [1, 3, 4]
?
- [2, 3]
- This would be true if it was
remove(0)
- [1, 2, 3]
- The
remove
will remove a value from the list, so this can't be correct. - [1, 2]
- This would be true if it was
remove(2)
- [1, 3]
- This removes the value at index 1 which is 2.
8-11-8: What will print when the following code executes?
List<Integer> list1 = new ArrayList<Integer>();
list1.add(new Integer(1));
list1.add(new Integer(2));
list1.add(new Integer(3));
list1.remove(1);
System.out.println(list1);
- ["Sarah", "Destini", "Layla", "Sharrie"]
- The list is first ["Anaya", "Layla", "Sharrie"] and then ["Destini, "Layla", "Sharrie"] and finally ["Sarah", "Destini, "Layla", "Sharrie"]
- ["Sarah", "Destini", "Anaya", "Layla", "Sharrie"]
- The set replaces the value at index 0.
- ["Sarah", "Layla", "Sharrie"]
- This would be true if the second add was a set.
- ["Destini", "Layla", "Sharrie", "Sarah"]
- This would be true if the last add didn't have an index of 0.
8-11-9: What will print when the following code executes?
List<String> list1 = new ArrayList<String>();
list1.add("Anaya");
list1.add("Layla");
list1.add("Sharrie");
list1.set(0, "Destini");
list1.add(0, "Sarah");
System.out.println(list1);
8.11.2. Easier Search/Sort Multiple Choice Questions¶
- -1
- This value is returned if the target is not in the list since this is a sequential search.
- 0
- This would be true if the target was 90 since this is a sequential search.
- 1
- This would be true if the target was -30 since this is a sequential search.
- 2
- This is a sequential search that returns the index where the target appears in the elements list
- 50
- A sequential search returns the index, not the value. What is the index of the 50?
8-11-10: What would the following code return from mystery([90, -30, 50], 50)?
public static int mystery(int[] elements, int target)
{
for (int j = 0; j < elements.length; j++)
{
if (elements[j] == target)
{
return j;
}
}
return -1;
}
- -1
- A sequential search returns -1 if the target value is not found in the list.
- 0
- This would be true if the target was 90 since this is a sequential search.
- 1
- This would be true if the target was -30 since this is a sequential search.
- 2
- This would be true if the target was
- -20
- A sequential search returns negative one when the value isn't found in the list.
8-11-11: What would the following code return from mystery([90, -30, 50], -20)?
public static int mystery(int[] elements, int target)
{
for (int j = 0; j < elements.length; j++)
{
if (elements[j] == target)
{
return j;
}
}
return -1;
}
- 1
- This would be true if we were looking for 23.
- 2
- It first compares 23 at index 2 (5 / 2 is 2) to 2. The second time it compares the 2 at index 0 (1 / 2 = 0) to 2 and returns 0.
- 3
- This would be true if we were looking for 10.
8-11-12: Consider the binarySearch
method below. How many times would the while loop execute if you first do int[] arr = {2, 10, 23, 31, 55, 86} and then call binarySearch(arr,2)?
public static int binarySearch(int[] elements, int target) {
int left = 0;
int right = elements.length - 1;
while (left <= right)
{
int middle = (left + right) / 2;
if (target < elements[middle])
{
right = middle - 1;
}
else if (target > elements[middle])
{
left = middle + 1;
}
else {
return middle;
}
}
return -1;
}
- selection sort
- A selection sort has nested for loops.
- insertion sort
- An insertion sort has a while loop inside a for loop.
- merge sort
- A merge sort has a recursive call to mergeSortHelper in mergeSortHelper.
8-11-13: Which sort contains a recursive call?
- If the data is already sorted in ascending order
- If the data is already sorted in the correct order you don't need to move any values.
- If the data is already sorted in descending order
- All values will have to be moved multiple times since the data was sorted into descending order.
- It will always take the same amount of time to execute
- This would be true if it was a selection sort.
8-11-14: Under what condition will an ascending insertion sort execute the slowest?
8.11.3. Medium Multiple Choice Questions¶
- [1, 2, 3, 4, 5]
- The set replaces the 3 at index 2 with the 4 so this can't be right.
- [1, 2, 4, 5, 6]
- The add with an index of 2 and a value of 5 adds the 5 at index 2 not 3. Remember that the first index is 0.
- [1, 2, 5, 4, 6]
- The add method that takes just an object as a parameter adds that object to the end of the list. The set replaces the value at that index with the new value. The add with parameters of an index and an object puts the passed object at that index and moves any existing values by one index to the right (increments the index).
- [1, 5, 2, 4, 6]
- The add with an index of 2 and a value of 5 adds the 5 at index 2 not 1. Remember that the first index is 0.
8-11-15: What is printed as a result of executing the following code segment?
List<Integer> list1 = new ArrayList<Integer>();
list1.add(new Integer(1));
list1.add(new Integer(2));
list1.add(new Integer(3));
list1.set(2, new Integer(4));
list1.add(2, new Integer(5));
list1.add(new Integer(6));
System.out.println(list1);
- [0, 4, 2, 5, 3]
- This code will loop through the array list and if the current value at the current index (k) is 0 it will remove it. When you remove a value from an array list it moves all values to the right of that one to the the left. It only increments the index when it doesn't find a zero so it work work correctly.
- [3, 5, 2, 4, 0, 0, 0]
- This shows all zeros at the end and this code removes 0's so this can't be right.
- [0, 0, 0, 4, 2, 5, 3]
- This shows all zeros at the beginning and this code removes zeros so this can't be right.
- [4, 2, 5, 3]
- This shows all zeros removed. Since k is only incremented if a value wasn't removed this will work correctly.
- [0, 0, 4, 2, 5, 0, 3]
- This shows the original values, but this code does remove some zeros so this can't be right.
8-11-16: Given the following code and assume that nums
initially contains [0, 0, 4, 2, 5, 0, 3], what will nums
contain as a result of executing numQuest?
private List<Integer> nums;
// precondition: nums.size() > 0;
// nums contains Integer objects
public void numQuest()
{
int k = 0;
Integer zero = new Integer(0);
while (k < nums.size())
{
if (nums.get(k).equals(zero))
nums.remove(k);
else
k++;
}
}
- Both methods produce the same result, and process1 is faster than process2.
- In this case they do the same thing. The only difference would be if there were values in the list in process2.
- The two methods produce different results and take the same amount of time.
- These produce the same result on an empty list when you add to the end.
- The two methods produce different results, and process1 is faster than process2.
- These produce the same result on an empty list when you add to the end.
- The two methods produce different results, and process2 is faster than process1.
- These produce the same result on an empty list when you add to the end.
- Both methods produce the same result and take the same amount of time.
- The method process1 adds to the end of the list each time through the loop. The method process2 also adds to the end of the list each time through the loop. The only difference would be if there were values in the list in process2. Any existing values would be moved to the right. But, there are no existing values in the list at that index or beyond.
8-11-17: Which of the following best describes the behavior of process1 and process2 (shown below)?
public static List<Integer> process1(int n)
{
List<Integer> someList = new ArrayList<Integer>();
for (int k = 0; k < n; k++)
someList.add(k);
return someList;
}
public static List<Integer> process2(int n)
{
List<Integer> someList = new ArrayList<Integer>();
for (int k = 0; k < n; k++)
someList.add(k, k);
return someList;
}
- [1, 2, 5, 4, 6, 3]
- The set replaces the 3 with the 4 so this can't be right.
- [6, 5, 4, 3, 2, 1]
- The add with an index of 2 and a value of 5 adds the 5 at index 2 not 3. Remember that the first index is 0.
- [1, 2, 3, 4, 5, 6]
- The add method that takes just a value as a parameter adds that value to the end of the list. The set replaces the value at that index with the new value. The add with parameters of an index and a value puts the passed value at that index and moves any existing values by one index to the right (increments the index).
- [1, 4, 2, 6, 3]
- The add with an index of 2 and a value of 5 adds the 5 at index 2 not 1. Remember that the first index is 0.
- [1, 2, 4, 6, 3]
- When you declare and create a collection class you can specify the type of the items in it.
8-11-18: What is printed as a result of executing the following code segment?
List<Integer> aList = new ArrayList<Integer>();
aList.add(new Integer(1));
aList.add(new Integer(2));
aList.add(1, new Integer(5));
aList.set(1, new Integer(4));
aList.add(new Integer(6));
aList.add(new Integer(3));
System.out.println(aList);
- [1, 2, 3, 4, 5]
- This would be true if the code just added each integer at the end of the list. But, that is not what it does.
- [1, 4, 5]
- The list is [1], then [1, 2], then [1], then [1, 3], then [1, 4], then [1, 4, 5].
- [1, 4, 3, 5]
- This would be true if the
set
was an add. - [2, 4, 5]
- This would be true it it was
remove(0)
. Remember that it removes the object at the given index. - [2, 4, 3, 5]
- This would be true if the
set
was an add and if it wasremove(0)
.
8-11-19: What is printed as a result of executing the following code segment?
List<Integer> aList = new ArrayList<Integer>();
aList.add(new Integer(1));
aList.add(new Integer(2));
aList.remove(1);
aList.add(1, new Integer(3));
aList.set(1, new Integer(4));
aList.add(new Integer(5));
System.out.println(list);
- [c, d, e, b]
- What happened to the f?
- [c, d, e, b, f]
- This list is [a], then [a, b], then [c, a, b], then [c, d, a, b], then [c, d, e, b], then [c, d, e, b, f]
- [c, a, e, b, f]
- The a is pushed to position 2 and then replaced with the e.
- [c, d, e, a, b, f]
- This would be true if it was
list1.add(2,"e")
- [c, a, e, d, b, f]
- Remember that the set will replace the value at index 2.
8-11-20: What is printed as a result of executing the following code segment?
List<String> list1 = new ArrayList<String>();
list1.add("a");
list1.add("b");
list1.add(0,"c");
list1.add(1, "d");
list1.set(2, "e");
list1.add("f");
System.out.println(list1);
What is printed as a result of executing the following code segment?
- [2, 3, 4, 5]
- This would be true if it removed the first 4 but it removes the value at index 4.
- [2, 3, 5]
- This would be true if it removed all the 4 values, but it removes the value at index 4.
- [4, 2, 3, 5]
- This would be true if it removed the value at index 3.
- [4, 2, 3, 4]
- This removes the value at index 4 which is 5.
8-11-21: Given the list nums = [4, 2, 3, 4, 5]
what is the result after executing nums.remove(4)
?
- [e, d, b]
- This would be true if you couldn't add a duplicate object to a list, but you can.
- [e, d, b, b]
- The list is [a], [a, b], [c, a, b], [c, d, b], [e, d, b], and then [e, d, b, b]
- [e, d, a, b, b]
- This would be true it
list1.set(1,"d");
waslist1.add(1,"d");
- [e, d, a, b]
- This would be true it
list1.set(1,"d");
waslist1.add(1,"d");
and if lists didn't allow duplicate objects.
8-11-22: What is printed as a result of executing the following code segment?
List<String> list1 = new ArrayList<String>();
list1.add("a");
list1.add("b");
list1.add(0,"c");
list1.set(1, "d");
list1.set(0, "e");
list1.add("b");
System.out.println(list1);
What is printed as a result of executing the following code segment?
- [4, 3, 2, 1, 0]
- This would be true if it was
numList.add(numList.size() - i, obj)
- [1, 2, 3, 4, 0]
- This would be true if it was
mystery(1)
- [0, 1, 2, 3, 4]
- Each value is removed one at a time and added to the end of the list which results in the same list.
- [2, 3, 4, 0, 1]
- This would be true if it was
mystery(2)
- [4, 0, 1, 2, 3]
- This would be true if it was
mystery(4)
8-11-23: Assume that numList
has been initialized with the following Integer objects: [0, 1, 2, 3, 4]. What is the value of numList
after mystery(5)
executes?
private List<Integer> numList;
public void mystery(int n)
{
for (int i = 0; i < n; i++)
{
Integer obj = numList.remove(0);
numList.add(obj);
}
}
- [5, 7, 8, 12]
- What about the 11?
- [5, 7, 8, 11, 12]
- This will add the value at the correct location in a list in ascending order.
- [11, 5, 7, 8, 12]
- This would be true if it was
numList.add(0, value)
- [5, 7, 8, 12, 11]
- This would be true if the while loop was from 0 to one less than the size of the list.
- [5, 7, 11, 8, 12]
- This would be true if it was
numList.add(i-1, value)
8-11-24: Assume that numList
has been initialized with the following Integer objects: [5, 7, 8, 12]. Which of the following shows the values in numList
after a call to mystery(11)
?
private List<Integer> numList;
public void mystery(int value)
{
int i = 0;
while (i < numList.size() && numList.get(i) < value)
{
i++;
}
numList.add(i, value);
}
8.11.4. Medium Search/Sort Multiple Choice Questions¶
- {3,7,8,5,2}, {3,7,8,5,2}, {3,5,7,8,2}, {2,3,5,7,8}
- The insertion sort starts at index 1 and inserts each value into the sorted list to the left by moving any larger values right.
- {2,3,8,5,7}, {2,3,8,5,7}, {2,3,5,8,7}, {2,3,5,7,8}
- This would be true if it was a selection sort.
- {3,7,8,5,2}, {3,5,7,8,2}, {2,3,5,7,8}
- This looks like an insertion sort, but it is missing one step.
- {2,3,8,5,7}, {2,3,5,8,7}, {2,3,5,7,8}
- This looks like a selection sort, but it is missing one step.
- {2,7,3,8,5}, {2,3,7,8,5}, {2,3,5,7,8}
- This is more like a selection sort, but not a correct one.
8-11-25: Which of the following correctly shows the iterations of an ascending (from left to right) insertion sort on an array with the following elements: {7,3,8,5,2}?
- -1
- This would be true if the third value was something that wasn't in the array.
- 0
- This would be true if the third value was 1
- 1
- This is a binary search and it returns the index of the value 3, which is 1.
- 2
- This would be true if the third value was 5.
- 3
- This would be true if the third value was 8.
8-11-26: What is printed when the following main method is executed?
public class Searcher
{
private int[] arr = {1,3,5,8,9};
public int mystery(int low, int high, int num)
{
int mid = (low + high) / 2;
if (low > high) {
return -1; }
else if (arr[mid] < num) {
return mystery(mid + 1, high, num); }
else if (arr[mid] > num) {
return mystery(low, mid - 1, num); }
else
return mid;
}
public static void main(String[] args)
{
Searcher s = new Searcher();
System.out.println(s.mystery(0,4,3));
}
}
- {6,10,3,2,8}, {3,6,10,2,8}, {2,3,6,10,8}, {2,3,6,8,10}
- This would be true if it was an insertion sort.
- {6,10,3,2,8}, {3,6,10,2,8}, {2,3,6,8,10}
- This would be true if it was an insertion sort, but you are also missing a step.
- {2,6,3,10,8}, {2,3,6,10,8}, {2,3,6,8,10}
- This is almost right, but is missing one step.
- {2,6,3,10,8}, {2,3,6,10,8}, {2,3,6,10,8}, {2,3,6,8,10}
- This is the result from a selection sort.
8-11-27: Which of the following correctly shows the iterations of an ascending (from left to right) selection sort on an array with the following elements: {10, 6, 3, 2, 8}?
- int k = j - 1; k >= 0; k--
- The inner loop starts at the outer loop value plus one, not minus one.
- int k = j + 1; k < elem.length; k++
- The inner loop starts at the outer loop value plus one and ends at the last element.
- int k = j; k < elem.length; k++
- The inner loop should start at the outer loop value plus one.
- int k = j; k >= 0; k--
- The inner loop should start at the outer loop value plus one and increment.
- int k = j - 1; k > 0; k--
- The inner loop should start at the outer loop value plus one and increment.
8-11-28: Which of the following could be used to replace // missing code // in the code so that the method always sorts the array elem
in ascending order?
public class Searcher
{
public static void sort(int[] elem)
{
for (int j = 0; j < elem.length - 1; j++)
{
int minIndex = j;
for (// missing code //)
{
if (elem [k] < elem [minIndex])
{
minIndex = k;
}
}
int temp = elem[j];
elem[j] = elem[minIndex];
elem[minIndex] = temp;
}
}
public static void main(String[] args)
{
int[] nums = {28, -3, 2, 14, 30};
Searcher.sort(nums);
}
}
- -1
- This would be true if the sequential search code was okay and v was a value that wasn't in the array, but the code is incorrect. The
return -1
should be outside of the for loop. - 0
- This would be true if v was 1 and the code was correct for a sequential search.
- 1
- This would be true if v was 2 and the code was correct for a sequential search.
- 2
- This would be true if the code was correct for a sequential search, but it returns -1 inside the for loop instead of outside of it.
- The code will not compile
- This method won't compile because it is supposed to return an integer and if the for loop doesn't execute it will not return anything. The
return -1
should be outside the for loop to make this sequential search work as intended.
8-11-29: What would test return if a = {1,2,3,4} and v = 3?
public static int test(int[] a, int v)
{
for (int i = 0; i < a.length; i++)
{
if (a[i] == v)
return i;
else return -1;
}
}
8.11.5. Hard Multiple Choice Questions¶
- [5, 3, 1, 6]
- The remove(1) removes the item at index 1 which will be 5 after the 4 is added at index 0.
- [4, 3, 1, 6]
- The add(6) adds the 6 at the end of the list. The add(0,4) will add 4 at index 0. The remove(1) removes the 5 at index 1.
- [4, 3, 6]
- The remove(1) doesn't remove the 1, it removes the value at index 1.
- [5, 3, 6]
- The 5 will be removed with the remove(1).
- [4, 5, 3, 6]
- This would be true if remove(1) removed the item with that value, but it removes the item at that index.
8-11-30: What is in the list nums
if it initially contained {5, 3, 1} and the following code is executed?
nums.add(6);
nums.add(0,4);
nums.remove(1);
- [0, 0, 4, 2, 5, 0, 3, 0]
- This shows the original values but this code does remove some zeros so this can't be right.
- [3, 5, 2, 4, 0, 0, 0, 0]
- This shows all zeros at the end, but this code removes 0's so this can't be right.
- [0, 0, 0, 0, 4, 2, 5, 3]
- This shows all zeros at the beginning, but this code removes zeros so this can't be right.
- [4, 2, 5, 3]
- This shows all zeros removed. This would be correct if k was only incremented if a value wasn't removed.
- [0, 4, 2, 5, 3]
- This code will loop through the array list and if the current value at the current index (k) is 0, it will remove it. When you remove a value from an array list, it moves all values to the right of that down one. So the first 0 will be deleted but the second one will not since k is incremented even if you remove something. You should only increment k if you didn't remove something and then you would remove all 0's from the list.
8-11-31: Assume that nums has been created as an ArrayList object and initially contains the following Integer values: [0, 0, 4, 2, 5, 0, 3, 0]. What will nums contain as a result of executing the following method numQuest?
private List<Integer> nums;
//precondition: nums.size() > 0
//nums contains Integer objects
public void numQuest() {
int k = 0;
Integer zero = new Integer(0);
while (k < nums.size()) {
if (nums.get(k).equals(zero))
nums.remove(k);
k++;
}
}
8.11.6. Hard Search/Sort Multiple Choice Questions¶
- A B B C D
- This would be true if the for loop inside the main method did not interate through every value in the array.
- E D C B B A
- This would be true if the conditional statement inside the for loop stated "if (key.compareTo(letters[i]) < 0)", because that would put the array in a reverse alphabetical order.
- A B B C D E
- This is an insertion sort which sorts the array in alphabetical order using the compareTo() method.
- E D C B A B
- This would be true if array was not modified at all in the main method.
- E D C B B
- This would be true if the conditional statement inside the for loop stated "if (key.compareTo(letters[i]) < 0)" and if the loop did not iterate through every item of the letters array, because that would put the array in a reverse alphabetical order.
8-11-32: What is printed when the following main method is executed? The break; statement used in this code breaks out of or terminates the loop at that point. It is not used on the AP CS A exam.
public class AlphaSort
{
public static void main(String[] args)
{
int i, j;
String key;
String[] letters = {"E","D","C","B","A","B"};
for (j = 1; j < letters.length; j++)
{
key = letters[j];
i = j - 1;
while (i >= 0)
{
if (key.compareTo(letters[i]) > 0)
{
break;
}
letters[i + 1] = letters[i];
i--;
}
letters[i + 1] = key;
}
for (int t = 0; t < letters.length; t++)
{
System.out.print((letters[t]) + "");
}
}
}
- 4
- This would be true if the if statement was not trying to check if the numbers in the array were negative and odd.
- 2
- This answer is correct because the for loop iterates through every element and increments the count if the current number is negative and odd.
- 12
- This may be a result of misunderstanding the question, as 12 cannot be an answer because the array length itself is only 6.
- 1
- This would be true if the code was looking for the numbers in the array that were positive and odd.
8-11-33: What is printed when the following main method is executed?
public class NumberCount
{
public static void main(String[] args)
{
int count = 0;
int[] numbers = {-5,4,-5,3,-2,-4};
for (int j = 0; j < numbers.length; j++)
{
if(numbers[j] < 0 && numbers[j] % 2 != 0)
{
count++;
}
}
System.out.println(count);
}
}
You can step through the code above by clicking on the following link Ex-12-8-2.
- -3
- This would be true if there were three strings in the array that had the same first letter as the last letter.
- -4
- This would be true if there were four strings in the array that had the same first letter as the last letter.
- 4
- This would be true if there had been four strings in the array that had the first letter as an A and those strings' last letter was not an A.
- 0
- This is the correct answer. The for loop is iterating through every element in the guestList array and the first if statement is checking to see if the current element in the array starts with the same letter and ends with the same letter. The variable, count decreases by one if that is true. However if that is false, the program goes to the else if statment and checks to see if the first letter is an A. If that is true count increases by one.
8-11-34: What is printed when the following main method is executed?
public class GuestList
{
public static void main(String[] args)
{
int count = 0;
String[] guestList = {"Anna", "Briana", "Alex", "John"};
String subj1 = null;
String subj2 = null;
for (int j = 0; j < guestList.length; j++)
{
subj1 = guestList[j].substring(0,1);
subj2 = guestList[j].substring(guestList[j].length()-1);
if(subj1.equalsIgnoreCase(subj2))
{
count--;
}
else if(subj1.equalsIgnoreCase("a"))
{
count++;
}
}
System.out.println(count);
}
}
You can step through the code above by clicking on the following link Ex-12-8-3.
- 8,7,7,3,4,1
- This would be true if the array was not modified at all.
- 4,7,7,3,8,1
- This is the correct answer. The for loop is iterating through every element in the array. The if statement is checking to see if the current element is even or odd. If it is even, then the first element of the array and the current element will swap places in the array.
- 4,8,7,1,3,7
- This would be true if the loop had brought all the even numbers to the beginning of the array.
- 1,8,7,7,4,3
- This would be true if the if statement had said: if(arr[i] % 2 == 1).
8-11-35: What is printed when the following main method is executed?
public class OddEvenMod
{
public static void main(String[] args)
{
int[] arr = {8,7,7,3,4,1};
for (int i = 0; i < arr.length; i++)
{
if(arr[i] % 2 == 0)
{
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
}
}
for (int t = 0; t < arr.length; t++)
{
System.out.print((arr[t]) + ",");
}
}
}
You can step through the code above by clicking on the following link Ex-12-8-4.
- 3,5,3,9,2,4,
- This is the correct answer. The check method is using a for loop and an if statement to return true if the parameter is prime and false if it is not prime. In the main method, the for loop iterates through every element in the array and checks to see if it is prime. If it is prime, then the program will swap that element with the first element in the array.
- 4,5,2,3,9,3,
- This would be true if the if statement had said: if(!check(arr[i])).
- 5,3,2,9,3,4,
- This would be true if the array had not been modified at all.
- 2,3,5,9,3,
- This would be true if the final for loop did not iterate through every element in the array.
8-11-36: What is printed when the following main method is executed?
public class PrimeOrNot
{
private static boolean check(int n)
{
for(int i = 2; i < n; i++)
{
if(n % i == 0)
return false;
}
return true;
}
public static void main(String[] args)
{
int[] arr = {5,3,2,9,3,4};
for (int i = 0; i < arr.length; i++)
{
if(check(arr[i]))
{
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
}
}
for (int t = 0; t < arr.length; t++)
{
System.out.print((arr[t]) + ",");
}
}
}
You can step through the code above by clicking on the following link Ex-12-8-5.
- Anna John Billy Bob Roger Dominic
- This would be true if the program did not modify the names array at all.
- John Dominic Anna Roger Bob Billy
- This is the correct answer. The program is ordering the grades array from greatest to least as well as keeping the names with the grades.
- Billy Bob Roger Anna Dominic John
- This would be true if the program sorted the grades array from the smallest value to the largest value.
- Anna John Billy Bob Roger
- This would be true if the program did not modify the names array and if the for loop at the end of the program did not output all the values of the array.
8-11-37: What is printed when the following main method is executed?
public class GradeSort
{
public static void main(String[] args)
{
String[] names = {"Anna","John","Billy","Bob","Roger","Dominic"};
int[] grades = {93,100,67,84,86, 93};
int i, j, first, temp;
String temp2;
for (i = grades.length - 1; i > 0; i--)
{
first = 0;
for (j = 1; j <= i; j++)
{
if (grades[j] < grades[first])
first = j;
}
temp = grades[first];
grades[first] = grades[i];
grades[i] = temp;
temp2 = names[first];
names[first] = names[i];
names[i] = temp2;
}
for (int t = 0; t < names.length; t++)
{
System.out.print((names[t]) + " ");
}
}
}
You can step through the code above by clicking on the following link Ex-12-8-6.
- 6 7 17 3 2 9 1 5
- This would be true if the program had not modified the array at all.
- 9 6 3 2 3 1 5 17
- This would be true if the loop was moving the position of odd numbers in the array to arr.length-1.
- 5 1 2 3 6 17 7 9
- This would be true if the array was printed in the reversed order.
- 9 7 17 6 3 2 1 5
- This is the correct answer, because the divCheck method is checking to see if the values in the array are divisible by 2 or 3. If they are, they are swapped with the value at the first position (index 0).
8-11-38: What is printed when the following main method is executed?
public class DivisibleBy2or3
{
private static boolean divCheck(int n)
{
if(n % 2 == 0 || n % 3 == 0)
{
return true;
}
return false;
}
public static void main(String[] args)
{
int[] arr = {6,7,17,3,2,9,1,5};
for (int i = 0; i < arr.length; i++)
{
if(divCheck(arr[i]))
{
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
}
}
for (int t = 0; t < arr.length; t++)
{
System.out.print((arr[t]) + " ");
}
}
}
You can step through the code above by clicking on the following link Ex-12-8-7.