AP Computer Science Unit 7

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

[mew, pet]

Consider the following code segment. Watch out for ArrayList Pitfalls! ArrayList<String> words = new ArrayList<String>(); words.add("cat"); words.add("mew"); words.add("hen"); words.add("pet"); int i = 0; while (i < words.size()) { words.remove(i); i++; } System.out.println(words.toString()); What is printed when the code segment is executed? [ ] [mew, pet] [hen, pet] [mew, hen, pet] [cat, mew, hen, pet]

This occurs if the array is already sorted. The order is just confirmed. Each pass through the array will involve just one comparison. This occurs if the array is sorted in reverse order. This will lead to the maximum possible number of comparisons and moves.

Insertion sort Best Case: Worst Case:

2 is equal to 2

What is the output of the following: List<Integer> nums = new ArrayList<Integer>(3); nums.add(1); nums.add(2); nums.add(0, nums.get(1)); Integer x = nums.get(0); Integer y = nums.get(2); if (x == y) System.out.println(x + " is equal to " + y); else System.out.println(x + " is NOT equal to " + y); 1 is NOT equal to 2 1 is equal to 1 2 is NOT equal to 1 2 is equal to 2

Appends obj to the end of the list; returns true

boolean add(E obj)

Returns the number of elements in the list

int size()

List; size, get, set, add (overloaded), and remove

The ArrayList class implements the _____ inteface. It indicates which methods must be defined in the ArrayList class, which are:

ArrayList<String> myList

Consider the following statement, which is intended to create an ArrayList named arrList to store elements only of type String. /* missing code */ = new ArrayList<String>(); Which of the following can be used to replace /* missing code */ so that the statement works as intended? ArrayList myList() ArrayList myList ArrayList<> myList ArrayList myList <String> ArrayList<String> myList

public static void selectionSort(int[] elements) { for (int j = 0; j < elements.length - 1; j++) // line 3 { int minIndex = j; // line 5 for (int k = j + 1; k < elements.length; k++) // line 6 { if (elements[k] < elements[minIndex]) // line 8 { minIndex = k; } // line 11 } int temp = elements[j]; // line 13 elements[j] = elements[minIndex]; elements[minIndex] = temp; // line 15 } }

Selection sort

The target is in the first spot and is found on the first try the target is in last spot or not found - all n elements must be examined. (Longest total execution time would be if target is not found) there will be n/2 comparisons use a .equals method, rather than ==

Sequential String: Best Case: Worst Case: On average:

unsorted

Sequential search is the only search method that can be used to find a value in _____ data

ConcurrentModificationException

Suppose that myNums is an initialized ArrayList of Double objects which contains at least one negative number. What would be the result of the following code? (You may want to review the 7.3 README) for(Double each: myNums) { if(each < 0.0 || each > 100.0) myNums.add(0.0); } It would add 0.0 to the list whenever it finds a number outside the range of 0.0 - 100.0 IndexOutOfBoundsException ConcurrentModificationException NullPointerException

1. Loop from j = 0 to j = elements.length-2, inclusive, completing elements.length-1 passes. 2. In each pass, swap the item at index j with the minimum item in the rest of the array

What does a selection sort do?

Selection Sort will always take the same amount of time to execute, regardless of original order (this means that best, worst, and average case are the same).

What is the amount of time selection sort takes to execute?

[B, D, C, A]

What is the output from the following? ArrayList<String> list = new ArrayList<String>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); for (int i = 0; i < list.size(); i++) list.add(list.remove(i)); System.out.println(list); [A, B, C, D] [B, D, C, A] [B, C, D, A] [D, C, B, A]

[0, 1, 2, 0, 1, 2]

What is the output of the following? ArrayList<Integer> myList = new ArrayList<Integer>(); myList.add(0); myList.add(1); myList.add(2); myList.add(0,0); myList.add(1,1); myList.add(2,2); System.out.println(myList); [0, 1, 2] [0, 0, 1, 1, 2, 2] [0, 1, 2, 0, 1, 2] [0, 1, 2, 0, 0, 1, 1, 2, 2]

ArrayLists have a dynamic size, and the ArrayList class has methods for insertion and deletion of elements, making reordering and shifting elements easier.

What makes ArrayLists different from Arrays?

insert (int i, String x)

Which of the following is NOT a method of java.util.ArrayList<String>? add (String x) remove (Object x) insert (int i, String x) contains (Object x) set (int i, String x)

False

An ArrayList can hold primitive data?

Returns the element at position index in the list

E get(int index)

