Java Data Structures

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Draw the heap that results from adding the following integers. ( 34, 45, 3, 87, 65, 32, 1 ,12 ,17 )

1 / \ 12 3 / \ / \ 17 65 34 32 / \ 87 45

Errors? ArrayList dates = new ArrayList();dates.add(new Date());dates.add(newString());

No

Rule of the party is that participants that arrive later will leave earlier; what data structure is appropriate to store participants -Stack -Array List -Linked List -All of the above

Stack

public class Test {public static void main(String[] args{ System.out.println( "Sum is " + xMethod(5)); } public static int xMethod(int n) { if (n == 1) return1; else return n + xMethod(n - 1); } }

Sum is 15

Which of the following is the correct way to instantiate an array of 10 generic objects? • T[] x = new T[10]; • T[10] x = new T[]; • T[] x = (T[])(new Object[10]);

T[] x = (T[])(new Object[10]);

What is the complexity of the following code assuming that the cout statement has O(1) complexity. Explain your answer. for (int i = 0; i < n ; i++) for (int j = 0; j < n ; j++) cout << i + j;

The for loop with variable "i" iterates n times. And for each iteration of the for loop with variable "i" another for loop with "j" is iterated n times. Therefore, When i=0 -----------> j goes from 0 to n-1 ----------------> n times When i=1 -----------> j goes from 0 to n-1 ----------------> n times . . When i=n-1 -----------> j goes from 0 to n-1 ----------------> n times. Add n*n times Therefore, the time complexity of the code is O(n2 ).

A given set of n distinct numbers may be sorted by first building a binary search tree containing those numbers (inserting the numbers one by one), and then printing the numbers during a traversal. Which standard binary tree traversal should be used?

To print the node values in proper order, an inorder traversal must be used.

Which of the following represents the efficiency of the insertion sort? a. O(1) b. O(log n) c. O(n) d. O(n2)

d. O(n2)

What type of rotation do you need to rebalance an AVL tree if the addition that unbalanced the tree occurred in the left subtree of node N, the unblaanced node's right child?

double right rotation or right-left rotation

Which of the data types below does not allow duplicates? a) List b) LinkedList c) Stack d) Vector e) Set

e) Set

Which method do you use to test if an element e is in a set or list named x? a) x.include(e) b) (e instanceof List) || (e instanceof Set) c) x.in(e) d) x.contain(e) e) x.contains(e)

e) x.contains(e)

What is the preorder, postorder and inorder traversal of the following tree 60 / \ 15 100 \ / \ 57 67 107 / / 49 64

inorder: 15 49 57 60 64 67 100 107 preorder: 60 15 57 49 100 67 64 107 postorder: 49 57 15 64 67 107 100 60

Consider the following statements: int[ ] p = new int[100]; int[ ] s = p; After these statements, which of the following statements will change the last value of p to 75?

• p[99] = 75; • p[100] = 75;

Please describe when are the best times to use EACH of the following data structures? Trees Binary Trees Binary Search Trees Nodes (types) Paths Path lengths Array lists Linked Lists Doubly-Linked Lists Stacks Array-based Stacks Queues

