CS - 2013: 20. Java: Chapter 20 - Lists, Stacks, Queues, And Priority Queues

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

Which method can you use to perform binary search for elements in an ArrayList or a LinkedList? 1. Collections.binary(list, key) 2. Arrays.binary(Object[], key)

1

Which method can you use to sort an array of strings? 1. Arrays.sort(Object[]) 2. Collections.sort(list)

1

Which of the following static methods in the Collections class are for lists? (most common) 1. sort 2. disjoint 3. min 4. binarySearch 5. max 6. reverse 7. shuffle 8. frequency

1, 4, 6, 7 (Note that all the methods for collections are also for lists, because lists are collections. )

Which method can you use to perform binary search for an array of strings? 1. Collections.binary(list, key) 2. Arrays.binary(Object[], key)

2

Which method can you use to sort the elements in an ArrayList or a LinkedList? 1. Arrays.sort(Object[]) 2. Collections.sort(list)

2

Which of the following static methods in the Collections class are for collections? 1. sort 2. disjoint 3. min 4. binarySearch 5. max 6. reverse 7. shuffle 8. frequency

2, 3, 5, 8

Vector is a subclass of ....., and Stack is a subclass of ..... in the Java API

AbstractList, Vector

Two types of lists are supported: ArrayList and LinkedList. ...... is a resizable-array implementation of the List interface. All the methods in ArrayList are defined in ..... ........ is a linked-list implementation of the List interface. In addition to implementing the List interface, this class provides the methods for retrieving, inserting, and removing elements from both ....... of the list.

ArrayList, List, LinkedList, ends

Suppose a class B is defined as below: public class B implements Comparator<B1> { public int compare(B1 o1, B1 o2) { return an integer; } } -> How do you invoke the sort method to sort an array x of objects of the type B1?

Arrays.sort(x, new B());

Write a statement to find the largest element in an array of comparable objects.

Collections.max(Arrays.asList(arrayObject))

Suppose a class A is defined as below: public class A implements Comparable<A> { public int compareTo(A o) { return an integer; } } -> Which interface is used to implement class A?

Comparable

........ is used to compare the objects of the class that implement Comparable. ....... can be used to compare the objects of a class that doesn't implement Comparable. Comparing elements using the Comparable interface is referred to as comparing using ......, and comparing elements using the Comparator interface is referred to as comparing using ........

Comparable, Comparator, natural order, comparator

Suppose a class B is defined as below: public class B implements Comparator<B1> { public int compare(B1 o1, B1 o2) { return an integer; } } -> Which interface is used to implement class B?

Comparator

Each collection is ........ You can obtain its Iterator object to traverse all the elements in the collection.

Iterable

The Collection interface extends the ......... interface. You can obtain an iterator from a collection using the ............ method.

Iterable, iterator()

......... is a classic design pattern for walking through a data structure without having to expose the details of how data is stored in the data structure.

Iterator

Which list should you use to insert and delete elements at the beginning of a list?

LinkedList

ArrayList and LinkedList operate similarly. The critical difference between them pertains to internal implementation, which affects their performance. ....... is efficient for inserting and removing elements at the beginning of the list, while ..... is efficient for retrieving elements and for adding and removing elements from the end of the list

LinkedList, ArrayList

The methods getFirst, getLast, addFirst, addLast are in ......, but not in .......

LinkedList, ArrayList

The ...... interface extends the Collection interface and defines a collection for storing elements in a sequential order. To create a list, use one of its two concrete classes: ........ or ............

List, ArrayList, LinkedList

. All the concrete classes except .......... in the Java Collections Framework implement the ........ and ....... interfaces. Thus, their instances can be cloned and serialized.

PriorityQueue, Cloneable, Serializable

UnsupportedOperationException is a subclass of ......

RuntimeException

The Java Collections Framework supports sets, lists, queues, and maps. They are defined in the interfaces ....., ....., ........., and ...... (respectively)

