Array vs ArrayList
Creating Lists
Add / Insert / Autoboxing
Access / Modify
Remove / Traverse
100

Define, concisely, two fundamental differences between a Java array and an ArrayList.

Arrays have fixed size; ArrayLists are dynamic. Arrays can hold primitives directly; ArrayLists hold only objects (use wrapper classes). Access syntax differs: arr[i] vs. list.get(i) / arr.length vs. list.size().

100

Write the import statement required to use ArrayList in Java.

import java.util.ArrayList;

100

Which ArrayList method appends an element to the end of the list?

add(element)

100

Which ArrayList method retrieves the element at index i?

get(index)

100

Name the two main overloads of remove(...) for ArrayList and briefly explain the difference

remove(int index) removes element at that index; remove(Object o) removes the first occurrence of that object value.

200

Given the analogy “parking lot painted lines vs. magical parking lot,” explain what each represents and why the analogy fits.

Painted lines = array (fixed capacity). Magical parking lot = ArrayList (resizes dynamically). Fits because arrays waste or limit space; ArrayLists adapt.

200

Write the declaration and instantiation line (using generics and the diamond operator) for an ArrayList that will store Strings called students.

ArrayList students = new ArrayList<>();

200

Write code that inserts "Blueberry" at index 1 into an existing ArrayList fruits.

fruits.add(1, "Blueberry");

200

Given ArrayList colors = new ArrayList<>(); colors.add("Red"); colors.add("Green"); colors.add("Blue"); write the line of code to change "Green" to "GOLD".

colors.set(1, "GOLD");

200

Given ArrayList animals = ["Cat","Dog","Bird"], write code to remove "Dog" by value.

animals.remove("Dog");

300

Explain why ArrayLists cannot hold primitive types directly and name the Java language feature that makes using primitives with ArrayLists convenient.

ArrayLists hold objects; primitives must use wrapper classes (e.g., Integer). Autoboxing converts primitives to their wrappers automatically.

300

Explain what the diamond operator <> does and from which Java version it’s available.

Diamond operator <> allows the compiler to infer generic type parameters on instantiation (available Java 7+).

300

Explain autoboxing with a short example showing adding a primitive int to an ArrayList.

Example: ArrayList scores = new ArrayList<>(); scores.add(85); // autoboxes int 85 to Integer.valueOf(85)

300

Explain what happens (and what exception, if any) when you call list.get(list.size()) on a non-empty ArrayList.

list.get(list.size()) throws IndexOutOfBoundsException because valid indices are 0..size()-1

300

Explain the tricky ambiguity when calling remove(1) on ArrayList numbers and how to remove the value 1 instead of the element at index 1.

remove(1) is interpreted as remove by index; to remove the value 1: numbers.remove(Integer.valueOf(1));

400

Given code: int[] a = new int[3]; ArrayList b = new ArrayList<>(); List two runtime or compile-time behaviors that will differ between these two when you attempt to store more elements than currently allocated.

Array will throw an ArrayIndexOutOfBounds if you try to store past capacity or require manual copying to enlarge; ArrayList will resize automatically (may incur copying internally), and you cannot exceed heap memory; also ArrayList can increase capacity, arrays cannot.

400

Show code to create an ArrayList of Doubles named prices and add three values: 19.99, 5.0, 12.5.

ArrayList prices = new ArrayList<>(); prices.add(19.99); prices.add(5.0); prices.add(12.5);

400

Show code that adds the integers 10, 20, 30 to an ArrayList, then inserts 15 between 10 and 20.

ArrayList nums = new ArrayList<>(); nums.add(10); nums.add(20); nums.add(30); nums.add(1, 15); // list: 10,15,20,30

400

Show code that uses a for loop indexed by i to print all elements of an ArrayList names.

for (int i = 0; i < names.size(); i++) { System.out.println(names.get(i)); }

400

After removing an element at index 2 from a list, what happens to the indices of elements that followed index 2? Explain.

Elements after index 2 shift left one position; their indices decrease by 1

500

Describe a real-world application scenario (not from the notes) where an ArrayList is a better choice than a fixed array. Explain tradeoffs in performance and memory.

(Example) Dynamic chat participants list or undo stack with unpredictable size; tradeoffs: ArrayList is more convenient and flexible; arrays are slightly faster and more memory efficient.

Creating Lists 100 — import java.util.ArrayList;

500

Explain why using raw ArrayList (no generics) is discouraged; give an example that shows a potential runtime problem.

Raw types bypass compile-time type checks; e.g., ArrayList raw = new ArrayList(); raw.add("text"); raw.add(3); ArrayList s = raw; String t = s.get(1); // ClassCastException at runtime.

500

Compare complexity (big-O) of appending an element vs. inserting at an arbitrary index in an ArrayList. Explain why they differ.

Appending average-case O(1) amortized (because occasional resize); inserting at index requires shifting subsequent elements O(n). Shifting causes linear time.

500

Distinguish between set(index, value) and add(index, value) in behavior and effect on size, with a short code example.

set(index, value) replaces element at index; size unchanged. add(index, value) inserts and shifts subsequent elements; size increases by 1

500

Show three different ways to traverse an ArrayList and discuss one scenario where each method is preferable (include code snippets: indexed for loop, enhanced for-each, Iterator).

Indexed for loop: for (int i=0;i<list.size();i++) ... — useful when you need indices or to modify by index. Enhanced for-each: for (T item : list) ... — clean for read-only traversal. Iterator it = list.iterator(); while(it.hasNext()){T x = it.next(); if(cond) it.remove(); } — necessary when removing elements during traversal.

M
e
n
u