-->Trees :-If we organize keys in form of a tree (with some ordering e.g., BST), we can search for a given key in moderate time (quicker than Linked List and slower than arrays). Self-balancing search trees like AVL and Red-Black trees guarantee an upper bound of O(Logn) for search . We can insert/delete keys in moderate time (quicker than Arrays and slower than Unordered Linked Lists). Self-balancing search trees like AVL and Red-Black trees guarantee an upper bound of O(Logn) for insertion/deletion. -->Binary Trees :-Used in almost every high-bandwidth router for storing router-tables. -->Binary Search Trees:-Used in many search applications where data is constantly entering/leaving, such as the map and set objects in many languages libraries. -->Nodes:- Internal node:- a node with at least one child. Leaf node:- a node with no children. Root node:- a node distinguished from the rest of the tree nodes. Usually, it is depicted as the highest node of the tree. Sibling nodes:- these are nodes connected to the same parent node. -->Paths:- A paths in a graph is a sequence of vertices such that from each of its vertices there is an edge to the next vertex in the sequence. -->Path lengths:-number of edges that must be followed -->Array lists:-Internally an ArrayList uses an Object[]. As you add items to an ArrayList, the list checks to see if the backing array has room left. If there is room, the new item is just added at the next empty space. If there is not room, a new, larger, array is created, and the old array is copied into the new one. -->Linked Lists :-Linked list are better at addition and removals at a cursor. -->Doubly-Linked Lists:-Doubly Linked List is a variation of Linked list in which navigation is possible in both ways either forward and backward easily as compared to Single Linked List. Doubly LinkedList contains an link element called first and last. Each Link carries a data field(s) and a Link Field called next. Each Link is linked with its next link using its next link. Each Link is linked with its previous link using its prev link. Last Link carries a Link as null to mark the end of the list. -->Stacks :-It makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed (inserted or added) last, is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation. -->Array-based Stacks :- In spite of capacity limitation, array-based implementation is widely applied in practice. In a number of cases, required stack capacity is known in advance and allocated space exactly satisfies the requirements of a particular task. In other cases, stack's capacity is just intended to be "big enough". Striking example of the last concept is an application stack. It's capacity is quite large, but too deep recursion still may result in stack overflow. -->Queues :-Same as stack, queue can also be implemented using Array, Linked-list, Pointer and Structures. For the sake of simplicity we shall implement queue using one-dimensional array.

What is the value of the postfix expression 6 3 2 4 + - *? • 20 • -30 • -18 • -20

-18

What is the max heap that results from adding the following elements 5, 16, 30, 102, 14, 3 in that order?

102 / \ 30 16 / \ / 5 14 3

Consider the algorithm for determining whether a sequence of parentheses is balanced. What is the maximum number of parentheses that will appear on the stack AT ANY ONE TIME when the algorithm analyzes: (()(())(()))? • 1 • 2 • 3 • 4

3

After two passes on the numbers ( 5 3 9 5 ), what would be the result if you were to use Selection Sort? A. 5 3 5 9 B. 5 5 3 9 C. 3 5 5 9 D. 3 5 9 5

3 5 9 5

What is the output of the following program public class recurse2 { public static void main (String[] args) { recurse(3); System.out.print("---"); recurse(2); } public static void recurse(int x) { if (x<=1) System.out.print("***"); else if ((x % 2) == 0) { System.out.print("+++"); recurse(x-2); } else { System.out.print(x); recurse(x-1); } } } • 3***+++***---+++ • 3+++***---+++*** • 3*******++++---- • None of the above

3+++***---+++***

What is the binary search tree that results after inserting 45, 43, 100, 34, 23, and 3 into an empty binary search tree?

45 / \ 43 100 / 34 / 23 / 3

Consider the following list 45, 34, 342, 102, 3, 5, 35, 29, 244, 34, 7 On applying the median-of-three partitioning using quick sort, what is result of the first pass of partitioning? answer: 5, 34, 342, 102, 3, 7, 35, 29, 244, 34, 45 (median of three sorting) 5, 34, 342, 102, 3, 34, 35, 29, 244, 7, 45 5, 34, 342, 102, 3, 34, 35, 29, 244, 7, 45 5, 3, 342, 102, 34, 34, 35, 29, 244, 7, 45

5, 3, 7, 102, 34, 34, 35, 29, 244, 342, 45

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

A. TreeSet

Suppose that items A, B, C, D and E are pushed, in that order, onto an initially empty stack S. S is then popped four times; as each item is popped off, it is inserted into an initially empty queue. If two items are then removed from the queue, what is the next item that will be removed from the queue? need answer

A. item A B. item B C. item C D. item D E. item E

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)

