unit 7 Flashcards

1
Q

Arraylists are _____ (mutable/immutable)

A

mutable

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

how does an arraylist dynamically change size?

A

stores items in underlying array, creates bigger/smaller array and replaces old array with new one under same reference

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

define and write a “wild card” import of the java util class

A

imports all classes within a package (not specified, such as with import java.util.ArrayList;)

import java.util.*;

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

(true/false) referencing java.util.ArrayList
is the same as referencing ArrayList as a class name

A

true, but only after ArrayList has been imported, either via a wild card import or a single
import java.util.ArrayList;
statement

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

(true/false) import statements must come before a class definition

A

trueeee

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

how do you declare an arraylist?

A

ArrayList<Type> name;</Type>

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

in ArrayList<Type> name, type is a:</Type>

A

type parameter

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

in the statement:
ArrayList mystery = new Arraylist();

what type is the arraylist set to?

A

object type, as in:
ArrayList<object></object>

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

what types of data can ArrayLists hold? (primitives/references/ both)

A

references only, thus, for example, int must be converted to Int to be stored in an ArrayList

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

true/false: due to autoboxing, in arraylists Ints and Doubles are NOT able to be treated like ints and doubles

A

false; they can be approximately basically sorta mostly yearh

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

how to create arraylist

A

like all other reference variables, we must call a constructor. in this case, declare an arraylist:
ArrayList<String> myList;</String>

and set it equal to

new ArrayList<String>();</String>

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

what does the size() method do?

A

returns number of items in array

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

what happens if you call the size of list2 after the following initialization:
ArrayList<String> nameList = new ArrayList<String>();
?</String></String>

what about with

    ArrayList<String> list2 = null; ?
A
  1. you get returned a 0, there is literally no element and thus size = 0
  2. returned a null, methods cannot be called on null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

when are sequential searches used? when are binaries?

A

when you have a desired component in unsorted data

when you have data sorted in some manner

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

types of sorting algorithms (and their definitions)

A
  1. selection:
  2. insertion:
  3. merge:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which of the following is a reason to use an array instead of an ArrayList?

A. An array has faster access to its elements than a list does.

B. An array knows it length, but a list doesn’t know its length.

C. An ArrayList can allocate more space than it needs.

A

C, as every time an ArrayList fills up a new array is created that is twice as big. This can lead to extra space that is wasted.

17
Q

the .add() method adds an object to the (left/right) of the index in the parenthesis

A

left

18
Q

-11-2-9: Assume that numList has been initialized with the following Integer objects: [0, 1, 2, 3, 4]. What is the value of numList after mystery(5) executes?

private List<Integer> numList;
public void mystery(int n)
{
for (int i = 0; i < n; i++)
{
Integer obj = numList.remove(0);
numList.add(obj);
}
}
A. [4, 3, 2, 1, 0]
B. [1, 2, 3, 4, 0]
C. [0, 1, 2, 3, 4]
D. [2, 3, 4, 0, 1]
E. [4, 0, 1, 2, 3]</Integer>

A

c, ✔️ Each value is removed one at a time and added to the end of the list which results in the same list.

19
Q

Consider the following code segment.

ArrayList<String> arrList = new ArrayList<String>();</String></String>

arrList.add(“A”);

arrList.add(“B”);

arrList.add(“C”);

arrList.add(“D”);

for (int i = 0; i < arrList.size(); i++)

{

System.out.print(arrList.remove(i));

}

What, if anything, is printed as a result of executing the code segment?

A

“AC”
During the first iteration of the loop, i has the value 0 and the size of arrList is 4, so the element at index 0 (“A”) is removed and printed. During the second iteration of the loop, i has the value 1 and the size of arrList is 3, so the element at index 1 (“C”) is removed and printed. During the third comparison of i and arrList.size() in the loop header, i has the value 2 and the size of arrList is 2, so the loop terminates.

20
Q

Consider the following search method.

public static int search(int[] arr, int target)

{

int result = -1;

for (int j = 0; j < arr.length; j++)

{

if (arr[j] == target)

{

result = j; // Line 8

}

}

return result;

}

Which of the following describes the effect of replacing the statement in line 8 of the method with result = arr[j]; ?

The modified method will return the index of the first occurrence of target in arr.

B
The modified method will return the index of the last occurrence of target in arr.

C
The modified method will return target if target appears in arr and will return -1 otherwise.

D
The modified method will return -1 if target appears in arr and will return target otherwise.

E
The modified method will return -1 for all possible inputs.

A

Answer C
Correct. Since j represents an index into the array, arr[j] refers to an element of the array; so when arr[j] is equal to target, the modified method returns the value of target .

21
Q

Consider the following correct implementation of the insertion sort algorithm.

public static void insertionSort(int[] elements)

{

for (int j = 1; j < elements.length; j++)

{

int temp = elements[j];

int possibleIndex = j;

while (possibleIndex > 0 && temp < elements[possibleIndex - 1])

{

elements[possibleIndex] = elements[possibleIndex - 1];

possibleIndex–; // line 10

}

elements[possibleIndex] = temp;

}

}

The following declaration and method call appear in a method in the same class as insertionSort.

int[] arr = {10, 8, 3, 4};

insertionSort(arr);

How many times is the statement possibleIndex–; in line 10 of the method executed as a result of the call to insertionSort ?

A

5 times

The while loop iterates once each time an array element is shifted to the right as a result of an insertion. Therefore, the statement in line 10 is executed each time an element is shifted to the right. For the given array, 10 is shifted right when 8 is inserted before it. Then 8 and 10 are each shifted right when 3 is inserted before them. Lastly, 8 and 10 are each shifted right when 4 is inserted before them. A total of five shifts occur, so the statement in line 10 is executed five times.

22
Q

determine how many iterations of Binary Search logic will be required to find the stated value AND what value will be returned by the method given a return value of int: (the iteration and index)

[-7, -5, 0, 2, 4, 5, 9, 10]

a) 5
b) -3

A

a: 2 iterations, index 5
b: 3 iterations, -1

23
Q
A