All Flashcards
What is the output of the following code?
java
ArrayList<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3));
for (int i = 0; i < nums.size(); i++) {
System.out.print(nums.get(i) + " ");
}
1 2 3
What is the output of the following code?
java
ArrayList<String> words = new ArrayList<>(Arrays.asList("hello", "world"));
for (String word : words) {
System.out.print(word + " ");
}
hello world
Identify the error in the following code:
java
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3));
for (int i = 0; i <= numbers.size(); i++) {
System.out.println(numbers.get(i));
}
IndexOutOfBoundsException. The loop condition should be i < numbers.size().
What is the output of the following code?
java
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(2, 4, 6, 8));
for (int i = 0; i < list.size(); i++) {
if (list.get(i) % 4 == 0) {
list.remove(i);
i--;
}
}
System.out.println(list);
[2, 6]
What is the output of the following code?
java
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 3, 5, 7));
for (int i = 0; i < list.size(); i++) {
if (list.get(i) % 2 != 0) {
list.add(i, list.get(i));
i++;
}
}
System.out.println(list);
[1, 1, 3, 3, 5, 5, 7, 7]
Identify the error in the following code:
java
ArrayList<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3));
for (Integer num : nums) {
if (num % 2 == 0) {
nums.remove(num);
}
}
ConcurrentModificationException. Cannot modify the ArrayList while using an enhanced for loop.
What is the output of the following code?
java
ArrayList<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie"));
for (String name : names) {
name = "David";
}
System.out.println(names);
[Alice, Bob, Charlie]
What is the output of the following code?
java
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(10, 20, 30));
for (int i = 0; i < numbers.size(); i++) {
numbers.set(i, numbers.get(i) / 2);
}
System.out.println(numbers);
[5, 10, 15]
What is the output of the following code?
java
ArrayList<Integer> values = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
values.removeIf(n -> (n % 2 == 0));
System.out.println(values);
[1, 3, 5]
What is the output of the following code?
java
ArrayList<String> colors = new ArrayList<>(Arrays.asList("red", "green", "blue"));
colors.forEach(color -> System.out.print(color.toUpperCase() + " "));
RED GREEN BLUE
What are the steps to traverse an ArrayList using a for loop?
- Initialize a counter variable
ito 0. 2. Loop whileiis less thanlist.size(). 3. Access element at indexiusinglist.get(i). 4. Incrementi.
What are the steps to traverse an ArrayList using an enhanced for loop?
- Declare a variable of the ArrayList's element type. 2. Loop through each element in the list using the syntax
for (ElementType element : list). 3. Access the current element through the loop variable.
What are the steps to remove elements while traversing an ArrayList?
- Use a regular for loop. 2. Check if the element at index
ishould be removed. 3. If yes, remove the element usinglist.remove(i). 4. Decrementito avoid skipping elements. 5. Continue the loop.
What are the steps to add elements while traversing an ArrayList?
- Use a regular for loop. 2. Check if the element at index
irequires a new element to be added. 3. If yes, add the new element usinglist.add(i, element). 4. Incrementito avoid infinite loop. 5. Continue the loop.
What steps should you follow to modify elements in an ArrayList using a for loop?
- Use a regular for loop to iterate through the ArrayList. 2. Access the element at the current index using
list.get(i). 3. Modify the element usinglist.set(i, newValue). 4. Continue iterating through the ArrayList.
What steps should you follow to modify elements in an ArrayList of objects using an enhanced for loop?
- Use an enhanced for loop to iterate through the ArrayList. 2. Access the current object using the loop variable. 3. Modify the object's instance variables directly. 4. Note that you cannot replace the object itself.
What is the process for handling a ConcurrentModificationException?
- Identify the code section causing the exception (usually an enhanced for loop modifying the list). 2. Switch to a regular for loop. 3. Carefully manage index updates when adding or removing elements.
What is the process of debugging an IndexOutOfBoundsException when traversing an ArrayList?
- Check the loop condition to ensure it does not exceed the ArrayList's bounds. 2. Verify that the index being accessed is within the valid range (0 to size() - 1). 3. Review any modifications to the ArrayList's size during traversal.
What is the general process for iterating through an ArrayList and applying a condition to each element?
- Choose an appropriate loop (for or enhanced for). 2. Access each element in the ArrayList. 3. Apply the condition to the current element. 4. Perform an action based on the condition's result.
What are the steps to correctly iterate through an ArrayList and modify specific elements based on a condition?
- Use a regular for loop. 2. Check the condition for the element at index
i. 3. If the condition is met, modify the element usinglist.set(i, newValue). 4. Continue iterating through the ArrayList.
What are the differences between using a regular for loop and an enhanced for loop to traverse an ArrayList?
Regular for loop: Requires manual index management, allows modifications to the ArrayList during traversal. Enhanced for loop: Simpler syntax, does not allow modifications to the ArrayList during traversal.
What are the differences between modifying an ArrayList using set() within a for loop vs. modifying the enhanced for loop variable?
set(): Changes the actual element in the ArrayList. Enhanced for loop variable modification: Only changes the loop variable, not the ArrayList element (pass-by-value).
What are the differences between handling IndexOutOfBoundsException and ConcurrentModificationException?
IndexOutOfBoundsException: Occurs due to invalid index access. ConcurrentModificationException: Occurs due to structural modification during enhanced for loop traversal.
Compare the performance implications of adding elements at the end versus the beginning of an ArrayList.
Adding at the end: Generally faster, O(1) amortized time. Adding at the beginning: Slower, O(n) time due to element shifting.
Compare the use of removeIf() with a regular for loop for removing elements from an ArrayList based on a condition.
removeIf(): More concise, uses lambda expressions. Regular for loop: More control over the removal process and index management.
What are the differences between ArrayList and a standard Java array when traversing and modifying elements?
ArrayList: Dynamic size, uses get() and set() methods. Array: Fixed size, uses bracket notation for access and modification.
Compare the efficiency of searching for an element in an ArrayList using a loop versus using the contains() method.
Loop: Requires manual iteration, O(n) time complexity. contains(): Uses built-in optimized search, potentially faster in some implementations.
What are the differences between using a while loop and a for loop to traverse an ArrayList?
While loop: Requires manual initialization and increment of the index. For loop: More concise and manages index automatically.
Compare the readability and maintainability of using streams and lambda expressions versus traditional loops for processing ArrayList elements.
Streams and lambdas: More concise and declarative, can improve readability. Traditional loops: More verbose, but can be easier to understand for complex logic.
What are the differences between using Iterator and enhanced for loop for traversing an ArrayList?
Iterator: Allows removing elements during traversal using iterator.remove(). Enhanced for loop: More concise but does not allow removing elements.