A. x.remove(element)

19 A pointer variable which contains the location at the top element of the stack is called ..... A. Top B. Last C. Final D. End

A. Top

Here is an INCORRECT pseudocode for the algorithm which is supposed to determine whether a sequence of parentheses is balanced: declare a character stack while ( more input is available) { read a character if ( the character is a '(' ) push it on the stack else if ( the character is a ')' and the stack is not empty ) pop a character off the stack else print "unbalanced" and exit } print "balanced" Which of these unbalanced sequences does the above code think is balanced? A. ((()) B. ())(() C. (()())) D. (()))()

A. ((())

Here is an INCORRECT pseudocode for the algorithm which is supposed to determine whether a sequence of parentheses is balanced: declare a character stack while ( more input is available) { read a character if ( the character is a '(' ) push it on the stack else if ( the character is a ')' and the stack is not empty ) pop a character off the stack else print "unbalanced" and exit } print "balanced" Which of these unbalanced sequences does the above code think is balanced? A. ((()) B. ())(() C. (()())) D. (()))()

A. ((())

If the binary tree below is printed by a preorder traversal, what will the result be? 6 / \ 17 22 / \ / \ 9 4 16 12 need answer

A. 9 4 17 16 12 11 6 B. 9 17 6 4 16 22 12 C. 6 9 17 4 16 22 12 D. 6 17 22 9 4 16 12 E. 6 17 9 4 22 16 12

To create a list to store integers, use A. ArrayList<Integer> list = new ArrayList<Integer>(); B. ArrayList<Number> list = new ArrayList<Integer>(); C. ArrayList<Object> list = new ArrayList<Integer>(); D. ArrayList<int> list = new ArrayList<int>();

A. ArrayList<Integer> list = new ArrayList<Integer>();

In the linked list implementation of the stack class, where does the push method place the new entry on the linked list? A. At the head B. At the tail C. After all other entries that are greater than the new entry. D. After all other entries that are smaller than the new entry.

A. At the head

A queue is a ......... A. FIFO B. LIFO C. FILO D. LOFI

A. FIFO

In liked representation of stack ....... holds the elements of the stack. A. INFO fields B. TOP fields C. LINK fields D. NULL fields

A. INFO fields

........ form of access is used to add remove nodes from a stack. A. LIFO B. FIFO C. Both A and B D. None of these

A. LIFO

What kind of list is best to answer questions such as "What is the item at position n?" A. Lists implemented with an array. B. Doubly-linked lists. C. Singly-linked lists. D. Doubly-linked or singly-linked lists are equally best

A. Lists implemented with an array.

Which of the following expressions evaluates to true with approximate probability equal to P? (P is double and 0 <= P <= 1). A. Math.random() < P B. Math.random() > P C. Math.random() < P * 100 D. Math.random() > P * 100

A. Math.random() < P

The elements are removal from a stack in .......... order. A. Reverse B. Hierarchical C. Alternative D. Sequential

A. Reverse

What is the value of the postfix expression 6 3 2 4 + - *: A. Something between -15 and -100 B. Something between -5 and -15 C. Something between 5 and -5 D. Something between 5 and 15 E. Something between 15 and 100

A. Something between -15 and -100

What happens when you push a new node onto a stack? A. The new node is placed at the front of the linked list B. The new node is placed at the back of the linked list C. The new node is placed at the middle of the linked list D. No Changes happens

A. The new node is placed at the front of the linked list

What kind of list is best to answer the question "What is the item at position n?" • Array Lists implemented with an array • Doubly-linked Lists • Singly-linked Lists • B and C

Array List implemented with an array

In the linked list implementation of the stack class, where does the push method place the new entry on the linked list? A. At the head B. At the tail C. After all other entries that are greater than the new entry. D. After all other entries that are smaller than the new entry.

At the head

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

B. HashSet

Which boolean expression indicates whether the numbers in two nodes (p and q) are the same. Assume that neither p nor q is null. A. p == q B. p.data == q.data C. p.link == q.link D. None of the above.

B. p.data == q.data

The retrieval of items in a stack is ........... operation. A. push B. pop C. retrieval D. access

B. pop

Which of the following stack operations could result in stack underflow? A. is_empty B. pop C. push D. Two or more of the above answers

B. pop

14 The insertion operation in the stack is called ......... A. insert B. push C. pop D. top

B. push

Consider the implementation of the Queue using a circular array. What goes wrong if we try to keep all the items at the front of a partially-filled array (so that data[0] is always the front). A. The constructor would require linear time. B. The getFront method would require linear time. C. The insert method would require linear time. D. The isEmpty method would require linear time.

B. The getFront method would require linear time.

Suppose cursor refers to a node in a linked list (using the Node class with instance variables called data and link). What boolean expression will be true when cursor refers to the tail node of the list? A. (cursor == null) B. (cursor.link == null) C. (cursor.data == null) D. (cursor.data == 0.0) E. None of the above.

B. (cursor.link == null)

Consider the usual algorithm for determining whether a sequence of parentheses is balanced. Suppose that you run the algorithm on a sequence that contains 2 left parentheses and 3 right parentheses (in some order). What is the maximum number of parentheses that will ever appear on the stack AT ONE TIME during the computation? A. 1 B. 2 C. 3 D. 4 E. 5 or more

B. 2

Here is an infix expression: 4+3*(6*3-12). Suppose that we are using the usual Stack algorithm to convert the expression from infix to postfix notation. What is the maximum number of symbols that will appear on the stack AT ONE TIME during the conversion of this expression? A. 1 B. 2 C. 3 D. 4 E. 5

B. 2

In the linked list implementation of the queue class, where does the insert method place the new entry on the linked list? A. At the head B. At the tail C. After all other entries that are greater than the new entry. D. After all other entries that are smaller than the new entry.

B. At the tail

New nodes are added to the ......... of the queue. A. Front B. Back C. Middle D. Both A and B

B. Back

Consider the implementation of the Stack using a partially-filled array. What goes wrong if we try to store the top of the Stack at location [0] and the bottom of the Stack at the last used position of the array? A. Both peek and pop would require linear time. B. Both push and pop would require linear time. C. The Stack could not be used to check balanced parentheses. D. The Stack could not be used to evaluate postfix expressions.

B. Both push and pop would require linear time.

In linked representation of stack the null pointer of the last node in the list signals .......... A. Beginning of the stack B. Bottom of the stack C. Middle of the stack D. In between some value

B. Bottom of the stack

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))

