1
Name:_______________________
Covers
50 min
CSCI 3230 Data Structures and Algorithms
Armstrong Atlantic State University
Instructor: Y. Daniel Liang
Solution
Part I:
1.
public static void printStar(int n) {
if (n > 0) {
System.out.print(“*”);
2.
public static void printTriangle(int n) {
if (n > 0) {
printTriangle(n – 1);
}
3. (2 pts) Are there any compile errors in (a) and (b)?
ArrayList dates = new ArrayList();
(a)
(b)
(a) no
(b) Yes
4. (6 pts) Suppose that set1 (Set<Object>) is a set that contains the strings “red”,
What is set1 after executing set1.addAll(set2)?
set1: fired, yellow, green, blue]
2
What is set1 after executing set1.add(set2)?
What is set1 after executing set1.removeAll(set2)?
What is set1 after executing set1.remove(set2)?
What is set1 after executing set1.retainAll(set2)?
What is set1 after executing set1.clear()?
set1: []
3
5. (2 pts) Show the output of the following code:
public class Test {
6
Comparator<String> c = (s1, s2)> s1.length() s2.length();
4
7. (7 pts) (Display words) Write a program that reads words separated by whitespaces
from a text file and displays words in ascending order. (If two words are the same,
display only one). Pass the text filename from the command line.
import java.io.*;
import java.util.*;
File file = new File(args[0]);
Scanner input = new Scanner(file);
TreeSet<String> set = new TreeSet<>();
}
8. (3 pts) (Binary search) Complete the code in the
recursiveBinarySearch method for binary search in a sorted
list.
5
private static int recursiveBinarySearch(int[] list, int key,
int low, int high) {
int mid = (low + high) / 2;
if (key < list[mid])
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. Log in and click Take Instructor Assigned Quiz. Choose Quiz1. You
have 5 minutes to enter and submit the answers.)
1. The output of the following code is _________.
public class Test {
static int count = 0;
public static void main(String[] args) {
f(7);
System.out.println(count);
}
public static int f(int n) {
6
count++;
if (n == 0)
return 1;
else
return f(n – 1) + n * n;
}
}
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) { … }
#
3. To create a list to store integers, use
A. ArrayList<Integer> list = new ArrayList<>();
B. ArrayList<Number> list = new ArrayList<Integer>();
#
4. Suppose List<String> list = new ArrayList<String>(). 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());
#
5. Which of the following is correct to sort the elements in a list lst?
7
A. new LinkedList(new String[]{“red”, “green”, “blue”})
B. lst.sort()
C. Arrays.sort(lst)
D. Collections.sort(lst)
#
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)
#
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
8
C. HashSet
D. LinkedHashSet
E. Set
#
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
C. TreeMap
D. LinkedList
E. HashSet