ArrayList myarray = new ArrayList(); ArrayList<Fish> myFish = new ArrayList<Fish>();

Pre-Generics constructor: Generics constructor for an ArrayList with a Fish Object:

n(n-1)/2

What are the number of comparisons in a selection sort?

inserts obj at position index (0 <= index <= size), moving elements at position index and higher to the right (adds 1 to their indicies) and adds 1 to size

void add(int index, E obj)

If you try to add the incorrect data type to the list, you will get a compile error.

If the reference type is generic (left side) and the object (right side) is non-generic, what will happen?

capacity 10 and size 0

What is the default capacity and size created by the default constructor?

You cannot change primitive values, and you cannot get the index of the element using "for-each".

What can you NOT do while traversing an array using a for each loop

Objects only; Strings are objects, ints, doubles, and chars are primitives; You use a wrapper class such as Integer, Double, or Char.

An array list stores references to _____ only. What are objects and what are primitives? How do you store a primitive data type in an ArrayList and what are they called?

class; object; java.util; Collections; import java.util.ArrayList;

ArrayList is a _____ in Java. When you make an ArrayList, you are making an ArrayList _____. ArrayList is part of the _____ package. The ArrayList is in the _____ API. Use this import statement:_____.

class

ArrayList is a(n): interface class method part of the java.lang package

new ArrayList<Thing>()

Consider the following statement, which is intended to create an ArrayList named a to store only elements of type Thing. Assume that the Thing class has been properly defined and includes a no-parameter constructor. ArrayList<Thing> a = /* missing code */; Which of the following can be used to replace /* missing code */ so that the statement works as intended? A. new Thing() B. new ArrayList<Thing>() C. new ArrayList(Thing) D. new ArrayList(<Thing>) E. new ArrayList<>(Thing)

ConcurrentModificationException; add or remove elements

Changing the size of an ArrayList while traversing it using an enhanced for loop ("for-each" loop) can result in an _____ being thrown. Therefore you should not _____ while traversing an ArrayList with a for loop

1 3

Consider the following code segment. ArrayList<Integer> myList = new ArrayList(); for (int i = 0; i < 4; i++) { myList.add(i + 1); } for (int i = 0; i < 4; i++) { if (i % 2 == 0) { System.out.print(myList.get(i) + " "); } } What output is produced as a result of executing the code segment? 0 1 2 3 1 2 3 4 0 2 1 3 2 4

[B, B, A]

Consider the following code segment. ArrayList<String> myLetters = new ArrayList<String>(); myLetters.add("C"); myLetters.add(0, "B"); myLetters.set(1, "D"); myLetters.add("A"); myLetters.add(2, myLetters.get(0)); myLetters.remove(1); System.out.println(myLetters.toString()); What is printed as a result of executing the code segment? [B, A, B] [B, B, A] [C, C, A] [D, B, A] [D, D, A]

D. 1 3

Consider the following code segment. ArrayList<Integer> myList = new ArrayList(); for (int i = 0; i < 4; i++) { myList.add(i + 1); } for (int i = 0; i < 4; i++) { if (i % 2 == 0) { System.out.print(myList.get(i) + " "); } } What output is produced as a result of executing the code segment? A. 0 1 2 3 B. 1 2 3 4 C. 0 2 D. 1 3 E. 2 4

[4, 3, 0, 2, 0]

Consider the following code segment. ArrayList<Integer> nums = new ArrayList<>(); nums.add(3); nums.add(2); nums.add(1); nums.add(0); nums.add(0, 4); nums.set(3, 2); nums.remove(3); nums.add(2, 0); Which of the following represents the contents of nums after the code segment has been executed? A. [2, 4, 3, 2, 0] B. [3, 2, 0, 1, 0] C. [4, 2, 0, 2, 0] D. [4, 3, 0, 2, 0] E. [4, 3, 0, 3, 0]

[4, 2, 0]

Consider the following code segment. ArrayList<Integer> vals = new ArrayList<Integer>(); vals.add(vals.size(), vals.size()); vals.add(vals.size() - 1, vals.size() + 1); vals.add(vals.size() - 2, vals.size() + 2); System.out.println(vals.toString()); What is printed as a result of executing the code segment? A. [0, 1, 2] B. [0, 2, 4] C. [1, 2, 3] D. [2, 1, 0] E. [4, 2, 0]

A. AC

Consider the following code segment. ArrayList<String> arrList = new ArrayList<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 B. BD C. ABC D. ABCD E. Nothing is printed.

[DI, DI, DA]