......... form of access is used to add and remove nodes from a queue. A. LIFO, Last In First Out B. FIFO, First In First Out C. Both a and b D. None of these

B. FIFO, First In First Out

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

I have implemented the queue with a circular array, keeping track of front, rear, and manyItems (the number of items in the array). Suppose front is zero, and rear is one less than the current capacity. What can you tell me about manyItems? A. manyItems must be zero. B. manyItems must be equal to the current capacity. C. count could be zero or the capacity, but no other values could occur. D. None of the above.

C (But count should be replaced by manyItems---sorry!)

One difference between a queue and a stack is: A. Queues require linked lists, but stacks do not. B. Stacks require linked lists, but queues do not. C. Queues use two ends of the structure; stacks use only one. D. Stacks use two ends of the structure, queues use only one.

C. Queues use two ends of the structure; stacks use only one.

The term push and pop is related to A. Array B. Lists C. Stacks D. Trees

C. Stacks

In the linked representation of the stack ......... behaves as the top pointer variable of stack. A. Stop pointer B. Begin pointer C. Start pointer D. Avail pointer

C. Start pointer

Which is the pointer associated with the stack? A. FIRST B. FRONT C. TOP D. REAR

C. TOP

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());

C. list.add("Red");

Suppose that a method has one node as a parameter and it returns two references to nodes. What's the best header for the method? A. IntNode foo(IntNode p) B. IntNode, IntNode foo(IntNode p) C. IntNode[ ] foo(IntNode p) D. void foo(IntNode p, IntNode answer1, IntNode answer2)

