CS II Chapter 21 Quiz

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Analyze the following code. import java.util.*; public class Test { public static void main(String[] args) throws Exception { TreeSet<String> set = new TreeSet<>(); set.add("Red"); set.add("Green"); set.add("Blue"); System.out.println(set.last()); } } A. The program displays Red B. The program displays Blue C. The program displays Green D. The program may display Red, Blue, or Green. E. The program cannot compile, because the last() method is not defined in Set

A

Analyze the following code. import java.util.*; public class Test { public static void main(String[] args) throws Exception { TreeSet<String> set = new TreeSet<>(); set.add("Red"); set.add("Yellow"); set.add("Green"); set.add("Blue"); SortedSet temp = set.tailSet("Purple"); System.out.println(temp.first()); } } A. The program displays Red B. The program displays Blue C. The program displays Green D. The program displays Yellow E. The program displays Purple

A

If two objects o1 and o2 are equal, what are the values for o1.equals(o2) and o1.hashCode() == o2.hashCode()? A. true true B. true false C. false true D. false false

A

What is the output for the following code? import java.util.*; public class Test { public static void main(String[] args) { Set<A> set = new HashSet<A>(); set.add(new A()); set.add(new A()); set.add(new A()); set.add(new A()); System.out.println(set); } } class A { int r = 1; public String toString() { return r + ""; } public boolean equals(Object o) { return this.r == ((A)o).r; } public int hashCode() { return r; } } A. [1] B. [1, 1] C. [1, 1, 1] D. [1, 1, 1, 1]

A

What is the output of the following code? import java.util.*; import java.util.*; public class Test { public static void main(String[] args) { Set<String> set1 = new HashSet<>(); set1.add("Atlanta"); set1.add("Macon"); set1.add("Savanna"); Set<String> set2 = new HashSet<>(); set2.add("Atlanta"); set2.add("Macon"); set2.add("Savanna"); Set<String> set3 = new HashSet<>(); set3.add("Macon"); set3.add("Savanna"); set3.add("Atlanta"); System.out.println(set1.equals(set2) + " " + set1.equals(set3)); } } A. true true B. true false C. false false D. false true

A

Which of the data types below does not allow duplicates? A. Set B. List C. Vector D. Stack E. LinkedList

A

The elements in ________ are sorted. A. TreeSet B. List C. TreeMap D. HashSet E. LinkedHashSet

A and C

Which of the following are correct methods in Map? A. put(Object key, Object value) B. put(Object value, Object key) C. get(Object key) D. get(int index)

A and C

The Map is the base interface for _____________. A. TreeMap B. HashMap C. LinkedHashMap D. ArrayList E. LinkedList

A, B and C

The Collection interface is the base interface for _____________. A. Set B. List C. ArrayList D. LinkedList E. Map

A, B, C and D

Which of the following are correct methods in Map? A. containsKey(Object key) B. containsValue(Object value) C. remove(Object key) D. remove(int index) E. isEmpty(

A, B, C and E

Which of the following data types have iterators? A. HashSet B. TreeSet C. ArrayList D. LinkedList E. LinkedHashSet

A, B, C, D and E

Analyze the following code: import java.util.*; public class Test { public static void main(String[] args) { Set<String> set1 = new HashSet<>(); set1.add("red"); Set set2 = set1.clone(); } } A. Line 5 is wrong because the declared type for set1 is Set and the clone method is not defined Set. B. The program will be fine if set1.clone() is replaced by (HashSet)set1.clone() C. The program will be fine if set1.clone() is replaced by (Set)((HashSet)set1).clone() D. The program will be fine if set1.clone() is replaced by (HashSet)((HashSet)set1).clone() E. The program will be fine if set1.clone() is replaced by (LinkedHashSet)((HashSet)set1).clone()

A, C and D

Which of the following statements are true? A. All the methods in HashSet are inherited from the Collection interface. B. All the methods in TreeSet are inherited from the Collection interface. C. All the methods in LinkedHashSet are inherited from the Collection interface. D. All the methods in Set are inherited from the Collection interface. E. All the concrete classes of Collection have at least two constructors. One is the no-arg constructor that constructs an empty collection. The other constructs instances from a collection

A, C and D

Analyze the following code. import java.util.*; public class Test { public static void main(String[] args) throws Exception { TreeSet<String> set = new TreeSet<>(); set.add("Red"); set.add("Yellow"); set.add("Green"); set.add("Blue"); SortedSet temp = set.headSet("Purple"); System.out.println(temp.first()); } } A. The program displays Red B. The program displays Blue C. The program displays Green D. The program displays Yellow E. The program displays Purple

B

Analyze the following code: public class Test { public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("123", "John Smith"); map.put("111", "George Smith"); map.put("123", "Steve Yao"); map.put("222", "Steve Yao"); } } A. After all the four entries are added to the map, "123" is a key that corresponds to the value "John Smith". B. After all the four entries are added to the map, "123" is a key that corresponds to the value "Steve Yao". C. After all the four entries are added to the map, "Steve Yao" is a key that corresponds to the value "222". D. After all the four entries are added to the map, "John Smith" is a key that corresponds to the value "123". E. A runtime error occurs because two entries with the same key "123" are added to the map

