Select the correct answer based on your knowledge of Java Arrays, ArrayLists, and Exception Handling.
1. Which of the following is the main advantage of an ArrayList over a standard Array?
ArrayLists can store primitive data types directly.
ArrayLists automatically shrink and grow in size as elements are added or removed.
ArrayLists process elements faster than standard arrays.
ArrayLists do not require importing any packages from java.util.
Answer: B
2. If you want to store primitive double values inside an ArrayList, what type must you specify?
ArrayList<double>
ArrayList<Double>
ArrayList<decimal>
ArrayList<Float>
Answer: B
(Explanation: ArrayLists require wrapper classes for primitive types. The wrapper for double is Double.)
3. In exception handling, what is the purpose of the catch block?
To execute code that might cause an error.
To run cleanup code regardless of whether an error occurred.
To handle the error and execute alternative code if an exception is thrown in the try block.
To terminate the program immediately when a bug is found.
Answer: C
Question 2: Code Tracing (8 points)
Read the following code snippets carefully. Determine exactly what will be printed to the console.
1. What is the output of the following array loop?
int[] numbers = {10, 15, 20, 25, 30};
int count = 0;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 == 0) {
count++;
}
}
System.out.println("Result: " + count);
Output: Result: 3
(Explanation: The loop checks for even numbers. 10, 20, and 30 are even, so the count increases 3 times.)
2. What is the output of the following ArrayList manipulation?
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> colors = new ArrayList<String>();
colors.add("Red");
colors.add("Blue");
colors.add("Green");
colors.set(1, "Yellow");
colors.remove(0);
System.out.println(colors.get(0));
}
}
Output: Yellow
(Explanation:
1. List starts as: [Red, Blue, Green]
2. Index 1 changed to Yellow: [Red, Yellow, Green]
3. Index 0 (Red) is removed. Everything shifts left: [Yellow, Green]
4. colors.get(0) now returns Yellow.)
Question 3: Programming Tasks (10 points)
Write complete Java code to solve the following standard programming problems.
1. Finding the Maximum Value in an Array:
Write a complete Java method named findMax that takes an array of integers as a parameter. The method should use a loop to find and return the largest number in the array. Assume the array is not empty.
public int findMax(int[] arr) {
int max = arr[0]; // Assume first element is the largest
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i]; // Update max if a larger number is found
}
}
return max;
}
2. Basic Exception Handling:
Write a complete main method that does the following:
Creates an integer array: int[] scores = {85, 90, 95};
Uses a try-catch block to attempt to print the score at index 4.
Catches the Exception and prints: "Error: Index out of bounds."
public static void main(String[] args) {
int[] scores = {85, 90, 95};
try {
System.out.println("Score: " + scores[4]);
} catch (Exception e) {
System.out.println("Error: Index out of bounds.");
}
}