C. IntNode[ ] foo(IntNode p)

Suppose that p is a reference variable that contains the null reference. What happens at runtime if the program tries to activate a method of p? A. IllegalArgumentException B. IllegalStateException C. NullPointerException D. The results are unpredictable.

C. NullPointerException

If data is a circular array of CAPACITY elements, and rear is an index into that array, what is the formula for the index after rear? A. (rear % 1) + CAPACITY B. rear % (1 + CAPACITY) C. (rear + 1) % CAPACITY D. rear + (1 % CAPACITY)

C. (rear + 1) % CAPACITY

Consider the usual algorithm for determining whether a sequence of parentheses is balanced. What is the maximum number of parentheses that will appear on the stack AT ANY ONE TIME when the algorithm analyzes: (()(())(()))? A. 1 B. 2 C. 3 D. 4 E. 5 or more

C. 3

After one pass on the numbers ( 5 3 9 5 ), what would be the result if you were to use Bubble Sort? A. 5 3 5 9 B. 5 5 3 9 C. 3 5 5 9 D. 9 5 5 3

C. 3 5 5 9

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"})) C. new HashSet(Arrays.asList(new String[]{"red", "green", "blue"})) D. new HashSet({"red", "green", "blue"})

C. new HashSet(Arrays.asList(new String[]{"red", "green", "blue"}))

The operation for removing an entry from a stack is traditionally called: A. delete B. peek C. pop D. remove

C. pop

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) { ... }

C. public interface A<E, F> { ... }

Consider the following pseudocode: declare a stack of characters while ( there are more characters in the word to read ) { read a character push the character on the stack } while ( the stack is not empty ) { pop a character off the stack write the character to the screen } What is written to the screen for the input "carpets"? A. serc B. carpets C. steprac D. ccaarrppeettss

C. steprac

Suppose cursor refers to a node in a linked list (using the IntNode class with instance variables called data and link). What statement changes cursor so that it refers to the next node? A. cursor++; B. cursor = link; C. cursor += link; D. cursor = cursor.link;

D. cursor = cursor.link;

Suppose we have an array implementation of the stack class, with ten items in the stack stored at data[0] through data[9]. The CAPACITY is 42. Where does the push method place the new entry in the array? A. data[0] B. data[1] C. data[9] D. data[10]

D. data[10]

Suppose we have a circular array implementation of the queue class, with ten items in the queue stored at data[2] through data[11]. The current capacity is 42. Where does the insert method place the new entry in the array? A. data[1] B. data[2] C. data[11] D. data[12]

D. data[12]

The operation for adding an entry to a stack is traditionally called: A. add B. append C. insert D. push

D. push

Fill in the code in Comparable______ c = new Date(); A. <E> B. <?> C. <String> D. <Date>

D. <Date>

Which of the following applications may use a stack? A. A parentheses balancing program. B. Keeping track of local variables at run time. C. Syntax analyzer for a compiler. D. All of the above.

D. All of the above.

I have implemented the queue with a linked list, keeping track of a front node and a rear node with two reference variables. Which of these reference variables will change during an insertion into an EMPTY queue? A. Neither changes B. Only front changes. C. Only rear changes. D. Both change.

D. Both change.

If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed? A. ABCD B. ABDC C. DCAB D. DCBA

D. DCBA

Entries in a stack are "ordered". What is the meaning of this statement? A. A collection of Stacks can be sorted. B. Stack entries may be compared with the '<' operation. C. The entries must be stored in a linked list. D. There is a first entry, a second entry, and so on.

D. There is a first entry, a second entry, and so on.

Which of the following is an application of stack? A. finding factorial B. tower of Hanoi C. infix to postfix D. all of the above

