zuai-logo

What are the differences between ArrayLists and arrays?

ArrayLists: Dynamic size, use methods | Arrays: Fixed size, use bracket notation

Flip to see [answer/question]
Flip to see [answer/question]

All Flashcards

What are the differences between ArrayLists and arrays?

ArrayLists: Dynamic size, use methods | Arrays: Fixed size, use bracket notation

What are the differences between selection sort and insertion sort?

Selection Sort: Finds minimum and swaps | Insertion Sort: Inserts elements into correct position

What are the differences between using a for loop and a for-each loop to traverse an ArrayList?

For loop: Uses index, can modify | For-each loop: No index, cannot modify

What are the differences between add(E obj) and add(int index, E obj) in ArrayLists?

add(E obj): Appends to the end | add(int index, E obj): Inserts at a specific index

What are the differences between get(int index) and set(int index, E obj) in ArrayLists?

get(int index): Retrieves element at index | set(int index, E obj): Updates element at index

What are the differences between using primitive types and wrapper classes in ArrayLists?

Primitive types: Cannot be stored directly | Wrapper classes: Can be stored in ArrayLists

What are the differences between searching and sorting?

Searching: Locates a specific element | Sorting: Arranges all elements in order

What are the differences between arrays and ArrayLists in terms of memory usage?

Arrays: Allocate fixed memory | ArrayLists: Dynamically allocate memory, potentially more overhead

What are the differences between the best-case time complexity of selection sort and insertion sort?

Selection Sort: O(n^2) | Insertion Sort: O(n)

What are the differences between the worst-case time complexity of selection sort and insertion sort?

Selection Sort: O(n^2) | Insertion Sort: O(n^2)

What does the following code output?

java
ArrayList<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
System.out.println(list.size());

2

What does the following code output?

java
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.set(0, 15);
System.out.println(list.get(0));

15

Identify the error in the following code:

java
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
System.out.println(list.get(2));

IndexOutOfBoundsException: The index 2 is out of bounds for the list of size 2.

What does the following code output?

java
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.remove(0);
System.out.println(list.size());

1

What does the following code output?

java
ArrayList<Integer> list = new ArrayList<>();
list.add(5);
list.add(10);
list.add(5);
int index = list.indexOf(5);
System.out.println(index);

0

What does the following code output?

java
ArrayList<String> list = new ArrayList<>();
System.out.println(list.size());

0

Identify the error in the following code:

java
ArrayList<int> list = new ArrayList<>();

ArrayLists cannot store primitive types directly. Use the Integer wrapper class instead: ArrayList.

What does the following code output?

java
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.clear();
System.out.println(list.size());

0

What does the following code output?

java
ArrayList<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
System.out.println(list.get(1));

banana

What does the following code output?

java
ArrayList<Integer> list = new ArrayList<>();
list.add(5);
list.add(10);
list.remove(Integer.valueOf(5));
System.out.println(list.size());

1

What is an ArrayList?

A dynamic data structure that stores an ordered collection of items.

What does 'mutable' mean in the context of ArrayLists?

Elements can be changed after the ArrayList is created.

What is a wrapper class?

A class that encapsulates a primitive data type, turning it into an object.

What is linear search?

A method for finding a target element in a list by checking each element sequentially.

Define selection sort.

A sorting algorithm that repeatedly finds the minimum element and moves it to the sorted portion of the list.

Define insertion sort.

A sorting algorithm that inserts each element into its correct position in the already sorted portion of the list.

What is encryption?

The process of converting plain text into a code to protect data from unauthorized access.

What is traversing an ArrayList?

Accessing and processing each element in the ArrayList, usually within a loop.

What is the Java Collections Framework?

A set of interfaces and classes that provide implementations for common data structures.

Define personal data.

Any information that relates to an identifiable individual.