Arrays & ArrayLists — Answer Key
Chapter 5 · Instructor Reference
⚠ For instructor use only
PART 1 — Fill in the Blanks Answers
# Statement Answer
1 Array indices in Java always start at __________. 0
Once an array is created, its size is __________ and
2 fixed
cannot be changed.
To find the number of elements in an array, use the
3 length
__________ property.
An __________ is a dynamic data structure that can
4 ArrayList
grow or shrink at runtime.
To add an element to an ArrayList, use the
5 add()
__________ method.
To retrieve an element from an ArrayList at a specific
6 get()
index, use the __________ method.
Primitive types like int cannot be directly stored in an
7 Integer
ArrayList. Instead, use the __________ class.
A __________ class converts primitive types into
8 wrapper
objects.
9 To sort an ArrayList, you can use __________. [Link]()
Accessing an invalid array index throws an
10 ArrayIndexOutOfBoundsException
__________ exception.
A __________ occurs when you try to use a reference
11 null / NullPointerException
that points to nothing.
Exception handling in Java is done using a
12 try-catch
__________ block.
PART 2 — True / False Answers
# Statement Answer Explanation
Arrays in Java can hold elements of different Arrays must hold elements of the
1 FALSE
data types. same type.
2 Array indices start at 1 in Java. FALSE Array indices start at 0.
1
Prepared by: Dr. Reem Alomari
# Statement Answer Explanation
The length of an array is accessed using It's [Link] (property, not
3 FALSE
[Link](). method).
ArrayLists can automatically resize as
4 TRUE —
elements are added or removed.
You can use primitive types directly in an Must use wrapper classes (Integer,
5 FALSE
ArrayList without wrapper classes. Boolean, etc.).
The get() method is used to retrieve an
6 TRUE —
element from an ArrayList.
7 An array of size 5 has valid indices from 0 to 5. FALSE Valid indices: 0 to 4 (size - 1).
[Link]() can be used to sort an Use [Link](list,
8 TRUE
ArrayList in descending order. [Link]())
That's
A NullPointerException occurs when ArrayIndexOutOfBoundsException.
9 FALSE
accessing an array element that doesn't exist. NullPointerException occurs when
using null reference.
The finally block in exception handling always
10 executes regardless of whether an exception TRUE —
occurred.
PART 3 — Multiple Choice Answers
# Question Answer Explanation
1 Correct way to declare an array of 10 integers? B int[] array = new int[10];
Default value for int array elements when
2 C 0 (numeric default)
created?
Which statement correctly adds an element to
3 B [Link]("item");
ArrayList?
4 Wrapper class for primitive type int in ArrayList? D Integer (capital I)
What happens accessing array[5] in array of ArrayIndexOutOfBoundsException
5 C
size 5? (valid: 0-4)
Method that returns number of elements in
6 C [Link]() — it's a method
ArrayList?
What does ArrayList<String> names = new Creates empty ArrayList for
7 B
ArrayList<>(); do? strings
8 To sort ArrayList 'numbers' in ascending order? C [Link](numbers);
2
Prepared by: Dr. Reem Alomari
PART 4 — Code Analysis Answers
Question 1
int[] numbers = {10, 20, 30, 40, 50};
[Link](numbers[2]);
[Link]([Link]);
Line 1: 30 (index 2 is the third element)
Line 2: 5 (array has 5 elements)
Question 2
int[] scores = new int[5];
scores[0] = 85;
scores[1] = 90;
[Link](scores[0]);
[Link](scores[3]);
Line 1: 85 (assigned value)
Line 2: 0 (default value for uninitialized int)
Explanation: When an int array is created with 'new', all elements are automatically initialized to 0.
Only scores[0] and scores[1] were explicitly assigned values.
Question 3
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
[Link]([Link](1));
[Link]([Link]());
Line 1: Banana (index 1 → second element)
Line 2: 3 (three elements in list)
Question 4 — Loop Through Array
int[] nums = {2, 4, 6, 8};
int sum = 0;
for (int i = 0; i < [Link]; i++) {
sum += nums[i];
}
3
Prepared by: Dr. Reem Alomari
[Link](sum);
Output: 20
Calculation: 2 + 4 + 6 + 8 = 20
Question 5 — Enhanced For Loop
String[] colors = {"Red", "Green", "Blue"};
for (String color : colors) {
[Link](color + " ");
}
Output: Red Green Blue
Note: print() keeps output on same line; enhanced for-loop iterates through all elements.
Question 6 — Spot the Error
int[] values = {5, 10, 15};
[Link](values[3]);
Error: ArrayIndexOutOfBoundsException
Why: Array has 3 elements (indices 0, 1, 2). Index 3 doesn't exist.
Fix: Change to values[2] (last element) or values[0], values[1], or values[2]
4
Prepared by: Dr. Reem Alomari
PART 5 — ArrayList Operations Answers
# Operation ArrayList State After Operation
1 [Link](10); [10]
2 [Link](20); [10, 20]
3 [Link](30); [10, 20, 30]
[10, 30] — removes element at index 1 (which was
4 [Link](1);
20)
5 [Link](1, 25); [10, 25, 30] — inserts 25 at index 1
6 [Link](0, 15); [15, 25, 30] — replaces element at index 0 with 15
7 int x = [Link](2); [15, 25, 30] (unchanged) x = 30
8 int size = [Link](); [15, 25, 30] (unchanged) size = 3
PART 6 — Code Writing Solutions
Challenge 1: Find the Maximum
int[] numbers = {15, 42, 8, 23, 16};
int max = numbers[0]; // assume first element is max
for (int i = 1; i < [Link]; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
[Link]("Maximum value: " + max); // Output: 42
Challenge 2: Reverse an ArrayList
ArrayList<String> names = new ArrayList<>();
[Link]("Alice");
[Link]("Bob");
[Link]("Charlie");
// Solution 1: Reverse using loop
for (int i = [Link]() - 1; i >= 0; i--) {
[Link]([Link](i));
}
// Output: Charlie, Bob, Alice
5
Prepared by: Dr. Reem Alomari
// Solution 2: Using Collections
[Link](names);
[Link](names); // [Charlie, Bob, Alice]
Challenge 3: Count Even Numbers
int[] data = {12, 7, 18, 5, 22, 9};
int count = 0;
for (int num : data) {
if (num % 2 == 0) {
count++;
}
}
[Link]("Even numbers count: " + count); // Output: 3
// Even numbers: 12, 18, 22
6
Prepared by: Dr. Reem Alomari
PART 7 — Exception Handling Answers
Scenario Analysis
1. Code Snippet:
int[] arr = {1, 2, 3};
[Link](arr[5]);
Exception? YES
Exception: ArrayIndexOutOfBoundsException
Reason: Array has indices 0-2, but trying to access index 5.
2. Code Snippet:
int[] arr = null;
[Link]([Link]);
Exception? YES
Exception: NullPointerException
Reason: Trying to access .length on a null reference.
3. Code Snippet:
ArrayList<Integer> list = new ArrayList<>();
[Link](10);
[Link]([Link](0));
Exception? NO
Output: 10 (This code works correctly — element added at index 0 and then retrieved)
Write Try-Catch Code — Solution
int[] numbers = {10, 20, 30};
try {
[Link](numbers[5]);
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Error: Index out of bounds!");
[Link]("Valid indices are 0 to " + ([Link] - 1));
}
// Alternative with generic Exception:
try {
[Link](numbers[5]);
} catch (Exception e) {
[Link]("Something went wrong: " + [Link]());
7
Prepared by: Dr. Reem Alomari
}
PART 8 — Arrays vs ArrayLists Comparison
Feature Array ArrayList
Size Fixed (cannot change) Dynamic (grows/shrinks)
Can resize? No Yes
ArrayList<Integer> list = new
Syntax to declare int[] arr = new int[5];
ArrayList<>();
No — must use wrapper classes
Can hold primitives? Yes (int, double, etc.)
(Integer, Double)
Method to get size [Link] (property) [Link]() (method)
Method to add element arr[index] = value; [Link](value);
Slightly slower (overhead for
Performance Faster (direct access)
resizing)
🎯Quick Wins, Don’t miss this out !
• Arrays are fixed-size; ArrayLists are dynamic.
• Array indices ALWAYS start at 0 and end at length - 1.
• ArrayLists require wrapper classes for primitives (Integer, not int).
• Common ArrayList methods: add(), get(), remove(), set(), size(), clear().
• Always use try-catch when array index could be invalid.
• [Link]() works on ArrayLists, [Link]() works on arrays.
8
Prepared by: Dr. Reem Alomari