Consider the following code segment. ArrayList<String> syllables = new ArrayList<String>(); syllables.add("LA"); syllables.add(0, "DI"); syllables.set(1, "TU"); syllables.add("DA"); syllables.add(2, syllables.get(0)); syllables.remove(1); System.out.println(syllables.toString()); What is printed as a result of executing the code segment? A. [DI, DA, DI] B. [DI, DI, DA] C. [LA, LA, DA] D. [TU, DI, DA] E. [TU, TU, DA]

B [new, pet]

Consider the following code segment. ArrayList<String> words = new ArrayList<String>(); words.add("mat"); words.add("new"); words.add("open"); words.add("pet"); int i = 0; while (i < words.size()) { words.remove(i); i++; } System.out.println(words.toString()); What is printed when the code segment is executed? A. [] B. [new, pet] C. [open, pet] D. [new, open, pet] E. [mat, new, open, pet]

[4, 3, 0, 2, 0]

Consider the following code segment. ArrayList<Integer> list = new ArrayList<>(); list.add(3); list.add(2); list.add(1); list.add(0); list.add(0, 4); list.set(3, 2); list.remove(3); list.add(2, 0); Which of the following represents the contents of list after the code segment has been executed? [2, 4, 3, 2, 0] [3, 2, 0, 1, 0] [4, 2, 0, 2, 0] [4, 3, 0, 2, 0] [4, 3, 0, 3, 0]

5

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--; } elements[possibleIndex] = temp; // line 12 } } The following declaration and method call appear in a method in the same class as insertionSort. int[] nums = {8, 7, 5, 4, 2, 1}; insertionSort(nums); How many times is the statement elements[possibleIndex] = temp; in line 12 of the method executed as a result of the call to insertionSort ?

5

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 ?

ArrayList<String> arrList

Consider the following statement, which is intended to create an ArrayList named arrList to store elements only of type String. /* missing code */ = new ArrayList<String>(); Which of the following can be used to replace /* missing code */ so that the statement works as intended? A. ArrayList arrList() B. ArrayList arrList C. ArrayList<> arrList D. ArrayList arrList<String> E. ArrayList<String> arrList

5 Correct. The statement in line 12 executes once each time an element is inserted into the array. The number of insertions is one less than the length of the array. For this array, the statement is executed for a total of five times: when 7 is inserted before 8, when 5 is inserted before 7, when 4 is inserted before 5, when 2 is inserted before 4, and when 1 is inserted before 2.

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--; } elements[possibleIndex] = temp; // line 12 } } The following declaration and method call appear in a method in the same class as insertionSort. int[] nums = {8, 7, 5, 4, 2, 1}; insertionSort(nums); How many times is the statement elements[possibleIndex] = temp; in line 12 of the method executed as a result of the call to insertionSort ?

D. 5 Correct. 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.

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. 0 B. 1 C. 4 D. 5 E. 6

3

