CS2 Final Exam

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Is ArrayList<?> same as ArrayList<? extends Object>? a) Yes b) No

a) Yes

23.) ____ is a data structure to store data in a sequential order. a. A list b. A set c. A BST d. A stack e. A queue f. A Map

a. A list

17.) Which of the following statements are true? a. All of the methods in HashSet are inherited from the Collection interface. b. All of the methods in TreeSet are inherited from the Collection interface. c. All of the methods in LinkedHashSet are inherited from the Collection interface. d. All of the methods in the 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. All of the methods in HashSet are inherited from the Collection interface c. All of the methods in LinkedHashSet are inherited from the Collection interface. d. All the methods in the Set are inherited from the Colection interface.

34.) Chaining and probing are two methods used to resolve collisions in hash tables so that the amortized access time is O(1). For each of the following claims, indicate whether it is true of chaining, probing, both, or neither. (match each statement below to one of the 4 items.) a. Needs additional memory beyond the primary array for the hash table. b. Requires doubling the table periodically. c. May be either "linear" or "quadratic' d. Crashes if the load factor becomes greater than 1 e. Requires computing the hash function multiple times when doing an insertion.

a. Chaining b. Probing c. Probing d. Chaining e. Probing

16.) The Collection interface is the base interface for ________. a. Set b. List c. ArrayList d. LinkedList e. Map f. All of the above

a. Set b. List c. ArrayList d. LinkedList

15.) The elements in _______ are stored. a. TreeSet b. List c. TreeMap d. HashSet e. LinkedHashSet

a. TreeSet

32.) Answer the following questions with either true or false. Assume there are in elements in the data structure. No explanation necessary. a. One can implement a stack based on a linked list so that EACH INDIVIDUAL push/pop operation is time O(1) b. One can implement a stack (of unbounded size) based on an array so that each individual push/pop operations is time O(1) c. One can reverse the order of the elements in a linked list in time O(n). d. It is possible to append two linked lists in time O(1). e. Adding an element to a heap has worst-case time complexity O(log(n)). f. Returning the maximum element in a max-heap (but not deleting it from the heap can be done in time O(1).

a. True b. False c. True d. True e. True f. True

5.) Which of the following statements are true? a. java.util.LinkedList implements the java.util.Queue interface. b. java.util.ArrayList implements the java.util.Qeue interface. c. java.util.Hashset implements the java.util.Qeue interface. d. java.util.LinkedHashset implements the java.util.Qeue interface. e. java.util.PriorityQueue implements the java.util.Queue interface.

a. java.util.LinkedList implements the java.util.Queue interface. e. java.util.PriorityQueue implements the java.util.Queue interface.

2.) Is ArrayList<Integer> a subclass of ArrayList<Object> ? a) Yes b) No

b) No

Which of the following can be used to replace YYYYYYYY in the following code? public class WildCardDemo3 { public static void main(String[] args){ GenericStack<String> stack1 = new GenericStack<>(); GenerickStack<Object> stack2 = new GenericStack<>(); stack2.push("Java"); stack2.push(2); stack1.push("CSharp"); add(stack1, stack2); WildCardDemo2.print(stack2); } public static <T> void add(GenericStack<T> stack1, GenericStack<YYYYYYYY> stack2){ while(!stack1.isEmpty()) stack2.push(stack1.pop()); } } a. ? super Object b. ? super T c. ? extends T d. ? extends superT e. ? extends Object

b. ? super T

12..) Analyze the following code: public class Test{ public static void main(String[] args){ Map<String, String> map = new HashMap<>(); map.put("23", Michael Jordan"); map.put("30", Steve Curry"); map.put("23", Lebron James"); map.put("11", Clay Thompson"); } } a. After all the four entries are added to the map, "23" is a key that corresponds to the value "Michael Jordan". b. After all the four entries are added to the map, "23" is a key that corresponds to the value "LeBron James". c. After all the four entries are added to the map, "Clay Thompson" is a key that corresponds to the value "11". d. After all the four entries are added to the map, "Michael Jordan" is a key that corresponds to the value "23". e. A runtime error occurs because two entries with the same key "23" are added to the map.

b. After all the four entries are added to the map, "23" is a key that corresponds to the value "LeBron James".

