Exam 2 Flashcards

(68 cards)

1
Q

An array is an indexed structure:

A

elements may be accessed in any order using subscript

elements can be accessed in sequence using a loop that increments the subscript

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

With Java arrays object , you cannot

A

increase or decrease its length (length is fixed )

insert an element at a specified position

remove an element at a specified position

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A _____ is a collection of elements, each with a position

A

list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In Java, the methods to be implemented by lists are defined in the ____ interface

A

List

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Classes that implement the List interface provide the functionality of an array and offer several operations :

A

Obtain an element at a specified position

Replace an element at a specified position

Find a specified target value

Add an element at either end a

Remove an element from either end

Insert or remove an element at any position

Traverse the list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The simplest class that implements the List interface

A

ArrayList class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Use an ArrayList class when:

A

you will be adding new elements to the end of a list

you need to access elements quickly in arbitrary order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you declare a List “object” whose elements will reference String objects :

A

List myList = new ArrayList();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

ArrayList is empty and has a default capacity of:

A

10 elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In an ArrayList, you cannot access an element using a bracket index as you can with arrays (array[1]).

Instead use _____ method.

A

get()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to search an ArrayList to get location

A

.indexOf()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

List myList new ArrayList <>() ; language feature called:

A

generics

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The statement creates a List of Strings; only references of type String can be stored in the list

A

Generic collections

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In generic collection, String in this statement is called a:

A

type parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The _______________ sets the data type of all objects stored in a collection

A

type parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The general declaration for generic collection is:

A

CollectionClass ab1e= new 1ass<>() ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

In a generic collection The indicates a:

A

type parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Adding a noncompatible type to a generic collection will generate an error during:

A

compile time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

The elements of a list can be of any type, including your own classes :

A

List inventory new ArrayList (); = new ArrayList();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

In order to use _______ with a List of user-defined objects, there must be a way to determine when two objects are equal

A

indexof

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

indexOf is done by overriding the ______ method inherited from ______

A

equals

object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

The equals method has a parameter of type:

A

Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How to use Object.equals

A

public boolean equals (Object other)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What does Object.equals do?

A

Compares two references to determine if they refer to the same object:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
An Object.equals method class must _____________ for the comparison to be meaningful
Override equals
26
How do you override an equals() in computer class?
``` @Override public boolean equals (Object obj) { if (obj instanceof Computer) { Computer other = (Computer) obj: return computePower() == other.computePower } (); return false ; } ```
27
Internally, the elements of an ArrayList are stored in an array. Three variables are needed:
theData : An array to hold the elements capacity : Physical size of array size : Number of data items in the list
28
We have two add methods:
One will append at the end of the list The other will insert an item at a specified position
29
Implementing add (String anEntry), If size is less than capacity, then to append a new item:
insert the new item at the position indicated by the value of size increment the value of size return true to indicate successful insertion
30
To insert into the middle of the array, the values at the insertion point are shifted over to make room, beginning at the end of the array and proceeding in the indicated order
Implementing add (int index, String anEntry)
31
When an item is removed , the items that follow it must be moved ________ to close the gap
forward
32
Creates a new array that is twice the size of the current array and then copy the contents of the new array
reallocate Method
33
Single-linked ArrayList is limited because
It’s add and remove method require a loop to shift elements
34
_________ is useful for inserting and removing at arbitrary locations without shifting elements, regardless of the size of the list
linked list
35
In a linked list, instead of using an array , each element is stored in a ______ and linked to the ______ element
node next
36
A LinkedList object can be used anywhere an ArrayList is used :
List < String > myList = new Linkedlist<>();
37
List < String > myList = new Linkedlist<>(); The initial list is empty. How do we add strings?
myList.add(“Tom”);
38
A list node contains :
a data item one (or more) link(s) (references to other node(s))
39
A Linkedlist object has a data field _____, which references the first list node.
head
40
Limitations of a singly-linked list include:
Insertion at the front is quickinsertion at other positions may require a list traversal to find insertion place Insertion is convenient only after a referenced node Removing a node requires a reference to the previous node We can traverse the list only in the forward direction
41
Limitations such as: Insertion at the front is quickinsertion at other positions may require a list traversal to find insertion place Insertion is convenient only after a referenced node Removing a node requires a reference to the previous node We can traverse the list only in the forward direction We can overcome these limitations by:
Adding a reference in each node to the previous node, creating a double-linked list
42
can be viewed as a moving place marker that keeps track of the current position in a particular linked list
An iterator
43
An Iterator object for a list starts at the:
List head
44
The programmer can move the Iterator by calling its _____ method.
next
45
The ________ stays on its current list item until it is needed
Iterator
46
By using an Iterator to traverse a linked list, the code segment can be:
rewritten so as to require n operations
47
Where is the iterative interface defined?
In java.util
48
The _____ interface declares the method ________ which returns an ________ object that iterates over the elements of that list
List iterator Iterator
49
An Iterator is: it does not refer to a particular object at any given time
conceptually between elements
50
How do we process all List through Iterator?
Iterator iter = aList.iterator(); while(iter.hasNext()) { item = iter.next(); }
51
You can use the ____________ method to remove items from a list as you access them.
Iterator remove()
52
deletes the most recent element returned in an Iterator
remove()
53
You must call next() before each remove(); otherwise , an _________ will be thrown
IllegalStateException
54
What’s the different LinkedList.remove(i) vs Iterator.remove
LinkedList.remove(i): must walk down the list each time , then remove Iterator.remove removes items without starting over at the beginning
55
How do you remove all elements from a list of type Integer that are divisible by a particular value:
public static void remove DivisibleBy (LinkedList alist, int div) Iterator iter = aList.iterator()); while ( iter.hasNext() ) { ``` int nextInt = iter.next (); if ( nextInt % div == 0) { iter.remove(); } } } ```
56
Iterator limitations: How do we overcome these limitations?
Traverses List only in the forward direction Provides a remove method, but no add method You must advance the Iterator using your own loop if you do not start from the beginning of the list ListIterator extends Iterator, overcoming these limitations
57
ListIterator positions are assigned an index from ____ to ____
0 Size
58
ListIterator has a method which is able to receive the proceeding and future index of items.
nextIndex(); | previousIndex();
59
ListIterator is a subinterface of:
Iterator
60
Classes that implement ListIterator must provide the features of:
Both ListIterator and Iterator
61
Requires fewer methods Can iterate over more general data structures used by enhanced for loop
Iterator
62
Iterator is required by the ________ interface
Collection
63
ListIterator is required only by the _______ interface
List
64
The collection interface specifically excludes: But including:
add (int , E) get ( int) remove ( int) set (int, E ) add (E) remove (Object) the iterator method
65
In a general Collection the order of elements is:
not specified
66
For collections implementing the List interface, the order of the elements is determined by the:
index
67
In a general Collection, the position where an ______ is inserted is not specified
object
68
In ArrayList and LinkedList, add (E) always inserts at the ____ and always returns:
end | true