B

If you want to store non-duplicated objects in the order in which they are inserted, you should use ____________. A. HashSet B. LinkedHashSet C. TreeSet D. ArrayList E. LinkedList

B

Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.addAll(s2), s1 is __________. A. [1, 2, 2, 3, 5, 6] B. [1, 2, 3, 5, 6] C. [1, 5] D. [2]

B

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

B

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. LinkedList E. Vector

B

To empty a Collection or a Map, you use the __________ method. A. empty B. clear C. zero D. setEmpty

B

Which of the data types below could be used to store elements in their natural order based on the compareTo method? A. HashSet B. TreeSet C. LinkedHashSet D. Collection E. Set

B

Analyze the following code: import java.util.*; public class Test { public static void main(String[] args) { HashSet<String> set1 = new HashSet<>(); set1.add("red"); Set<String> set2 = set1.clone(); } } A. Line 5 is wrong because a HashSet object cannot be cloned. B. Line 5 has a compile error because set1.clone() returns an Object. You have to cast it to Set in order to compile it. C. The program will be fine if set1.clone() is replaced by (Set<String>)set1.clone() D. The program will be fine if set1.clone() is replaced by (Set<String>)(set1.clone()) E. The program will be fine if set1.clone() is replaced by (HashSet<String>)(set1.clone())

B, D and E

Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.removeAll(s2), s1 is __________. A. [1, 2, 2, 3, 5, 6] B. [1, 2, 3, 5, 6] C. [1, 5] D. [2]

C

The output of the following code is ____________. LinkedHashSet<String> set1 = new LinkedHashSet<>(); set1.add("New York"); LinkedHashSet<String> set2 = set1; set1.add("Atlanta"); set2.add("Dallas"); System.out.println(set2); A. [New York] B. [New York, Atlanta] C. [New York, Atlanta, Dallas] D. [New York, Dallas]

C

To get an iterator from a set, you may use the __________ method. A. getIterator B. findIterator C. iterator D. iterators

C

Which of the following is correct to perform the set intersection of two sets s1 and s2? A. s1.intersect(s2) B. s1.join(s2) C. s1.retainAll(s2) D. s1.intersection(s2)

C

Which of the following is correct to perform the set union of two sets s1 and s2? A. s1.union(s2) B. s1 + s2 C. s1.addAll(s2) D. s1.add(s2)

C

Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.addAll(s2), s2 is __________. A. [1, 2, 2, 3, 5, 6] B. [1, 2, 3, 5, 6] C. [1, 5] D. [2, 3, 6] E. [2]

D

Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.retainAll(s2), s1 is __________. A. [1, 2, 2, 3, 5, 6] B. [1, 2, 3, 5, 6] C. [1, 5] D. [2]

D

The output of the following code is ____________. LinkedHashSet<String> set1 = new LinkedHashSet<>(); set1.add("New York"); LinkedHashSet<String> set2 = (LinkedHashSet<String>)(set1.clone()); set1.add("Atlanta"); set2.add("Dallas"); System.out.println(set2); A. [New York] B. [New York, Atlanta] C. [New York, Atlanta, Dallas] D. [New York, Dallas]

D

What is the output for the following code? import java.util.*; public class Test { public static void main(String[] args) { Set<A> set = new HashSet<>(); set.add(new A()); set.add(new A()); set.add(new A()); set.add(new A()); System.out.println(set); } } class A { int r = 1; public String toString() { return r + ""; } public boolean equals(Object o) { return this.r == ((A)o).r; } } A. [1] B. [1, 1] C. [1, 1, 1] D. [1, 1, 1, 1]

D

What is the output for the following code? import java.util.*; public class Test { public static void main(String[] args) { Set<A> set = new HashSet<>(); set.add(new A()); set.add(new A()); set.add(new A()); set.add(new A()); System.out.println(set); } } class A { int r = 1; public String toString() { return r + ""; } public int hashCode() { return r; } } A. [1] B. [1, 1] C. [1, 1, 1] D. [1, 1, 1, 1]

D

Which of the following is correct to perform the set difference of two sets s1 and s2? A. s1.difference(s2) B. s1 - s2 C. s1.subtract(s2) D. s1.removeAll(s2)

D

Analyze the following code. import java.util.*; public class Test { public static void main(String[] args) throws Exception { Set<String> set = new TreeSet<>(); set.add("Red"); set.add("Green"); set.add("Blue"); System.out.println(set.first()); } } A. The program displays Red B. The program displays Blue C. The program displays Green D. The program may display Red, Blue, or Green. E. The program cannot compile, because the first() method is not defined in Set

E

Which of the following data types does not implement the Collection interface? A. HashSet B. TreeSet C. ArrayList D. LinkedList E. Map

E


Kaugnay na mga set ng pag-aaral

World History -REGENTS Practice Q's & Answers

View Set

Lehne - Chs. 83-89 - Antibiotics

View Set

Managerial Economics (Chapter 6)

View Set

Practice Questions (Exam 5-8)- Comps B

View Set