1/57
Looks like no tags are added yet.
What are the essential properties of a Java array?
Ordered, zero-indexed collection of one type, fixed physical size after creation, can store primitives or object references, valid indexes from 0 to arr.length-1.
What is the length and valid indexes of 'int[] arr = new int[5];'?
Length = 5; valid indexes = 0-4.
What default values are assigned in a newly created int array?
Every int starts with 0.
What is the difference between 'int[] a, b;' and 'int a[], b;'?
'int[] a, b;' makes both variables arrays, while 'int a[], b;' makes 'a' an array and 'b' a normal int.
What does an array initializer list do?
'int[] coins = {1,5,10,25};' creates and initializes the array with length determined by the initializer.
Why can array values change even though its size cannot?
The array size is fixed, but the values in the positions can be reassigned.
What is array aliasing?
'int[] b = a;' copies the reference, both variables point to the same array object.
What is the output of 'System.out.println(a[0]);' after 'int[] a={1,2,3}; int[] b=a; b[0]=9;'?
'9', as 'a' and 'b' refer to the same array.
What does traversing an array mean?
Visiting every element using a loop.
Why does 'for (int x : arr) { x *= 2; }' not modify the original array?
x is a copy, reassigning it does not change arr.
What is the standard pattern for summing values in an array?
Initialize sum = 0, traverse, add each value to sum.
How do you count occurrences of a target in an array?
Set count = 0, traverse array, increment count when element matches the target.
How do you check if a target is present in an array?
Search until found; if not found by the end, report not found.
What is the method to find the last occurrence of a target in an array?
Search backwards from the end of the array.
Why must a loop comparing 'arr[i]' with 'arr[i+1]' use 'i < arr.length-1'?
To avoid out of bounds access with 'arr[i+1]'.
What does 'if (arr[i] > arr[i+1]) count++;' count?
Counts adjacent descents in the array.
What is the final array after 'arr[i] = arr[i] + arr[i-1]' for '{3,8,1,9,4}'?
Final array: '{3,11,12,21,25}'.
Why is 'int max = 0;' unsafe for finding max in an array?
If all values are negative, max will incorrectly remain 0.
What is the correct pattern for checking if every element satisfies a condition?
Return false immediately if one element violates condition, true after checking all.
What is the integer-division trap when calculating the array average?
Using int division truncates the fractional part. Cast to double to preserve it.
What is the physical size of an array?
The number of allocated positions, given by 'arr.length'.
What are physical and logical sizes of an array with length 1000 but 50 used?
Physical size = 1000; logical size = 50.
How do you safely add an element to an array using logical size?
Use 'if (lsize < arr.length) { arr[lsize] = value; lsize++; }'.
What does logical removal of the last element from an array involve?
Decrement the logical size, e.g., 'lsize--;'. The physical size remains unchanged.
How do you remove an element from the middle of an array?
Shift later elements left, decrement logical size.
What are the main differences between an array and an ArrayList?
Array: fixed size, can store primitives; ArrayList: resizable, stores objects.
Where does ArrayList fit in Java Collections?
ArrayList is a type of List within Java Collections.
Why is 'ArrayList
ArrayList stores objects, not primitives; Integer is the wrapper class.
What is autoboxing in Java?
Java converts a primitive into its wrapper object automatically.
What are the three size expressions to know?
Array: arr.length; String: str.length(); ArrayList: list.size().
What does 'list.add(value)' do?
Appends value to the end and increases size by 1.
What does 'list.add(index,value)' do?
Inserts value at index and shifts elements right.
What does 'list.set(index,value)' do?
Replaces the element at index without changing size.
What does 'list.get(index)' do?
Returns the element at index without changing the ArrayList.
What does 'list.remove(index)' do?
Removes and returns element at index, shifting later elements left.
What is the final state after 'add(0,
add(1,'D'); Final list: '[C,D,A,B]'. First insertion makes '[C,A,B]', then inserting D shifts A and B right.
What happens when 'int x=list.remove(1); int y=list.set(1,5);'?
x=20, y=30, final list = '[10,5,40]'. Remove returns 20, then 30 is replaced with 5.
What is the output after 'list.add(0, list.remove(list.size()-1));'?
'[Howard, Sheldon, Mickey, Raj]'. Remove last, insert at index 0.
Why is 'remove(1)' tricky in 'ArrayList
Index refers to a position; it may remove a different element than expected.
What is the major gotcha when removing from an ArrayList while traversing forward?
Removal shifts later elements left, may skip the next element.
What are three safe strategies for removing multiple matching ArrayList elements?
1) Forward for-loop with decremented index. 2) While-loop that increments only if no removal occurs. 3) Traverse backward.
Why is backward traversal good for repeated ArrayList removal?
No unprocessed elements are skipped since higher indexes have been processed.
What’s the danger in storing original ArrayList size before removal?
If the size decreases, using the old size can lead to index errors.
What happens when you modify an ArrayList inside an enhanced for loop?
Throws a ConcurrentModificationException.
How should String contents be compared in array/ArrayList algorithms?
Use '.equals()' for comparing contents.
What should 'countMatches(ArrayList
Count matched strings using '.equals()'.
How should 'removeShort(ArrayList
Traverse backward, removing short strings without skipping.
What does 'isSorted(int[] arr)' do?
Checks if the array is sorted by comparing adjacent pairs.
Why is returning true inside the loop wrong for 'isSorted'?
One correct pair does not prove the entire array is sorted.
What is the method to create a reversed copy of an array?
Create a new array and assign values from the original in reverse.
Why is an ArrayList a good return type for a method collecting scores above average?
It can grow dynamically, accommodating an unknown varying count.
How should 'aboveAverage(int[] scores)' conceptually work?
Calculate average, create a list, add scores greater than average, return.
What is the result of computing consecutive differences in an array?
The last element minus the first element, canceling interior values.
What exceptions to recognize for invalid indexes?
'ArrayIndexOutOfBoundsException' for arrays; 'IndexOutOfBoundsException' for ArrayLists.
What happens during indexed ArrayList add/remove?
'add(index,x)' shifts elements right; 'remove(index)' shifts elements left.
What is the first step when tracing complicated ArrayList code?
Rewrite the list after every change and recalculate indexes and size.
What should you do first when tracing complicated array code?
Check loop bounds and whether the array is being modified.
What are the highest-value gotchas to remember?
Last index = length-1, aliasing, enhanced-for copies, mutation effects, add vs set, removal shifts, skipping elements, String equality, Integer vs int, integer division issues.