27.) Suppose list1 is an MyArrayList and list2 is a MyLinkedList. Both contain 1 million double values. Analyze the following code: A: while(list1.size() > 0) list1.remove(0); B.: while(list2.size() > 0) list2.remove(0); a. Code fragment A runs faster than code fragment B. b. Code fragment B runs faster than code fragment A. c. Code fragment A runs as fast as code fragment B. d. Code fragment A runs faster only slightly. e. Impossible to compare.

b. Code fragment B runs faster than code fragment A.

13.) Suppose your program frequently test 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. TreMap .d LinkedList e. HashSet

b. HashMap

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. LinkedHashSet

26.) In the implementation of MyLinkedList, which of the following are true? a. MyLinkedList has a capacity property. b. MyLinkedList has the properties named first and last to point to the nodes in a linked list. c. If a linked list is empty, first is null and last is null. d. If a linked list contains one element, first points to the mode and last is null. e. last.next is always null. f. All of the above

b. MyLinkedList has the properties named first and last to point to the nodes in a linked list. c. If a linked list is empty, first is null and last is null. e. last.next is always null.

6.) What does the following codes display? import.java.util.* public class Test { public static void main(String[] args){ PriorityQueue<Integer> Queue = new PriorityQueue<Integer> { Arrays.asList(60, 10, 50, 30, 40 20)); while(!queue.isEmpty()) System.out.print(queue.poll() + " "); } } a. The program displays 60 10 50 30 40 20 b. The program displays 10 20 30 40 50 60 c. The program displays 60 50 40 30 20 10 d. There is no guarantee that the program displays 10 20 30 40 50 60

b. The program displays 10 20 30 40 50 60

25.) In the implementation of MyArrayList, which of the following are true? a. size never reduces. b. capacity never reduces. c. inside MyArrayList, a regular array is used to store elements. d. size is not declared in MyArrayList, but declared in MyAbstract List as protected. e. If the current capacity equals to size, capacity is doubled when a new element is added to MyArrayList f. All of the above

b. capacity never reduces c. inside MyArrayList, a regular array is used to store elements. d. size is not declared in MyArrayList, but declared in MyAbstractList as protected. e. If the current capacity equals to size, capacity is doubled when a new element is added to MyArrayList

22.) Which of the following big O notation is O(nlogn) ? a. 300n + 400n*n b. 23nlogn + 9999 c. 45n + 45nlogn + 5000 d. n*n*n + nlogn + 99999 e. n + 99logn f. none of the above

c. 45n + 45nlogn + 5000

9.) Which of the following is correct to create a list from an array? a. new List<String>{{"red", "green", "blue"}} b. new List<String>{new String[]{"red","green","blue"}} c. Arrays.asList<String>(new String[]{"red", "green", "blue"}) d. new ArrayList<String>(new String[]{"red', "green", "blue"}} e. new LinkedList(new String[]{"red", "green", "blue"}}

c. Arrays.asList<String>(newString[]{"red", "green", "blue"})

28.) In the implementation of MyStack and MyQueue, which of the following are true? a. MyStack contains all the methods defined in MyArrayList. b. MyQueue contains all the methods defined in MyLinkedList. c. MyStack contains an array list for storing elements. d. MyQueue contains a linked list for storing elements. e. None of the above

c. MyStack contains an array list for storing elements.

29.) During hashing, a collision occurs ________ a. When two or more hash values are mapped in the same key. b. When two or more key values are the same. c. When two or more keys are mapped to the same hash value. d. When two elements have the same key value. e. When two elements are mapped to the same key.

c. When two or more keys are mapped to the same hash value.