D. all of the above

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. LinkedList C. Dictionary D. LinkedList

Dictionary

In the array version of the Stack class, which operations require linear time for their worst-case behavior? A. is_empty B. peek C. pop D. push when the stack is below capacity E. None of these operations require linear time.

E. None of these operations require linear time.

In the linked-list version of the Stack class, which operations require linear time for their worst-case behavior? A. is_empty B. peek C. pop D. push E. None of these operations require linear time.

E. None of these operations require linear time.

Write a recursive mathematical definition for computing 1+2+3+....+n for a positive integer

F(n) = 1; n = 1 F(n) = n + F(n-1); n > 1

A linked implementation of a stack adds and removes elements from the _______ of the linked list. • Front • Rear • Middle • None of the above

Front

A given set of n distinct numbers may be sorted by first building a binary search tree containing those numbers (inserting the numbers one by one), and then printing the numbers during a traversal. What is the big-O best-case running time for this sorting algorithm?

In the best case, the BST will be balanced, and inserting to a balanced BST of k nodes has cost log(k), so building the tree will cost: log(1) + log(2) + ... + log(n-1) = log (n-1)! which is O(n log n). Traversing the BST to print the nodes will have cost O(n). So the total cost is O(n log n).

A given set of n distinct numbers may be sorted by first building a binary search tree containing those numbers (inserting the numbers one by one), and then printing the numbers during a traversal. (a) [8 pts] What is the big-O worst-case running time for this sorting algorithm?

In the worst case, the BST will be a "stalk" (a linear list), and inserting to such a BST of k nodes has cost k, so building the tree will cost: 1 + 2 + ... + n-1 which is O(n2). Traversing the BST to print the nodes will have cost O(n). So the total cost is O(n2).

In the circular array version of the Queue class, which operations require linear time for their worst-case behavior? A. getFront B. insert when the capacity has not yet been reached C. isEmpty D. None of these operations require linear time.

None of these operations require linear time.

In the linked-list version of the Queue class, which operations require linear time for their worst-case behavior? A. getFront B. insert C. isEmpty D. None of these operations require linear time.

None of these operations require linear time.

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('*'); } }}

O ( n ^ 2)

What is the complexity of the following program in terms of n assuming that multiplication is a constant time operation double result = a; int i = 1; while (i <= n) { result = result * result; i *= 2; }

O(log n)

Which Growth function has the highest order? A. O(n log n) B. O(n2) C. O(2n) D. O(log n) E. O(n!)

O(n ! )

What is the complexity of the following program in terms of n? for (int k = 0; k < 10000; k++) { for (int i = 0; i < n+200; i++) { for (int j = 0; j < n- 25; j+=2) { System.out.print("a"); } } }

O(n^2)

I have implemented the queue with a linked list, keeping track of a front node and a rear node with two reference variables. Which of these reference variables will change during an insertion into a NONEMPTY queue? A. Neither changes B. Only front changes. C. Only rear changes. D. Both change.

Only rear changes.

Recursion is memory-intensive because: a. Recursive functions tend to declare many local variables. b. Previous function calls are still open when the function calls itself and the activation records of these previous calls still occupy space on the call stack. c. Many copies of the function code are created. d. It requires large data values.

Previous function calls are still open when the function calls itself and the activation records of these previous calls still occupy space on the call stack.

Errors? ArrayList<Date> dates = new ArrayList<Date>();dates.add(new Date());dates.add(newString());

Yes

Show the printout of the following code:public class Test { public static void main(String[] args) { Map map = new 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)); } }

[[123, "Steve Yao"], [111, "George Smith"], [222, "Steve Yao"]] [[111, "George Smith"], [123, "Steve Yao"], [222, "Steve Yao"]]

If you want to store non-duplicated objects in the order in which they are inserted, you should use ________. a) LinkedHashSet b) ArrayList c) HashSet d) LinkedList e) TreeSet

