JAVA test 2

Ace your homework & exams now with Quizwiz!

Which of the following statements are true? A. A heap is a complete binary tree. B. A heap is a full binary tree. C. Each node is greater than or equal to any of its children. D. A binary tree is complete if every level of the tree is full except that the last level may not be full and all the leaves on the last level are placed left-most.

A. C. D.

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

A. Collections.sort(lst)

Suppose a list contains {"red", "green", "red", "green"}. What is the list after the following code? String element = "red"; for (int i = 0; i < list.size(); i++) if (list.get(i).equals(element)) {list.remove(element);i--;}

{"green", "green"}

Suppose a heap is stored in an array list as follows: {100, 55, 92, 23, 33, 81}. After inserting 103, what is the content of the array list?

{103, 55, 100, 23, 33, 81, 92}

You can traverse the elements in a BST using a for-each loop and a Stack.

True

A Circular Linked List uses NULL to check for ending an iteration.

False

public static <T extends Comparable<T>> void selectionSort(T[] ar) {for (int i = 0; i < ar.length - 1; ++i) {for (int j = i + 1; j < ar.length; ++j) {if (ar[i].compareTo(ar[j]) > 0) {swap(ar, i, j); if ( i == 3 && j == 4) printArray( ar);}}}} private static <T> void swap(T[] ar, int i, int j) {T temp = ar[i]; ar[i] = ar[j]; ar[j] = temp;} private static <T> void printArray(T[] ar) {for (T t : ar) System.out.print(t.toString() + " "); System.out.println();} public static void main(String... ar) {Integer[] ar3 = {10, 2, 20, 1, 12, 8, 3, 66, 1}; selectionSort( ar3 ); }

1 1 2 12 20 10 8 66 3

For a sorted list of 1024 elements, a binary search takes at most ________ comparisons.

11

Suppose the keys 3, 4, 45, 21, 92, 12 are inserted into a BST in this order. What is the preorder traversal of the elements?

3 4 45 21 12 92

________ is a data structure to store data in sequential order.

A list

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

A. After all the four entries are added to the map, "123" is a key that corresponds to the value "Steve Yao".

In the implementation of BST, which of the following are true? A. Node is defined as an inner class inside BST. B. Node has a property named left that links to the left subtree and a property named right that links to the right subtree and a property named right C. BST contains a property named root. If tree is empty, root is null.

A. B. C.

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

A. B. C. E.

Which of the following are true? A. Since deletions are made at the beginning of the list, it is more efficient to implement a queue using a linked list than an array list. B. A queue represents a waiting list. A queue can be viewed as a special type of list, where the elements are inserted into the end (tail) of the queue, and are accessed and deleted from the beginning (head) of the queue. C. Since the insertion and deletion operations on a stack are made only at the end of the stack, using an array list to implement a stack is more efficient than a linked list. D. A stack can be viewed as a special type of list, where the elements are accessed, inserted, and deleted only from the end, called the top, of the stack.

A. Since deletions are made at the beginning of the list, it is more efficient to implement a queue using a linked list than an array list. B. A queue represents a waiting list. A queue can be viewed as a special type of list, where the elements are inserted into the end (tail) of the queue, and are accessed and deleted from the beginning (head) of the queue. D. A stack can be viewed as a special type of list, where the elements are accessed, inserted, and deleted only from the end, called the top, of the stack.