8.) What is the output of the following code? import.java.util.*; public class Test { public static void main(String[] args){ ArrayList<Student> list = new ArrayList <>(); list.add(new Student("Peter", 65)); list.add(new Student("Jill", 50)); list.add(new Student("Sarah", 34)); Collections.sort(list); System.out.print(list + " "); Collections.sort(list., new StudentComparator1()); System.out.println(list); } static class StudentComparator 1 implements Comparator<Student> { public int compare(Student s1, Student s2){ return s1.name.compareTo(s2.name); } } static class Student implements Comparable<student> { String name; int age; Student(String name, int age){ this.name = name; this.age = age; } public int compareTo(Student s){ return this.age - s.age; } public String toString() { return "[" + name + "," + "age" + "]"; } } } a. [[Sarah, 34], [Jill, 50], [Peter, 65]] | [[Sarah, 34], [Jill, 50], [Peter, 65]] b. [[Jill, 50], [Peter, 65], [Sarah, 34]] | [[Jill, 50], [Peter, 65], [Sarah, 34]] c. [[Sarah, 34], [Jill, 50], [Peter, 65]] | [[Jill, 50], [Peter, 65], [Sarah, 34]] d. [[Jill, 50], [Peter, 65], [Sarah, 34]] | [[Sarah, 34], [Jill, 50], [Peter, 65]] e. None of the above. This program fails to compile.

c. c. [[Sarah, 34], [Jill, 50], [Peter, 65]] | [[Jill, 50], [Peter, 65], [Sarah, 34]]

30.) When a collision occurs during the insertion of an entry to a hash table, ______ finds the next available location sequentially. a. quadratic probic b. double hashing. c. linear probing d. Rehashing e. Separate chaining

c. linear probing

7.) You can use the methods in the Arrays class to a. find the maximum object in an array based on the compareTo method. b. find the maximum object in an array using a Comparator object. c. sort an array. d. shuffle an array. e. do a binary search on an array.

c. sort an array e. do a binary search on an array.

21.) Analyzing algorithm efficiency is ___. a. to measure their actual execution time. b. to estimate their execution time. c. to estimate their growth function. d. to estimate their complexity.

c. to estimate their growth function

What is list after the following code is executed? ArrayList<Interger> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.remove(2); System.out.println(list): a. [1,2,3,4,5] b. [2,3,4,5] c. [1,3,4,5] d. [1,2,4,5] e. [1,2,3,5]

d. [1,2,4,5]

20.) 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] e. None of the above

d. [2]

19.) The output of the following code is _______. LinkedHashSet<String> set1 - new LinkedHashSet<>(); set1.add("Memphis"); LinkedHashSet<String> set2 = set1; set1.add("Germantown"); set2.add("Kelowna"); set2.add("San Francisco"); System.out,println(set2); a. [Memphis] b. [Memphis, Germantown] c. [Kelowna, San Francisco] d. [Memphis, Germantown, Kelowna, San Francisco] e. [Memphis, San Francisco] f. Compiler error (cannot have set2 = set1, one has to copy the contents)

d. [Memphis, Germantown, Kelowna, San Francisco]

14.) 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. LinkedHashSet c. TreeSet d. LinkedList e. HashSet

e. HashSet

11.) Which of the following data types does not implement the Collection interface? a. HashSet b. TreeSet c. ArrayList d. LinkedList e. Map

e. Map

1.) The method header is left blank in the following code. Fill in the header. public class GenericMethodDemo{ public static void main(String[] args){ integer[] integers = {1,2,3,4,5}; String[] strings = {"London", "Paris", "New York", "Austin"}; print(integers); print(strings); } // "missing header here" ------------------------------(which header ???) { for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); System.out.println(); } } a. public static void print(integer[] list) b. public static void print(String[] list) c. public static void print(int[] list) d. public static void print(Object[] list) e. public static <E> void print(E[] list) f. public static <?> void print(E[] list)

e. public static <E> void print(E[] list) f. public static <?> void print(E[] list)

24.) Which of the following statements are true? a. MyArrayList and MyLinkedList are two concrete implementations of MyList. b. MyArrayList is implemented using an array. The array is dynamically created. If the capacity of the array is exceeded, create a new larger array and copy all the elements from the current array to the new array. c. MyLinkedList is implemented using a linked structure. d. A linked structure consists of nodes. Each node is dynamically created to hold an element. All the nodes are linked together to form a list. e. MyAbstractList partially implements MyList. f. All of the above

f. All of the above


संबंधित स्टडी सेट्स

IB Computer Science SL (Chapter 2)

View Set

NASM Corrective exercise specialist

View Set

Leadership, chapter 4: nursing licensure & certification

View Set

Texas Real Estate Agency - UNIT 3 - Duties and Disclosures to Third Parties

View Set

Observation & Assessment II Final

View Set