zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion Bank

What does the following code output?

java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10));
int target = 6;
System.out.println(linearSearch(arr, target));

2

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What does the following code output?

java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10));
int target = 6;
System.out.println(linearSearch(arr, target));

2

What does the following code output?

java
int[] arr = {1, 3, 5, 7, 9};
int target = 2;
System.out.println(linearSearch(arr, target));

-1

Identify the error in the following code:

java
public static int linearSearch(ArrayList<String> array, int n) {
    for (int i = 0; i < array.size(); i++) {
        if (array.get(i) == n) {
            return i;
        }
    }
    return -1;
}

The method expects an ArrayList of Strings but compares the elements to an integer n. The parameter type should be String n.

Complete the following code snippet for linear search in an integer array:

java
public static int linearSearch(int[] array, int n) {
    for (int i = 0; i < array.length; i++) {
        if (array[i] == n) {
            // Complete this line
        }
    }
    return -1;
}

return i;

What does the following code output?

java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(5, 10, 15, 20));
int target = 5;
System.out.println(linearSearch(arr, target));

0

What does the following code output?

java
int[] arr = {5, 10, 15, 20};
int target = 25;
System.out.println(linearSearch(arr, target));

-1

What is the key principle of Linear Search?

Examining each element of a list in sequence until the target element is found or the entire list has been checked.

What is the time complexity of Linear Search in the worst-case scenario?

O(n), where n is the number of elements in the list.

What is the time complexity of Linear Search in the best-case scenario?

O(1), when the target element is the first element in the list.

Does Linear Search require the data to be sorted?

No, Linear Search can be used on unsorted data.

What are the advantages of using Linear Search?

Simple to implement, works on unsorted data.

What are the disadvantages of using Linear Search?

Inefficient for large datasets compared to other searching algorithms.

What is the definition of Linear Search?

A searching algorithm that sequentially checks each element in a list until the target element is found or the list is exhausted.

What is the definition of Sequential Search?

A searching algorithm that sequentially checks each element in a list until the target element is found or the list is exhausted.

What is the definition of an ArrayList?

A resizable array implementation of the List interface.

What is the definition of 'index' in the context of searching?

The position of an element within a list or array.

What does it mean to 'search' in the context of data structures?

To find a specific element within a data structure and potentially return its location.

What is the return value if an element is not found in Linear Search?

-1