a) LinkedHashSet

Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.removeAll(s2), s1 is ________. a) [1, 5] b) [1, 2, 2, 3, 5, 6] c) [1, 2, 3, 5, 6] d) [2]

a) [1, 5]

*Which of the following methods are in the Collection interface? a) isEmpty() b) clear() c) size() d) getSize()

a) isEmpty() b) clear() c) size()

At most, how many comparisons are required to search a sorted vector of 1023 elements using the binary search algorithm? a. 10 b. 15 c. 20 d. 30

a. 10

A random access file is organized most like a(n): a. Array. b. Object. c. Class. d. Pointer.

a. Array.

What kind of linked list begins with a pointer to the first node, and each node contains a pointer to the next node, and the pointer in the last node points back to the first node? a. Circular, singly-linked list. b. Circular, doubly-linked list. c. Singly-linked list. d. Doubly-linked list.

a. Circular, singly-linked list.

In general, linked lists allow: a. Insertions and removals anywhere. b. Insertions and removals only at one end. c. Insertions at the back and removals from the front. d. None of the above.

a. Insertions and removals anywhere.

To add a new node, you need to start a process by first placing it as _______ and move it up to maintain the heap property. a. the last node in the heap b. the new root c. the right child of the root d. the left child of the root

a. the last node in the heap

Your program frequently tests whether a DVD is available in a set of sorted DVDs. which is the best solution? -store in a linked list and use binary search -store in a array list and use binary search -store in a array list and use sequential search -store in a linked list and use sequential search

array list with binary search

Which of the following is correct to sort the elements in a list lst? a) lst.sort() b) Collections.sort(lst) c) new LinkedList(new String[ ]{"red", "green", "blue"}) d) Arrays.sort(lst)

b) Collections.sort(lst)

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

b) List c) Set d) LinkedList e) ArrayList

The printout of the following code is ________. LinkedHashSet<String> set1 = new LinkedHashSet<String>(); set1.add("New York"); LinkedHashSet<String> set2 = (LinkedHashSet<String>)(set1.clone()); set1.add("Atlanta"); set2.add("Dallas"); System.out.println(set2); a) [New York, Atlanta, Dallas] b) [New York, Dallas] c) [New York] d) [New York, Atlanta]

b) [New York, Dallas]

To create a set that consists of string elements "red", "green", and "blue", use a) new HashSet(new String[ ]{"red", "green", "blue"}) b) new LinkedHashSet(Arrays.asList(new String[ ]{"red", "green", "blue"})) c) new Set(Arrays.asList(new String[ ]{"red", "green", "blue"})) d) new HashSet(Arrays.asList(new String[ ]{"red", "green", "blue"})) e) new HashSet({"red", "green", "blue"})

b) new LinkedHashSet(Arrays.asList(new String[ ]{"red", "green", "blue"})) d) new HashSet(Arrays.asList(new String[ ]{"red", "green", "blue"}))

The ________ method in the Queue interface retrieves and removes the head of this queue, or null if this queue is empty. a) remove() b) poll() c) element() d) peek()

b) poll()

Which data structure represents a waiting line and limits insertions to be made at the back of the data structure and limits removals to be made from the front? a. Stack. b. Queue. c. Binary tree. d. Linked list.

b. Queue.

Which of the following statements about stacks is incorrect? a. Stacks can be implemented using linked lists. b. Stacks are first-in, first-out (FIFO) data structures. c. New nodes can only be added to the top of the stack. d. The last node (at the bottom) of a stack has a null (0) link.

b. Stacks are first-in, first-out (FIFO) data structures.

To create a list to store integers, use a) ArrayList<Object> list = new ArrayList<Integer>(); b) ArrayList<Number> list = new ArrayList<Integer>(); c) ArrayList<Integer> list = new ArrayList<Integer>(); d) ArrayList<int> list = new ArrayList<int>();

