Deck 19: Array-Based Lists

Full screen (f)
exit full mode
Question
The boolean contains(E element)method searches a ArrayList for a given element.A correct and efficient implementation of this method

A) throws an exception if the element is not found in the list
B) uses binary search to locate the element
C) uses sequential search to locate the element
D) returns 0 if the element is not found in the list
Use Space or
up arrow
down arrow
to flip the card.
Question
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
int nElements;
to represent the array of elements,and the number of elements currently stored.Show the code for a constructor that creates a list with a default capacity of 100.
Question
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
int nElements;
to represent the array of elements,and the number of elements currently stored.Show the code for a method String set(int index,String str)that sets the element at the given index to str and returns the element previously stored there.
Question
If a new element is added to an ArrayList whose internal array if already full,

A) the add method throws an exception
B) A new,bigger internal array is created,the elements are moved to the new array,the old internal array is deleted,and then the new element is added
C) the new element is not added,and -1 is returned
D) the new element is not added,and null is returned
Question
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
int nElements;
to represent the array of elements,and the number of elements currently stored.Show the code for a method void clear()that removes all elements from the list.
Question
What is the difference between the Iterator and Iterable interfaces? How would you use these two interfaces if you had to write a collection class?
Question
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
int nElements;
to represent the array of elements,and the number of elements currently stored.Show the code for a method boolean empty()that determines whether the list is empty.
Question
The int indexOf(Object o)method of the List interface

A) searches a list for the occurrence of an object and returns its index
B) adds an object to a list and returns the index of the newly added object
C) uses binary search to locate the given object,and then returns its index
D) None of the above
Question
A constructor for an array-based list takes an integer parameter,to be used as the capacity of the internal array of the list.Which exception should the constructor throw if its parameter is zero or negative?

A) IllegalArgumentException
B) IllegalStateException
C) RuntimeException
D) NullPointerException
Question
The difference between an array and an ArrayList is

A) an ArrayList uses a LinkedList to hold its elements,an array does not.
B) an array keeps track of its length while an ArrayList does not track comparable information
C) arrays have a fixed size and cannot grow to accommodate more elements
D) All of the above
Question
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
int nElements;
to represent the array of elements,and the number of elements currently stored.Show the code for a method int size( )that returns the current size of the list.
Question
The E get(int index)method of the List interface should throw an exception if

A) the index passed to it is negative
B) the index passed to it nonnegative
C) the index passed to it is negative or greater or equal to the size of the list
D) the index passed to is negative or greater than the size of the list
Question
Why does the Java Collections Framework define a List as an interface rather than a class?

A) To be consistent with earlier versions of Java
B) To allow different types of concrete classes,with different performance characteristics to implement the interface
C) To allow the use of generic types
D) All of the above
Question
A new element is added to an ArrayList object at index k.Assuming the list has size s and does not have to be resized,

A) the elements at current positions 0..k must be moved toward the beginning of the list
B) the elements at current positions k..s-1 must be moved toward the end of the array
C) the elements at current positions k..s must be moved toward the end of the array
D) the element at position k is overwritten
Question
An ArrayList is so called because

A) you can use array subscript notation to work with the ArrayList
B) it is implemented as a class that uses an internal array to hold the elements of the list
C) you can pass it as a parameter to any method that expects an array
D) All of the above
Question
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
Int nElements;
To represent the array of elements,and the number of elements currently stored.Assuming there is space
In the internal array,the code for adding a new item str to the end of the list is

A) list.add(str);
B) list[nElements] = str;
C) list[nElements] = str;nElements++;
D) nElements ++;list[nElements] = str;
Question
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
int nElements;
to represent the array of elements,and the number of elements currently stored.Show the code for a method String remove(int index)that removes and returns the element at the given index.The method should throw an appropriately named exception if it cannot return the requested element.
Question
The size of an array-based list such as ArrayList

A) is the number of bytes of memory that the list can hold
B) is the number of elements that are currently stored in the list
C) is the length of its internal array
D) None of the above
Question
The position of an item within a list is called its

A) index
B) rank
C) level
D) number
Question
The capacity of an array-based list such as ArrayList

A) is the number of bytes of memory that the list can hold
B) is the number of elements that are currently stored in the list
C) is the size of its internal array
D) None of the above
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/20
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 19: Array-Based Lists
1
The boolean contains(E element)method searches a ArrayList for a given element.A correct and efficient implementation of this method

