All Flashcards
What is the importance of initializing maxValue and minValue correctly when finding the minimum and maximum in an ArrayList?
Initializing to 0 can lead to incorrect results if all values are positive (for minimum) or negative (for maximum).
Why is it important to check for duplicates in an ArrayList?
To ensure data integrity, uniqueness, or to avoid unintended behavior in algorithms.
Explain the concept of shifting elements in an ArrayList.
Moving each element to the left or right, potentially wrapping around the ends.
What is the significance of consecutive sequences in an ArrayList?
They represent patterns or groupings of elements that may have a specific meaning or relationship.
Why is it important to handle edge cases when developing algorithms for ArrayLists?
To ensure the algorithm works correctly for all possible inputs, including empty or unusual ArrayLists.
What is the advantage of using ArrayLists over arrays?
ArrayLists offer dynamic resizing, making them more flexible for handling varying amounts of data.
Explain the importance of preconditions when finding the mode of an ArrayList.
The algorithm assumes the ArrayList has a mode; otherwise, the result may be incorrect.
What is the purpose of using nested loops when checking for duplicates in an ArrayList?
To compare each element with every other element in the ArrayList.
What is the purpose of casting to double when calculating the mean of an ArrayList of integers?
To ensure that the result is a floating-point number and that any fractional part of the mean is preserved.
Why is it important to consider time complexity when choosing an algorithm for ArrayList manipulation?
Different algorithms have different performance characteristics, and choosing an efficient algorithm can significantly impact performance, especially for large ArrayLists.
What are the general steps to find the maximum value in an ArrayList?
Initialize maxValue to the first element, iterate through the ArrayList, compare each element with maxValue, and update maxValue if a larger element is found.
What are the general steps to find the minimum value in an ArrayList?
Initialize minValue to the first element, iterate through the ArrayList, compare each element with minValue, and update minValue if a smaller element is found.
What are the general steps to calculate the sum of elements in an ArrayList?
Initialize sum to 0, iterate through the ArrayList, and add each element to the sum.
What are the steps to calculate the mean of an ArrayList?
Calculate the sum of the elements, divide the sum by the number of elements, and cast to a double to preserve fractional parts.
What are the steps to determine if all values in an ArrayList have a certain property?
Iterate through the ArrayList, check if each element satisfies the property, and return false if any element does not satisfy the property. Return true if all elements satisfy the property.
What are the steps to check for duplicate elements in an ArrayList?
Use nested loops to compare each element with every other element. If any two elements are equal, return true. If the loops complete without finding any duplicates, return false.
What are the steps to shift elements one index to the left in an ArrayList?
Store the first element, iterate through the ArrayList, set each element to the value of the next element, and set the last element to the stored first element.
What are the steps to shift elements one index to the right in an ArrayList?
Store the last element, iterate through the ArrayList in reverse order, set each element to the value of the previous element, and set the first element to the stored last element.
What are the steps to reverse an ArrayList?
Create a new ArrayList, iterate through the original ArrayList in reverse order, and add each element to the new ArrayList.
What are the steps to transfer items from an ArrayList into an Array?
Create a new Array of the same size as the ArrayList, iterate through the ArrayList, and copy each element to the corresponding index in the Array.
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3));
doubleArray(arr);
System.out.println(arr);
assuming doubleArray
is the method provided in the notes.
[2, 4, 6]
Identify the error in the following code:
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3));
int sum = sum(arr);
System.out.println(sum / (arr.size()));
Integer division will truncate the decimal part of the mean. Should cast to double either sum or arr.size().
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 2, 3, 3, 3));
System.out.println(mode(arr));
assuming mode
is the method provided in the notes.
3
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(2, 4, 6, 8));
System.out.println(isEven(arr));
assuming isEven
is the method provided in the notes.
true
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
returnAllConsecutiveSequences(arr, 3);
assuming returnAllConsecutiveSequences
is the method provided in the notes.
1 2 3 2 3 4 3 4 5
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3, 2, 1));
System.out.println(duplicates(arr));
assuming duplicates
is the method provided in the notes.
true
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));
System.out.println(evenFrequency(arr));
assuming evenFrequency
is the method provided in the notes.
3
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(shiftLeft(arr));
assuming shiftLeft
is the method provided in the notes.
[2, 3, 4, 5, 1]
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(shiftRight(arr));
assuming shiftRight
is the method provided in the notes.
[5, 1, 2, 3, 4]
What does the following code output?
java
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(reverse(arr));
assuming reverse
is the method provided in the notes.
[5, 4, 3, 2, 1]