c) ArrayList<Integer> list = new ArrayList<Integer>();

How many pointers are contained as data members in the nodes of a circular, doubly linked list of integers with five nodes? a. 5 b. 8 c. 10 d. 15

c. 10

An algorithm that requires __________ operations to complete its task on n data elements is said to have a linear runtime. a. n 3 + 9 b. 3 n 2 + 3 n + 2 c. 2 n + 1 d. 6

c. 2 n + 1

: Select the incorrect statement. Binary search trees (regardless of the order in which the values are inserted into the tree): a. Always have multiple links per node. b. Can be sorted efficiently. c. Always have the same shape for a particular set of data. d. Are nonlinear data structures

c. Always have the same shape for a particular set of data.

Which of the following is not a dynamic data structure? a. Linked list. b. Stack. c. Array. d. Binary tree.

c. Array.

The total number of elements that can be stored in a string without increasing its current amount of allocated memory is called its: a. Size. b. Length. c. Capacity. d. Maximum size.

c. Capacity.

The time complexity for finding an element in a binary search tree is _________. a. O(logn) b. O(nlogn) c. O(n) d. O(1)

c. O(n)

The ________ is to visit the left subtree of the current node first, then the current node itself, and finally the right subtree of the current node. a. postorder traversal b. preorder traversal c. inorder traversal d. breadth-first traversal

c. inorder traversal

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

d) Arrays.asList(new String[ ]{"red", "green", "blue"})

Which of the data types below could be used to store elements in their natural order based on the compareTo method. a) Collection b) HashSet c) Set d) TreeSet e) LinkedHashSet

d) TreeSet

Suppose set s1 is [1, 2, 5] and list L2 is [2, 3, 6]. After s1.addAll(L2), s1 is ________. a) [1, 2, 2, 3, 5, 6] b) [2] c) [1, 5] d) [1, 2, 3, 5, 6] e) runtime exception

d) [1, 2, 3, 5, 6]

To get an iterator from a set, you may use the ________ method. a) iterators b) findIterator c) getIterator d) iterator

d) iterator

Which of the following is correct to perform the set intersection of two sets s1 and s2? a) s1.intersect(s2) b) s1.intersection(s2) c) s1.join(s2) d) s1.retainAll(s2)

d) s1.retainAll(s2)

What value does function mystery return when called with a value of 4? int mystery ( int number ) { if ( number <= 1 ) return 1; else return number * mystery( number - 1 ); } a. 0. b. 1. c. 4. d. 24.

d. 24.

Linear search is highly inefficient compared to binary search when dealing with: a. Small, unsorted arrays. b. Small, sorted arrays. c. Large, unsorted arrays. d. Large, sorted arrays.

d. Large, sorted arrays.

If the numbers 23, 16, and 34 are added to an empty binary search tree in that order, in order to rebalance it, you need a(n) _______ .

left rotation 16 \ 23 \34 the balanced factor of root node 16 is -2. Tree is balanced, so we need to do a single left rotation to make it balanced 23 / \ 16 34

To declare a class named A with a generic type, use a) public class A(E) { ... } b) public class A(E, F) { ... } c) public class A<E, F> { ... } d) public class A<E> { ... }

public class A<E> { ... }

When there is an addition to a node's left subtree that creates an unbalanced search tree, you can correct it with a(n) _________ .

right rotation, the reason is the unbalancing of a tree by left subtree can be resolved by a single right rotation

What is set1 after executing set1.clear()?

set1: []

Suppose that set1 is a set that contains the strings "red", "yellow", "green", and that set2 is another collection that contains the strings "red", "yellow", 1 "blue". Answer the following questions: (All the questions are independent) What are set1 and set2 after executing set1.addAll(set2)?

set1: [red, yellow, green, blue] set2: [red, yellow, blue]


Set pelajaran terkait

MGMT 498 SB Assignment Chapter 9

View Set

Personal and Healthy Relationships + Healthy Eating

View Set