Consider the following correct implementation of the selection sort algorithm. public static void selectionSort(int[] elements) { for (int j = 0; j < elements.length - 1; j++) { int minIndex = j; for (int k = j + 1; k < elements.length; k++) { if (elements[k] < elements[minIndex]) { minIndex = k; } } if (j != minIndex) { int temp = elements[j]; elements[j] = elements[minIndex]; elements[minIndex] = temp; // line 19 } } } The following declaration and method call appear in a method in the same class as selectionSort. int[] arr = {30, 40, 10, 50, 20}; selectionSort(arr); How many times is the statement elements[minIndex] = temp; in line 19 of the method executed as a result of the call to selectionSort ? 1 2 3 4 5

3 Correct. The statement in line 19 executes each time a value is swapped into the correct position in the array. For the given array, the values 30 and 10 are swapped. Then the values 40 and 20 are swapped. Then, since 30 is already in the correct position, no swap occurs. Lastly, 50 and 40 are swapped.

Consider the following correct implementation of the selection sort algorithm. public static void selectionSort(int[] elements) { for (int j = 0; j < elements.length - 1; j++) { int minIndex = j; for (int k = j + 1; k < elements.length; k++) { if (elements[k] < elements[minIndex]) { minIndex = k; } } if (j != minIndex) { int temp = elements[j]; elements[j] = elements[minIndex]; elements[minIndex] = temp; // line 19 } } } The following declaration and method call appear in a method in the same class as selectionSort. int[] arr = {30, 40, 10, 50, 20}; selectionSort(arr); How many times is the statement elements[minIndex] = temp; in line 19 of the method executed as a result of the call to selectionSort ? A. 1 B. 2 C. 3 D. 4 E. 5

C. When firstList is {1, 3, 5, 7} and secondList is {5, 5, 3, 1}

Consider the following method definition. The method isReversed is intended to return true if firstList and secondList contain the same elements but in reverse order, and to return false otherwise. /** Precondition: firstList.size() == secondList.size() */ public static boolean isReversed(ArrayList<Integer> firstList, ArrayList<Integer> secondList) { for (int j = 0; j < firstList.size() / 2; j++) { if (firstList.get(j) != secondList.get(secondList.size() - 1 - j)) { return false; } } return true; } The method does not always work as intended. For which of the following inputs does the method NOT return the correct value? A. When firstList is {1, 3, 3, 1} and secondList is {1, 3, 3, 1} B. When firstList is {1, 3, 3, 1} and secondList is {3, 1, 1, 3} C. When firstList is {1, 3, 5, 7} and secondList is {5, 5, 3, 1} D. When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 1} E. When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 3}

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

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. The modified method will return the index of the last occurrence of target in arr. The modified method will return target if target appears in arr and will return -1 otherwise. The modified method will return -1 if target appears in arr and will return target otherwise. The modified method will return -1 for all possible inputs.

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

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]; ? A. 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.

new ArrayList<Panda>()

Consider the following statement, which is intended to create an ArrayList named a to store only elements of type Panda . Assume that the Panda class has been properly defined and includes a no- parameter constructor. ArrayList< Panda > a = /* missing code */; Which of the following can be used to replace /* missing code */ so that the statement works as intended? new ArrayList<>(Panda) new ArrayList<Panda> new ArrayList(Panda) new ArrayList(<Panda>) new ArrayList<Panda>()

I and III only

Consider the following statement, which is intended to create an ArrayList named numbers that can be used to store Double values. Warning: There is more than one answer. See the section on "Mixing Generics and PreGenerics" in the notes. ArrayList<Double> numbers = /* missing code */; Which of the following can be used to replace /* missing code */ so that the statement works as intended? I. new ArrayList() II. new ArrayList<Double> III. new ArrayList<Double>() III only I and II only I and III only II and III only I, II, and III

C. I and III only

Consider the following statement, which is intended to create an ArrayList named numbers that can be used to store Integer values. ArrayList<Integer> numbers = /* missing code */; Which of the following can be used to replace /* missing code */ so that the statement works as intended? I. new ArrayList() II. new ArrayList<Integer> III. new ArrayList<Integer>() A. III only B. I and II only C. I and III only D. II and III only E. I, II, and III

The modification in seqSearch2 will cause the value of target to be returned instead of the index of the target

Consider the method seqSearch, which implements a sequential search algorithm. public int seqSearch(int[] arr, int target) { for (int j = 0; j < arr.length; j++) { if (arr[j] == target) { return j; } } return -1; } Consider another method, seqSearch2, which modifies seqSearch to use an enhanced for loop ("for each"). public int seqSearch2(int[] arr, int target) { for (int j : arr) { if (j == target) { return j; } } return -1; } Which of the following best describes the difference in the behavior of seqSearch2 relative to seqSearch as a result of the modification? The modification in seqSearch2 has no effect: seqSearch2 will always behave exactly as seqSearch does. The modification in seqSearch2 will cause a compilation error. The modification in seqSearch2 will cause an ArrayIndexOutOfBoundsException to be thrown for some inputs. The modification in seqSearch2 will cause -1 to be returned for all inputs. The modification in seqSearch2 will cause the value of target to be returned instead of the index of the target

E. The modification in seqSearch2 will cause the value of target to be returned instead of the index of target in cases where target appears in arr.

Consider the method seqSearch, which implements a sequential search algorithm. public int seqSearch(int[] arr, int target) { for (int j = 0; j < arr.length; j++) { if (arr[j] == target) { return j; } } return -1; } Consider another method, seqSearch2, which modifies seqSearch to use an enhanced for loop. public int seqSearch2(int[] arr, int target) { for (int j : arr) { if (j == target) { return j; } } return -1; } Which of the following best describes the difference in the behavior of seqSearch2 relative to seqSearch as a result of the modification? A The modification in seqSearch2 has no effect: seqSearch2 will always behave exactly as seqSearch does. B The modification in seqSearch2 will cause a compilation error. C The modification in seqSearch2 will cause an ArrayIndexOutOfBoundsException to be thrown for some inputs. D The modification in seqSearch2 will cause -1 to be returned for all inputs. E The modification in seqSearch2 will cause the value of target to be returned instead of the index of target in cases where target appears in arr.

B The modified method will return the index of the last appearance of the target value in the list, or -1 if the target value does not appear in the list.

Consider the method sequentialSearch, which takes an ArrayList of Integer elements and an int value as parameters and returns the index of the first appearance of the target value in the list or -1 if the target value does not appear in the list. public static int sequentialSearch(ArrayList<Integer> elements, int target) { for (int j = 0; j < elements.size(); j++) // Line 3 { if (elements.get(j) == target) { return j; } } return -1; } Which of the following explains how replacing Line 3 with for (int j = (elements.size() - 1); j >= 0; j--) will affect the behavior of sequentialSearch? A. The modification has no effect: the modified method will continue to return the index of the first appearance of the target value in the list, or -1 if the target value does not appear in the list. B. The modified method will return the index of the last appearance of the target value in the list, or -1 if the target value does not appear in the list. C. The modified method will throw an ArrayIndexOutOfBoundsException. D. The modified method will return -1 regardless of the inputs. E. The modified method will not compile.

int i = 0; while (i < words.size()) { if("like".equals(words.get(i))) words.remove(i); else i++; }

Removing parts of indexes with a while loop

Removes element from position index, moving elements at position index + 1 and higher to the left (subtracts 1 from their indices) and subtracts 1 from size; returns the element formerly at position index

E remove(int index)

Replaces the element at position index with obj; returns the element formerly at position index

E set(int index, E obj)

ArrayList <object type> arrayName = new ArrayList<object type> (); ArrayList<String> names = new ArrayList<String>(); ArrayList<Integer> nums = new ArrayList<Integer>(); ArrayList<Pet> myPets = new ArrayList<Pet>();

How do you declare an array? Declare an array of strings Declare an array of Integers Declare an array of Pet Objects

The compiler will not catch your error if you add a different type of data to the list. Such errors wouldn't appear until runtime

If the reference type is not generic and the object is generic, what will happen?

n-1 passes

In a selection sort, for an array of "n" elements the array is sorted after how many passes?

The target is at the beginning of the array/ArrayList.

In the AP version of Sequential Search, the "best case" scenario occurs when: The target is at the beginning of the array/ArrayList. The target is in the middle of the array/ArrayList. The target is at the end of the array/ArrayList. The target is not found.

The index of the first occurrence of the target

In the AP version of Sequential Search, what is returned from the method if the target value is found? The value of the target The index of the first occurrence of the target -1 It depends on the type of data being searched

{1, 0, 0, 2}

In the code segment below, myList is an ArrayList of integers. The code segment is intended to remove all elements with the value 0 from myList. int j = 0; while (j < myList.size()) { if (myList.get(j) == 0) { myList.remove(j); } j++; } The code segment does not always work as intended. For which of the following lists does the code segment NOT produce the correct result? {0, 1, 2, 3} {0, 1, 0, 2} {1, 0, 0, 2} {1, 2, 3, 0} {1, 2, 3, 4}

C {1, 0, 0, 2}

In the code segment below, myList is an ArrayList of integers. The code segment is intended to remove all elements with the value 0 from myList. int j = 0; while (j < myList.size()) { if (myList.get(j) == 0) { myList.remove(j); } j++; } The code segment does not always work as intended. For which of the following lists does the code segment NOT produce the correct result? A. {0, 1, 2, 3} B. {0, 1, 0, 2} C. {1, 0, 0, 2} D. {1, 2, 3, 0} E. {1, 2, 3, 4}

A. 4

In the code segment below, numList is an ArrayList of integers that is sorted in descending order. The code segment is intended to insert the integer value val into numList so that numList is still sorted in descending order. int j = 0; while (val != numList.get(j)) { j++; } numList.add(j, val); The code segment does not always work as intended. Assuming that numList has been initialized to {3, 2, 1, 0}, for which value of val does the code segment NOT produce the expected result? A. 4 B. 3 C. 2 D. 1 E. 0

False

The elements in an array or ArrayList must be sorted in order to use Sequential Search.

It will always take the same amount of time to execute

Under what condition will a selection sort execute faster? It is unpredictable (random). If the data is already sorted in ascending order If the data is already sorted in descending order It will always take the same amount of time to execute

If the data is already sorted in ascending order

Under what condition will an insertion sort execute faster? It is unpredictable (random). If the data is already sorted in ascending order If the data is already sorted in descending order It will always take the same amount of time to execute


Set pelajaran terkait

Circuits and Electricity AP Physics

View Set

BLD 204 Hallmarks of Cancer pt. 2

View Set

Fundamentals of Networking Chapter 12

View Set