1
Name:_______________________
CSCI 3230 Data Structures and Algorithms
Georgia Southern University
Instructor: Dr. Y. Daniel Liang
I pledge by honor that I will not discuss this exam with anyone until my
instructor reviews the exam in the class.
Signed by ___________________ Date ___________________
Part I:
1. (3 pts) Write a recursive method printStar(int n) that
displays n stars.
2. (3 pts) Suppose the method printStar(n) is given to
display n stars. Write a recursive method printTriangle(k)
that displays a triangle. For example, printTriangle(5)
displays
2
3. (2 pts) Are there any compile errors in (a) and (b)?
ArrayList dates = new ArrayList();
dates.add(new Date());
ArrayList<Date> dates =
new ArrayList<Date>();
4. (6 pts) Suppose that set1 (Set<Object>) is a set that
contains the strings “red”, “yellow”, “green”, and that
set2 (Set<String>) is another set that contains the strings
“red”, “yellow”, “blue”. Answer the following questions:
(All the questions are independent)
What is set1 after executing set1.addAll(set2)?
What is set1 after executing set1.add(set2)?
3
What is set1 after executing set1.clear()?
5. (2 pts) Show the output of the following code:
public class Test {
public static void main(String[] args) {
Map<String, String> map = new LinkedHashMap<>();
map.put(“123”, “John Smith”);
6. (5 pts)
a. Write a lambda expression for a Comparator that
compares strings in increasing order of their length.
b. Write a lambda expression for a Comparator that
compares strings in increasing order of their last
characters.
4
Comparator<String> c = ___________________________________;
7. (7 pts) (Displaying words) Write a program that reads
words separated by whitespaces from a text file and
5
8. (3 pts) (Binary search) Complete the code in the
recursiveBinarySearch method for binary search in a sorted
list.
public static int recursiveBinarySearch(int[] list, int key) {
int low = 0;
private static int recursiveBinarySearch(int[] list, int key,
int low, int high) {
if (low > high) // The list has been exhausted without a match
return low 1;
6
Name:_______________________
Part III: Multiple Choice Questions: (1 pts each)
(Please circle your answers on paper first. After you
finish the test, enter your choices online to LiveLab
(tiger.armstrong.edu/JavaLiveLab). Log in and click Take
1. The output of the following code is _________.
public class Test {
static int count = 0;
}
}
A. 6
B. 7
C. 8
D. 9
#
2. To declare an interface named A with two generic types, use
A. public interface A(E, F) { … }
B. public interface A<E> { … }
C. public interface A<E, F> { … }
D. public interface A(E) { … }
#
7
3. To create a list to store integers, use
A. ArrayList<Integer> list = new ArrayList<>();
B. ArrayList<Number> list = new ArrayList<>();
C. ArrayList<Object> list = new ArrayList<>();
D. ArrayList<int> list = new ArrayList<int>();
#
4. Suppose List<String> list = new ArrayList<>(). Which of the following operations are
correct?
A. list.add(new Integer(100));
B. list.add(new ArrayList());
C. list.add(“Red”);
D. list.add(new java.util.Date());
6. To find a maximum object in an array of strings (e.g., String[] names = {“red”,
“green”, “blue”}), use
A. Arrays.max(names)
B. Collections.max(Arrays.asList(names))
C. Arrays.sort(names)
D. Collections.max(names)
E. None of the above
#
7. Which method do you use to remove an element from a set or list named x?
A. x.remove(element)
B. x.removes(element)
C. x.delete(element)
D. None of the above
E. x.deletes(element)
#
8
8. To create a set that consists of string elements “red”, “green”, and “blue”, use
A. new HashSet<String>(new String[]{“red”, “green”, “blue”})
B. new Set<String>(Arrays.asList(new String[]{“red”, “green”, “blue”}))
C. new HashSet<String>(Arrays.asList(new String[]{“red”, “green”, “blue”}))
D. new HashSet<String>({“red”, “green”, “blue”})
#
9. Which of the data types below could be used to store elements in their natural order
based on the compareTo method.
A. TreeSet
B. Collection
#
10. Suppose the rule of the party is that the participants who arrive later will leave
earlier. Which data structure is appropriate to store the participants?
A. Stack
B. Array List
C. Queue
D. Linked List
#
11. Suppose your program frequently tests whether a student is in a soccer team, what is
the best data structure to store the students in a soccer team?
A. ArrayList
B. HashSet
C. TreeSet
D. LinkedList
E. Vector
#
12. Suppose your program frequently tests whether a student is in a soccer team and also
need to know the student’s information such as phone number, address, and age,
what is the best data structure to store the students in a soccer team?
A. ArrayList
B. HashMap
9
Please double check your answer before clicking the Submit
button. Whatever submitted to LiveLab is FINAL and counted
for your grade.
Have you submitted your answer to LiveLib? ______________