Chapter 22

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

When you create an ArrayList using ArrayList<String> x = new ArrayList<>(2), ________ A. two elements are created in the array list. B. no elements are currently in the array list. C. the array list size is currently 2. D. the array list capacity is currently 2.

A & B

Which of the following statements are true? A. A PriorityQueue orders its elements according to their natural ordering using the Comparable interface if no Comparator is specified. B. A PriorityQueue orders its elements according to the Comparator if a Comparator is specified in the constructor. C. The priority of a PriorityQueue cannot be changed once a PriorityQueue is created. D. The priority of a PriorityQueue cannot be reversed once a PriorityQueue is created.

A and B

Which of the following statements are true? A. An ArrayList can grow automatically. B. An ArrayList can shrink automatically. C. You can reduce the capacity of an ArrayList by invoking the trimToSize() method on the list. D. You can reduce the capacity of a LinkedList by invoking the trimToSize() method on the list.

A and B

You can use the methods in the Collections class to A. find the maximum object in a collection based on the compareTo method. B. find the maximum object in a collection using a Comparator object. C. sort a collection. D. shuffle a collection. E. do a binary search on a collection.

A and B

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.Queue interface. C. java.util.HashSet implements the java.util.Queue interface. D. java.util.LinkedHashSet implements the java.util.Queue interface. E. java.util.PriorityQueue implements the java.util.Queue interface.

A and E

Suppose list1 is an ArrayList and list2 is a LinkedList. Both contains 1 million double values. Analyze the following code: 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); 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.

A is the answer. ArrayList run faster than linkedList

Which of the following statements are correct. A. When you create an array using new int[10], an array object is created with ten integers of value 0. B. When you create an array using new int[10], an array object is created with no values in the array. C. When you create an ArrayList using new ArrayList(), an ArrayList object is created with no elements in the ArrayList object. D. When you create an array using int[] x = new int[10], x.length() is 10. E. When you create an array using ArrayList x = new ArrayList(10), x.size() is 10.

A,C,D

Which of the following are true? A. You can insert an element anywhere is an arraylist. B. You can insert an element anywhere is a linked list. C. You can use a linked list to improve efficiency for adding and removing elements at the beginning of a list. D. You should use an array list if your application does not require adding and removing elements at the beginning of a list.

All of them

Which of the following methods are in java.util.List? A. add(int index, E element) B. get(int index) C. set(int index, E element) D. remove(int index) E. subList(int fromIndex, int toIndex)

All of them

Which of the following statements are true? A. The Collection interface is the root interface for manipulating a collection of objects. B. The Collection interface provides the basic operations for adding and removing elements in a collection. C. The AbstractCollection class is a convenience class that provides a partial implementation for the Collection interface. D. Some of the methods in the Collection interface cannot be implemented in the concrete subclass. In this case, the method would throw java.lang.UnsupportedOperationException, a subclass of RuntimeException. E. All interfaces and classes in the Collections framework are declared using generic type since JDK 1.5.

All of them

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

All of them

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. The AbstractList class provides a partial implementation for the List interface. C. ArrayList is a concrete implementation of List using an array. D. LinkedList is a concrete implementation of List using a linked list. LinkedList contains all the methods in List and additionally new methods for manipulating a linked list. E. ListIterator is a subinterface of Iterator and it provides the methods to support bi-directional traversal of a list.

All of them

Suppose list is a LinkedList that contains 1 million int values. Analyze the following code: A: for (int i = 0; i < list.size(); i++) sum += list.get(i); B: for (int i: list) sum += i; 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.

Answer is B because code fragment B uses an iterator to traverse the elements in a linked list.

Suppose a list contains ["red", "green", "red", "green"]. What is the list after the following code? list.remove("red"); A. ["red", "green", "red", "green"] B. ["green", "red", "green"] C. ["green", "green"] D. ["red", "green", "green"]

B

Suppose a list contains ["red", "red", "red", "red"]. 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); } A. ["red", "red", "red"] B. ["red", "red"] C. ["red"] D. []

B

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

B

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

B

import java.util.*; public class Test { public static void main(String[] args) { PriorityQueue<Integer> queue = new PriorityQueue<>( 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

Which method do you use to remove an element from a set or list named x? A. x.delete(element) B. x.remove(element) C. x.deletes(element) D. x.removes(element) E. None of the above

B, x.remove(element);

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

BCD

java.util.Vector is a subtype of __________. A. java.util.ArrayList B. java.util.LinkedList C. java.util.AbstractList D. java.util.Vector E. java.util.List

C and E

java.util.Stack is a subclass of __________. A. java.util.ArrayList B. java.util.LinkedList C. java.util.AbstractList D. java.util.Vector E. java.util.List

C,D,E

28 Suppose a list contains {"red", "red", "red", "red"}. What is the list after the following code? String element = "red"; for (int i = list.size() - 1; i >= 0; i--) if (list.get(i).equals(element)) list.remove(element); A. ["red", "red", "red"] B. ["red", "red"] C. ["red"] D. []

D

Analyze the following code: import java.util.*; public class Test { public static void main(String[] args) { PriorityQueue<Integer> queue = new PriorityQueue<>( Arrays.asList(60, 10, 50, 30, 40, 20)); for (int i: queue) System.out.print(i + " "); } } 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

D

Suppose a list contains ["red", "red", "red", "red"]. 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--; } A. ["red", "red", "red"] B. ["red", "red"] C. ["red"] D. []

D

Which of the following statements are true? A. Collections.shuffle(list) returns a new list while the original list is not changed. B. Collections.reverse(list) returns a new list while the original list is not changed. C. Collections.sort(list) returns a new list while the original list is not changed. D. Collections.nCopies(int, Object) returns a new list that consists of n copies of the object.

D

What is list after the following code is executed? ArrayList<Integer> 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, 4]

D Explanation: The ArrayList class has two overloaded remove method remove(Object) and remove(int index). The latter is invoked for list.remove(2) to remove the element in the list at index 2.

Which method do you use to find the number of elements in a set or list named x? A. x.length() B. x.count() C. x.counts() D. x.size() E. x.sizes()

D, x.size();

Which method do you use to test if an element is in a set or list named x? A. (element instanceof List) || (element instanceof Set) B. x.in(element) C. x.contain(element) D. x.contains(element) E. x.include(element)

D. The contains method defined in the Collection interface checks if an element is in the collection (set or list)

The iterator() method is defined in the __________ interface.

Iterable

You can use a for-each loop to traverse all elements in a container object that implements _____.

Iterable

The iterator() method returns an instance of the __________ interface.

Iterator

All the concrete classes in the Java Collections Framework implement

The cloneable and serializable interface

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

iterator();


Kaugnay na mga set ng pag-aaral

Biology Semester Review (Part 1)

View Set

RR - Cardiology 1, RR - Cardiology 2

View Set

Health Assessment: Chapter 10 - Head, Eyes, Ears, Nose, and Throat

View Set

Micro Econ 1123- Midterms 2, 3 & 4- Pallab Ghosh (Spring 2022)

View Set

Math 1 Unit 4- Vertex Edge Graphs Vocabulary

View Set

Romeo and Juliet Unit Test Review

View Set

5.11 Switch Security and Attacks

View Set

Principles of Computing Final; Ch. 11 & 17

View Set