Midterm Sample 5 PDF
Midterm Sample 5 PDF
Midterm Sample 5 PDF
Practice Midterm #5
1. ArrayList Mystery. Consider the following method:
public static void mystery5(ArrayList<Integer> list) {
for (int i = 0; i < list.size(); i++) {
int element = list.get(i);
list.remove(i);
list.add(0, element + 1);
}
System.out.println(list);
}
Write the output produced by the method when passed each of the following ArrayLists:
List
Output
____________________________________
(b) [8, 2, 9, 7, 4]
____________________________________
____________________________________
The call of filterRange(list, 5, 7); should remove all values between 5 and 7, therefore it should
change the list to store [4, 9, 2, 3, 1, 8]. If no elements in range min-max are found in the list, the
list's contents are unchanged. If an empty list is passed, the list remains empty. You may assume that the
list is not null.
The method removes and returns -8, so n will store -8 after the call and s will store the following values:
bottom [2, 8, 3, 19, 7, 3, 2, 42, 9, 3, 2, 7, 12, 4] top
If the minimum value appears more than once, all occurrences of it should be removed. For example, given
the stack above, if we again call removeMin(s);, it would return 2 and would leave the stack as follows:
bottom [8, 3, 19, 7, 3, 42, 9, 3, 7, 12, 4] top
You may use one queue as auxiliary storage. You may not use any other structures to solve this problem,
although you can have as many primitive variables as you like. You may not solve the problem recursively.
You may assume that the stack is not empty. For full credit, your solution must run in O(n) time.
You have access to the following two methods and may call them as needed to help you solve the problem:
public static void s2q(Stack<Integer> s, Queue<Integer> q) {
while (!s.isEmpty()) {
q.add(s.pop());
// Transfers the entire contents
}
// of stack s to queue q
}
public static void q2s(Queue<Integer> q, Stack<Integer> s) {
while (!q.isEmpty()) {
s.push(q.remove());
// Transfers the entire contents
}
// of queue q to stack s
}
Then your method should return the set [Alyssa, Dan, Lisa, Sylvia], because Alyssa's percentage
of 98 earns her a grade of 4.0 in the course, Dan's 81 earns him a 2.6, Lisa's 87 earns her a 3.3, and Sylvia's
92 earns her a 3.7.
The names can appear in any order in the set. If no students meet the desired minimum grade, return an
empty set. You may assume that no parameter is null, that every student's percentage is between 0 and
100 inclusive, that the grades map contains a grade entry between 0.0 and 4.0 inclusive for every
percentage earned by a student, and that the minGrade parameter is between 0.0 and 4.0 inclusive. For full
credit your code must run in less than O(n2) time where n is the number of students and/or percentages.
list
After
list
list2
Assume that you are using the ListNode class as defined in lecture and section:
public class ListNode {
public int data;
public ListNode next;
If a variable called list stores the values above, then the call of list.hasAlternatingParity()
would return true. If instead the list stored the following values, the call would return false because the
list has two even numbers in a row (4 and 12):
[2, 13, 4, 1, 0, 9, 2, 7, 4, 12, 3, 2]
By definition, an empty list or a list of one element has alternating parity. You may assume that every
element in the list is greater than or equal to 0.
Assume that we are adding this method to the LinkedIntList class as seen in lecture and as shown
below. You may not call any other methods of the class to solve this problem and your method cannot
change the contents of the list.
public class LinkedIntList {
private ListNode front;
}
methods
Output
"Bar")
"ck")
"a")
"McCain")
"BAR")
Value Returned
0
4
1
-1
-1
Strings have an indexOf method, but you are not allowed to call it. You are limited to these methods:
Method
equals(other)
length()
substring(fromIndex, toIndex)
substring(fromIndex)
Description
You are not allowed to construct any structured objects other than Strings (no array, List, Scanner,
etc.) and you may not use any loops to solve this problem; you must use recursion.
Output
[31, 21, 11]
[5, 8, 10, 3, 9]
[34, 10, 18, 29, 4, 0]
3.
4.
6.
//
//
//
//
//
//
list2 -> 4
4 -> 1
3 -> 2
list -> 3
2 /
1 /
7.
Call
mystery(13);
mystery(42);
mystery(40);
mystery(60);
mystery(48);
8.
Output
13
42,
40,
60,
48,
21
20, 10, 5
30, 15
24, 12, 6, 3