Set, List, Queue, Map

If a method has no meaning in the subclass, we can throw an .......

UnsupportedOperationException

Stack is a subclass of ...... The Stack class represents a LIFO stack of objects. The elements are accessed only from the ..... of the stack. You can retrieve, insert, or remove an element from the top of the stack. To add a new element to a stack, use the .... method. To remove an element from the top of the stack, use the method ...... To find a stack size, use the ...... method.

Vector, top, push, pop, size()

Show the output of the following code: import java.util.*; public class Test { public static void main(String[] args) { List<String> list = Arrays.asList("yellow", "red", "green", "blue"); Collections.reverse(list); System.out.print(list + ", "); Collection<String> c1 = Arrays.asList("red", "cyan"); Collection<String> c2 = Arrays.asList("red", "blue"); Collection<String> c3 = Arrays.asList("pink", "tan"); System.out.print(Collections.disjoint(c1, c2) + ", "); System.out.println(Collections.disjoint(c1, c3)); }

[blue, green, red, yellow], false, true

Show the output of the following code: import java.util.*; public class Test { public static void main(String[] args) { List<String> list1 = Arrays.asList("yellow", "red", "green", "blue"); List<String> list2 = Arrays.asList("white", "black"); Collections.copy(list1, list2); System.out.print(list1 +", "); Collection<String> collection = Arrays.asList("red", "cyan", "red"); System.out.println(Collections.frequency(collection, "red")); }

[white, black, green, blue], 2

What method do you use to add all the elements from one collection to another collection?

addAll(Collection c).

The ArrayList class and the LinkedList class are two concrete implementations of the List interface. ArrayList stores elements in an ....... LinkedList stores elements in a ........ Which of the two classes you use depends on your specific needs. If you need to support random access through an index without inserting or removing elements at the beginning of the list, ........... offers the most efficient collection. If, however, your application requires the insertion or deletion of elements at the beginning of the list, you should choose .......

array, linked list, ArrayList, LinkedList

Which of the following statements create a list from an array of objects? 1. new ArrayList(Arrays.asList(arrayObject)) 2. new LinkedList(Arrays.asList(arrayObject)).

both of them

A list stores an ordered ...... of elements.

collection

The Comparable interface contains the ..... method and Comparator interface contains the ..... method and ..... method. Normally, if the objects of a class have natural order (e.g., String, Date), let the class implement the ....... interface. The ...... interface is more flexible in the sense that it enables you to define a new class that contains the compare(Object, Object) method to compare two objects of other classes.

compareTo, compare, equals, Comparable, Comparator

How do you create a priority queue for integers? -> Use the ....... of PriorityQueue

constructors

Suppose that list1= {red, yellow, green} and that list2 = {red, yellow, blue} => What is list1 after executing list1.clear()?

empty

A queue is a FIFO data structure. Elements are appended to the .... of the queue and are removed from the ..... of the queue.

end, beginning

Deque interface supports element insertion and removal at both ..... The name deque is short for "double-ended queue" and is usually pronounced "deck."

ends

True or False? "The Collections class doesn't contains any static methods to perform common operations in a collection and a list"

false

True or False? "There is no another way to compare the objects of a class that doesn't implement Comparable."

false

True or False? "Vector is exactly the same as ArrayList."

false

In a priority queue, the element with the highest priority is removed .......

first

ArrayList is efficient for adding and removing elements ..... of the list. LinkedList is efficient for adding and removing elements ....... in the list.

from the end, anywhere

Suppose that list1= {red, yellow, green} and that list2 = {red, yellow, blue} => What is list1 after executing list1.removeAll(list2)?

green

To allow duplicate elements to be stored in a collection, you need to use a ...... A list not only can store duplicate elements but also allows the user to specify where they are stored. The user can access elements by an ......

list, index

Suppose a class B is defined as below: public class B implements Comparator<B1> { public int compare(B1 o1, B1 o2) { return an integer; } } -> How do you invoke the sort method to sort a list of objects of the type B1?

list.sort(new B())

By default, how are elements ordered in a priority queue? -> By default, the elements in a priority queue are ordered in their ...... order using the ...... method in the Comparable interface

natural, compareTo

What method do you use to obtain an element in the collection from an iterator?

next()

Are all the methods in ArrayList also in LinkedList?

no

When using a foreach loop to traverse all elements in a collection, do you need to use the next() or hasNext() methods in an iterator?

no (They are implicitly used in a foreach loop.)

The Queue interface represents a queue. The PriorityQueue class implements Queue for a ........

priority queue.

Suppose that list1= {red, yellow, green} and that list2 = {red, yellow, blue} => What is list1 after executing list1.retainAll(list2)?

red, yellow

Suppose that list1= {red, yellow, green} and that list2 = {red, yellow, blue} => What is list1 after executing list1.remove(list2)?

red, yellow, green

Suppose that list1= {red, yellow, green} and that list2 = {red, yellow, blue} => What is list1 after executing list1.add(list2)?

red, yellow, green, [red, yellow, blue]

Suppose that list1= {red, yellow, green} and that list2 = {red, yellow, blue} => What is list1 after executing list1.addAll(list2)?

red, yellow, green, red, yellow, blue

The "new PriorityQueue(initialCapacity, Collections.reverseOrder())." statement creates a priority queue that ...... the natural order of the elements.

reverses

Vector is the same as ArrayList, except that it contains ........ methods for accessing and modifying the vector.

synchronized

All the methods in ArrayList also in LinkedList except the ....... method

trimToSize()

True or False? "A convenience class is an abstract class that partially implements an interface. The Java Collections Framework defines interfaces, convenience abstract classes, and concrete classes."

true

True or False? "A list can hold identical elements. Thus., integer 1 is stored twice in the list."

true

True or False? "Because the LinkedList class implements the Deque interface, which extends the Queue interface, we can use LinkedList to create a queue."

true

True or False? "Comparator can be used to compare the objects of a class that doesn't implement Comparable"

true

True or False? "Comparator can be used to compare the objects of a class that doesn't implement Comparable."

true

True or False? "LinkedList is ideal for queue operations because it is efficient for inserting and removing elements from both ends of a list."

true

True or False? "The Collections class contains static methods to perform common operations in a collection and a list"

true

True or False? "Use the add or remove method to add or remove elements from a list."

true

How do you traverse a list in both directions?

use the listIterator()

Since Vector implements List, you can use the methods in List to add, remove elements from a ....., and use the .... method to find the size of a vector. To create a vector, use either its ......

vector, size(), constructors

Are all the methods in the Collections class static?

yes

Can you use a foreach loop to traverse the elements in any instance of Collection?

yes

Can you use the forEach method on any instance of Collection?

yes

Does LinkedList implement Queue?

yes

Is the element with the least value assigned the highest priority in a priority queue?

yes

Suppose a class A is defined as below: public class A implements Comparable<A> { public int compareTo(A o) { return an integer; } } -> Are two instances of class A comparable?

yes

Can a collection object be cloned and serialized?

yes (The concrete classes of Set, List, and Map implements the clone() method in the Cloneable interface.)

if the elements' classes do not implement the Comparable interface, can these elements be compared?

yes (You can define a comparator to compare the elements of different classes)

Is java.util.Queue a subinterface of java.util.Collection, java.util.Set, or java.util.List?

yes (because java.util.Queue is a subinterface of java.util.Collection)


Kaugnay na mga set ng pag-aaral

Chapter Exam (2) - Legal Concepts of the Insurance Contract

View Set

California Hunter Safety - Unit 6 Quiz

View Set

SHRM Test, SHRM, SHRM Flash Cards, Human Resources Test 1, HR TEST 1, People, SHRM, SHRM - RR - Multiple Choice, Foundations of HR - Midterm, SHRM-CP Workplace (15%) *

View Set