ArrayLists are dynamic in size, allowing addition and removal of elements.
Flip to see [answer/question]
Flip to see [answer/question]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Flip
Revise later
SpaceTo flip
If confident
All Flashcards
Why are ArrayLists more versatile than arrays?
ArrayLists are dynamic in size, allowing addition and removal of elements.
What is the purpose of the size() method in ArrayLists?
Returns the number of elements currently in the ArrayList.
Why must ArrayLists store objects instead of primitive types?
ArrayLists are designed to hold objects; primitive types must be wrapped in their corresponding class.
What is the significance of the import java.util.ArrayList; statement?
It makes the ArrayList class available for use in your Java code.
Explain the concept of traversing an ArrayList.
Iterating through each element of the ArrayList to perform an operation.
What is the purpose of searching in an ArrayList?
To find a specific element within the ArrayList.
What is the purpose of sorting an ArrayList?
To arrange the elements of the ArrayList in a specific order.
What is the ethical consideration when collecting user data?
Protecting user privacy and using data responsibly.
What does it mean for an ArrayList to be 'dynamic'?
Its size can change during runtime; elements can be added or removed.
Why is transparency important regarding data collection?
Builds trust with users and allows them to make informed decisions.
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 are the steps of adding an element at a specific index in an ArrayList?
Specify the index and the element. 2. Shift existing elements to the right. 3. Insert the new element at the specified index.
What are the steps of removing an element at a specific index in an ArrayList?
Specify the index. 2. Remove the element at that index. 3. Shift subsequent elements to the left.
What are the steps to convert an ArrayList to an array?
Create a new array with the same size as the ArrayList. 2. Iterate through the ArrayList. 3. Copy each element from the ArrayList to the array.
What are the steps of linear search?
Start at the beginning of the list. 2. Compare each element to the target. 3. If a match is found, return the index. 4. If the end is reached without a match, return -1.
What are the steps of selection sort?
Find the minimum element in the unsorted portion. 2. Swap it with the first element of the unsorted portion. 3. Repeat for the remaining unsorted portion.
What are the steps of insertion sort?
Assume the first element is sorted. 2. Pick the next element. 3. Insert it into the correct position in the sorted portion.
What are the steps for safe data handling?
Use encryption. 2. Collect only necessary data. 3. Implement security measures. 4. Be transparent with users.
What are the steps to create an ArrayList?
Import the ArrayList class. 2. Declare an ArrayList object. 3. Specify the data type using generics.
What are the steps to traverse an ArrayList using a for loop?
Initialize a counter variable. 2. Loop through the ArrayList using the size() method. 3. Access each element using the get() method.
What are the steps to traverse an ArrayList using a for-each loop?
Declare a variable of the ArrayList's data type. 2. Use the for-each loop to iterate through each element. 3. Access each element directly.