A) throws an exception if the element is not found in the list
B) uses binary search to locate the element
C) uses sequential search to locate the element
D) returns 0 if the element is not found in the list
C
2
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
int nElements;
to represent the array of elements,and the number of elements currently stored.Show the code for a constructor that creates a list with a default capacity of 100.
list = new String[100];
nElements = 0;
3
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
int nElements;
to represent the array of elements,and the number of elements currently stored.Show the code for a method String set(int index,String str)that sets the element at the given index to str and returns the element previously stored there.
String set(int index,String str)
{
if (index < 0 || index >= nElements)
throw NoSuchElementException();
String retValue = list[index];
list[index] = str;
return retValue;
}
4
If a new element is added to an ArrayList whose internal array if already full,

A) the add method throws an exception
B) A new,bigger internal array is created,the elements are moved to the new array,the old internal array is deleted,and then the new element is added
C) the new element is not added,and -1 is returned
D) the new element is not added,and null is returned
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
5
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
int nElements;
to represent the array of elements,and the number of elements currently stored.Show the code for a method void clear()that removes all elements from the list.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
6
What is the difference between the Iterator and Iterable interfaces? How would you use these two interfaces if you had to write a collection class?
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
7
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
int nElements;
to represent the array of elements,and the number of elements currently stored.Show the code for a method boolean empty()that determines whether the list is empty.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
8
The int indexOf(Object o)method of the List interface

A) searches a list for the occurrence of an object and returns its index
B) adds an object to a list and returns the index of the newly added object
C) uses binary search to locate the given object,and then returns its index
D) None of the above
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
9
A constructor for an array-based list takes an integer parameter,to be used as the capacity of the internal array of the list.Which exception should the constructor throw if its parameter is zero or negative?

A) IllegalArgumentException
B) IllegalStateException
C) RuntimeException
D) NullPointerException
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
10
The difference between an array and an ArrayList is

A) an ArrayList uses a LinkedList to hold its elements,an array does not.
B) an array keeps track of its length while an ArrayList does not track comparable information
C) arrays have a fixed size and cannot grow to accommodate more elements
D) All of the above
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
11
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
int nElements;
to represent the array of elements,and the number of elements currently stored.Show the code for a method int size( )that returns the current size of the list.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
12
The E get(int index)method of the List interface should throw an exception if

A) the index passed to it is negative
B) the index passed to it nonnegative
C) the index passed to it is negative or greater or equal to the size of the list
D) the index passed to is negative or greater than the size of the list
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
13
Why does the Java Collections Framework define a List as an interface rather than a class?

A) To be consistent with earlier versions of Java
B) To allow different types of concrete classes,with different performance characteristics to implement the interface
C) To allow the use of generic types
D) All of the above
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
14
A new element is added to an ArrayList object at index k.Assuming the list has size s and does not have to be resized,

A) the elements at current positions 0..k must be moved toward the beginning of the list
B) the elements at current positions k..s-1 must be moved toward the end of the array
C) the elements at current positions k..s must be moved toward the end of the array
D) the element at position k is overwritten
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
15
An ArrayList is so called because

A) you can use array subscript notation to work with the ArrayList
B) it is implemented as a class that uses an internal array to hold the elements of the list
C) you can pass it as a parameter to any method that expects an array
D) All of the above
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
16
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
Int nElements;
To represent the array of elements,and the number of elements currently stored.Assuming there is space
In the internal array,the code for adding a new item str to the end of the list is

A) list.add(str);
B) list[nElements] = str;
C) list[nElements] = str;nElements++;
D) nElements ++;list[nElements] = str;
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
17
Assume an array-based list implemented by a class that uses the fields
String [ ] list;
int nElements;
to represent the array of elements,and the number of elements currently stored.Show the code for a method String remove(int index)that removes and returns the element at the given index.The method should throw an appropriately named exception if it cannot return the requested element.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
18
The size of an array-based list such as ArrayList

A) is the number of bytes of memory that the list can hold
B) is the number of elements that are currently stored in the list
C) is the length of its internal array
D) None of the above
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
19
The position of an item within a list is called its

A) index
B) rank
C) level
D) number
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
20
The capacity of an array-based list such as ArrayList

A) is the number of bytes of memory that the list can hold
B) is the number of elements that are currently stored in the list
C) is the size of its internal array
D) None of the above
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 20 flashcards in this deck.