Analyze the following code: import java.util.*; public class Test {public static void main(String[] args) throws Exception {TreeSet 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 cannot compile, because the last() method is not defined in Set. D. The program may display Red, Blue, or Green. E. The program displays Green.

A. The program displays Red.

Which of the following methods are in the Collection interface? A. deleteAll(c: Collection>): boolean B. remove(o: Object): boolean c. delete(o: E): boolean D. removeAll(c: Collection>): boolean

A. remove(o: Object): boolean D. removeAll(c: Collection>): boolean

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

All are true

Which of the following statements are true? A. A Comparable object can compare this object with the other object. B. The Comparable interface contains the compareTo method with the signature "public int compareTo(E)". C. The Comparator interface contains the compare method with the signature "public int compare(E, E)". D. A Comparator object contains the compare method that compares two objects.

All are true

Which of the following statements are true? A. java.util.List inherits all the methods from java.util.Collection. Additionally, it contains new methods for manipulating a list. B. LinkedList is a concrete implementation of List using a linked list. LinkedList contains all the methods in List and additional new methods for manipulating a linked list. C. The AbstractList class provides a partial implementation for the List interface. D. ListIterator is a subinterface of Iterator and it provides the methods to support bi-directional traversal of a list. E. ArrayList is a concrete implementation of List using an array.

All are true

Which of the following is correct to create a list from an array? A. new ArrayList(new String[]{"red", "green", "blue"}) B. Arrays.asList(new String[]{"red", "green", "blue"}) C. new List({"red", "green", "blue"}) D. new LinkedList(new String[]{"red", "green", "blue"}) E. new List(new String[]{"red", "green", "blue"})

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

Suppose ArrayList x contains three strings [Beijing, Singapore, Tokyo]. Which of the following methods will cause runtime errors? A. x.get(2) B. x.get(3) C. x.size() D. x.remove(3) E. x.set(3, "New York");

B. D. E.

Which data type should you use if you want to store duplicate elements and be able to insert or delete elements anywhere efficiently? A. Vector B. LinkedList C. Stack D. ArrayList E. Set

B. LinkedList

Which of the following statements are true? A. java.util.ArrayList implements the java.util.Queue interface. B. java.util.LinkedHashSet implements the java.util.Queue interface. C. java.util.PriorityQueue implements the java.util.Queue interface. D. java.util.LinkedList implements the java.util.Queue interface. E. java.util.HashSet implements the java.util.Queue interface.

C. java.util.PriorityQueue implements the java.util.Queue interface. D. java.util.LinkedList implements the java.util.Queue interface.

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

C. s1.addAll(s2)

Analyzing algorithm efficiency is ________. A. to estimate their execution time B. to measure their actual execution time C. to estimate their growth function

C. to estimate their growth function

Suppose list1 is an ArrayList and list2 is a LinkedList. Both contains 1 million double values. Analyze the following code speed: A:for (int i = 0; i < list1.size(); i++)sum += list1.get(i); B:for (int i = 0; i < list2.size(); i++)sum += list2.get(i);

Code fragment A runs faster than code fragment B.

What is the number of iterations in the following loop? int count = 5; while (count < n) {count = count + 3;} A. n / 3 - 1 B. n - 5 C. n - 3 D. (n - 5) / 3

D. (n - 5) / 3

A Single LinkedList is more efficient than ArrayList for the following operations: A. Insert/delete an element in the beginning of the list B. Retrieve an element given the index. C. Insert/delete an element in the middle of the list. D. Insert/delete an element at the end of the list.

Insert/delete an element in the beginning of the list

The worst time for searching a BST tree that is NOT skewed but complete is

O(logn )

The time complexity for the Euclid's algorithm is ________.

O(logn)

The best-time complexity for insertion sort is ________.

O(n)

The time complexity for searching an element in a binary search tree is ________.

O(n)

The time to merge two sorted lists of size n is ________.

O(n)

The time complexity for the insertion sort algorithm in the text is ________.

O(n^2)

Which data structure is appropriate to store patients in an emergency room?

Priority Queue

Which of the data types below do not allow duplicates?LinkedList Stack Vector Set List

Set

A heap is represented using an array. Is the array {64 42 59 32 39 44} a heap?

Yes

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

[1, 2, 4, 5]

Suppose list list1 is [1, 2, 5] and list list2 is [2, 3, 6]. After list1.addAll(list2), list1 is ________.

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

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

[1, 5]

What is the output for the following code? import java.util.*; public class Test {public static void main(String[] args) {Set 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;} public int hashCode() {return r;}}

[1]

public static <T extends Comparable<T>> void heapSort( T[] ar){ ArrayList<T> list = new ArrayList<T>(); for ( int i=0; i < ar.length; ++i) {add(list, ar[i]); if ( i == 5 )System.out.println( list);}} public static <T extends Comparable<T>> void add( ArrayList<T> list , T t ) {list.add(t); if ( list.size() == 1 ) return; int curentIndex = list.size()-1; while ( curentIndex > 0 &&list.get(curentIndex).compareTo(list.get ( (curentIndex-1 ) / 2) ) > 0 ) {T temp = list.get( (curentIndex - 1) / 2); list.set( (curentIndex - 1) / 2, list.get(curentIndex)); list.set( curentIndex , temp);curentIndex = (curentIndex - 1) / 2;}} public static void main(String... ar) {Integer[] ar3 ={ 10, 2, 20, 1, 12, 8, 3, 66, 1 }; heapSort( ar3); }

[20, 12, 10, 1, 2, 8]

A ________ (with no duplicate elements) has the property that for every node in the tree the value of any node in its left subtree is less than the value of the node and the value of any node in its right subtree is greater than the value of the node.

binary search tree

For an instance of Collection, you can obtain its iterator using ________.

c.iterator()

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.

inorder traversal

The ________ method in the Queue interface retrieves and removes the head of this queue, or null if this queue is empty.

poll()

All the concrete classes in the Java Collections Framework implement ________.

the Serializable interface


Related study sets

APCSP College Board Questions For Midterm

View Set

Ch2 - Comm & Cust. Service Skill

View Set

LearningCurve: 8e. Resistance to Persuasion

View Set

Module 4: Time and Stress Management

View Set

Chapter 10: Director's Duties and Liabilities

View Set