Deck 3: Selections
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/21
Play
Full screen (f)
Deck 3: Selections
1
To create a set that consists of string elements "red", "green", and "blue", use A. new HashSet(new String[]{"red", "green", "blue"})
B) new Set(Arrays.asList(new String[]{"red", "green", "blue"}))
"}))
D) new HashSet({"red", "green", "blue"})
B) new Set(Arrays.asList(new String[]{"red", "green", "blue"}))
"}))
D) new HashSet({"red", "green", "blue"})
C
2
Suppose List 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());
B) list.add(new ArrayList());
C) list.add("Red");
D) list.add(new java.util.Date());
C
3
To create a list to store integers, use B. ArrayList list = new ArrayList();
C) ArrayList
C) ArrayList
A
4
Show the time complexity of the following program:
for (int k = 0; k < 10; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print('*');
}
}
}
for (int k = 0; k < 10; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print('*');
}
}
}
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
5
Write a recursive mathematical definition for computing
for a positive integer.

Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
6
(Displaying words) Write a program that reads words separated by spaces 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.
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
7
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
C) TreeSet
D) LinkedList
E) Vector
C) TreeSet
D) LinkedList
E) Vector
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
8
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? B. Array List
C) Queue
D) Linked List
C) Queue
D) Linked List
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
9
Are there any compile errors in (a) and (b)? 

Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
10
Which method do you use to remove an element from a set or list named x? B. x.removes(element)
C) x.delete(element)
D) None of the above
E) x.deletes(element)
C) x.delete(element)
D) None of the above
E) x.deletes(element)
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
11
Which of the following is correct to sort the elements in a list lst? A. new LinkedList(new String[]{"red", "green", "blue"})
B) lst.sort()
C) Arrays.sort(lst)
B) lst.sort()
C) Arrays.sort(lst)
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
12
Fill in the code in Comparable______ c = new Date(); A.
B)
C)
B)
C)
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
13
Design an efficient O(logn) time algorithm for computing
. (Describe it using a pseudo code)

Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
14
Suppose that set1 (Set) is a set that contains the strings "red", "yellow", "green", and that set2 (Set) is another set that contains the strings "red", "yellow", "blue". Answer the following questions: (All the questions are independent)
What are set1 and set2 after executing set1.addAll(set2)?
What are set1 and set2 after executing set1.add(set2)?
What are set1 and set2 after executing set1.removeAll(set2)?
What are set1 and set2 after executing set1.remove(set2)?
What are set1 and set2 after executing set1.retainAll(set2)?
What is set1 after executing set1.clear()?
What are set1 and set2 after executing set1.addAll(set2)?
What are set1 and set2 after executing set1.add(set2)?
What are set1 and set2 after executing set1.removeAll(set2)?
What are set1 and set2 after executing set1.remove(set2)?
What are set1 and set2 after executing set1.retainAll(set2)?
What is set1 after executing set1.clear()?
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
15
Show the output of the following program: 

Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
16
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
B) Collections.max(Arrays.asList(names))
C) Arrays.sort(names)
D) Collections.max(names)
E) None of the above
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
17
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
C) HashSet
D) LinkedHashSet
E) Set
B) Collection
C) HashSet
D) LinkedHashSet
E) Set
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
18
How many times is the factorial method in Listing 20.1 invoked for factorial(5)? B. 3
C) 5
D) 4
C) 5
D) 4
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
19
Show the printout of the following code:
Test {
main(String[] args) {
Map map = LinkedHashMap();
map.put("123", "John Smith");
map.put("111", "George Smith");
map.put("123", "Steve Yao");
map.put("222", "Steve Yao");
System.out.println("(1) " + map);
System.out.println("(2) " + new TreeMap(map));
}
}
Test {
main(String[] args) {
Map
map.put("123", "John Smith");
map.put("111", "George Smith");
map.put("123", "Steve Yao");
map.put("222", "Steve Yao");
System.out.println("(1) " + map);
System.out.println("(2) " + new TreeMap(map));
}
}
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
20
To declare an interface named A with two generic types, use A. public interface A(E, F) { ... }
B) public interface A { ... }
D) public interface A(E) { ... }
B) public interface A
D) public interface A(E) { ... }
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck
21
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
C) TreeMap
D) LinkedList
E) HashSet
C) TreeMap
D) LinkedList
E) HashSet
Unlock Deck
Unlock for access to all 21 flashcards in this deck.
Unlock Deck
k this deck