Be able to trace short array code by hand: predict default values, indexing results, aliasing effects, and exceptions. The single most important thing is that array variables hold references and Java passes those references by value, so element writes persist but reassigning the parameter does not.
Start practicing
Arrays and Methods — choose a session length
Free · No account required
Domain overview
This domain covers Java arrays and method mechanics: declaration, initialization, default values, bounds, reference aliasing, pass-by-value parameter passing, and return types. Questions present short code snippets and ask for output, compile errors, or runtime exceptions, plus scenario items on searching arrays of objects and methods that mutate array contents in place.
Exam objectives
Array declaration, initialization, and default values for int, boolean, and object element types
Zero-based indexing, length field, and ArrayIndexOutOfBoundsException on out-of-range access
Reference aliasing: assigning one array variable to another shares the same underlying array
Java pass-by-value: primitives copied, object references copied, so methods can mutate array elements
Assuming int[] b = a copies the array; it copies the reference, so writes through b are visible through a.
Confusing arr.length with arr.length(); length is a field for arrays, length() is a String method.
Believing a method can reassign the caller's array variable; reassigning the parameter only changes the local copy.
Click any question to see the full explanation and answer options, or start a focused practice session above.
A developer writes a method that takes an int array and returns the sum of its elements. The method signature is: 'public static int sumArray(int[] arr)'. Which statement correctly calls this method?
2Given the code snippet: 'int[] nums = {10, 20, 30, 40}; int sum = 0; for (int i = 0; i < nums.length; i++) { sum += nums[i]; }'. What is the value of sum after execution?
3A method 'public static void modifyArray(int[] arr) { arr[0] = 99; }' is called with 'int[] myArray = {1,2,3}; modifyArray(myArray); System.out.println(myArray[0]);'. What is the output?
4Which of the following correctly uses the shorthand array initializer to declare and initialize an array of strings with the elements "A", "B", and "C"?
5Given the method: 'public static void swapFirstTwo(int[] arr) { int temp = arr[0]; arr[0] = arr[1]; arr[1] = temp; }'. What is the effect of calling this method with an array of length 1?
6A developer writes a method that accepts a variable number of int arguments and returns their product. Which method signature correctly implements this?
7What is the output of the following code? int[] a = {1,2,3}; int[] b = a; b[0] = 99; System.out.println(a[0]);
8Which of the following correctly describes the effect of the method call 'Arrays.sort(myArray)' on an array of objects that do not implement Comparable?
9Which TWO statements are true about method overloading in Java?
10Which TWO statements are true about passing arrays to methods in Java?
11What is the output?
12A team is developing a Java application for an online store. The application has a class 'Inventory' that maintains an array of 'Product' objects. The method 'public void addProduct(Product p)' is intended to add a product to the array. The current implementation uses a fixed-size array of length 100. However, the business has grown, and the array may exceed its capacity. The team needs a solution that allows dynamic resizing without changing the method signature. Which approach should the team take?
13A developer is working on a Java program that processes sensor data. The data is stored in a 2D array 'double[][] readings', where each row represents a sensor and each column a time interval. The method 'public static double[] averagePerSensor(double[][] data)' should compute the average reading for each sensor (row) and return a 1D array of averages. The developer writes the following implementation: 'double[] result = new double[data.length]; for (int i = 0; i < data.length; i++) { double sum = 0; for (int j = 0; j < data[i].length; j++) { sum += data[i][j]; } result[i] = sum / data[i].length; } return result;'. However, the program sometimes throws a NullPointerException. What is the most likely cause?
14Arrange the steps to create an object from a class in Java in the correct order.
15A banking application stores daily transaction amounts in an array. Which declaration correctly creates an array of 31 double values?
16Given the array declaration: int[] data = new int[5];, what is the value of data[2] after initialization?
17Consider a method that takes an int array and modifies it: public static void doubleArray(int[] arr) { for (int i = 0; i < arr.length; i++) { arr[i] *= 2; } }. What is the output of: int[] nums = {1, 2, 3}; doubleArray(nums); System.out.println(nums[1]);
18A developer writes two methods: public void process(int a) { ... } and public void process(double a) { ... }. Which method is called by process(10)?
19A developer needs to search an unsorted array of 100,000 customer IDs (int) for a specific ID. Which approach is most efficient for a single search?
20Which TWO statements are correct about array declaration and initialization in Java?
21Which TWO are valid ways to pass an array to a method in Java?
22Which THREE statements are true about method overloading in Java?
23What is the output when the main method is executed? ```java public class Test { public static double add(double a, double b) { return a + b; } public static void main(String[] args) { System.out.println(add(5, 10)); } } ```
24What is the output of the following code? ```java public class FindIndex { public static int find(int[] arr, int target) { for (int i = 0; i < arr.length; i++) { if (arr[i] == target) return i; } return -1; } public static void main(String[] args) { int[] values = {3, 7, 2}; System.out.print(find(values, 7) + " " + find(values, 5)); } } ``` Options: A: 0 -1 B: -1 -1 C: 1 -1 D: 1 1
25A method is needed to return a new array where each element is doubled. Which method signature correctly accomplishes this?
26Given an array arr of length 5, which code snippet correctly creates a copy using System.arraycopy?
27A method is declared as 'public void printElements(int... numbers)'. Which invocation will cause a compilation error?
28A method 'public static void sort(int[] arr)' sorts the array in place. After calling 'sort(data)', the original array 'data' is changed. Which design issue does this demonstrate?
29A method 'public static double average(int[] numbers) { int sum = 0; for (int i = 0; i < numbers.length; i++) sum += numbers[i]; return sum / numbers.length; }' is called with array {10, 20, 30}. What change is needed to return the correct average?
30A method 'public static int[] generate() { int[] result = new int[10]; for (int i = 0; i < result.length; i++) result[i] = i * 2; return result; }' is defined. Which statement correctly calls this method and stores the result?
31Given 'int[] source = {1,2,3,4,5};' and 'int[] dest = new int[3];' which code correctly copies the first three elements from source to dest using System.arraycopy?
32A method 'public static void modify(int[][] matrix) { matrix[0][0] = 99; }' is called with 'int[][] values = {{1,2},{3,4}}; modify(values);'. What is the value of values[0][0] after the call?
33Which TWO methods correctly modify the passed array in place?
34Which THREE statements are true about passing arrays to methods in Java?
35Refer to the exhibit. Which overloaded methods cause this compilation error?
36Consider the following code that attempts to swap the first two elements of an array using a method: public static void swap(int[] arr) { int temp = arr[0]; arr[0] = arr[1]; arr[1] = temp; } public static void main(String[] args) { int[] values = {1, 2}; swap(values); System.out.println(values[0] + " " + values[1]); } What is the output?
37A developer implements a method to check if a number exists in an array using binary search. The array is not sorted. What will happen?
38Which approach does NOT create a new array that is independent of the original?
39A method is designed to reverse an array of integers. The method signature is: public static void reverse(int[] arr). The method correctly reverses the array. Which statement is true about the method?
40A developer writes a method that takes a variable number of integer arguments and returns the maximum. Which method signature is correct?
41Consider the following code: int[] arr = new int[5]; for (int i = 0; i <= arr.length; i++) { arr[i] = i; } System.out.println(arr[0]); What is the result?
42A method that calculates the average of an array of doubles is defined as: public static double average(double[] values) { double sum = 0; for (double v : values) sum += v; return sum / values.length; } Which call is valid?
43Given the code: int[] a = {1, 2, 3}; int[] b = {4, 5}; a = b; b[0] = 99; System.out.println(a[0]); What is the output?
44Given the code: public class Test { public static void change(int[] arr) { arr = new int[]{10, 20}; } public static void main(String[] args) { int[] arr = {1, 2}; change(arr); System.out.println(arr[0]); } } What is the output?
45Which three statements about method overloading are true? (Select three.)
46Which two statements about passing arrays to methods are correct? (Select two.)
47Given the method: public static void modify(int[] data) { data = new int[]{10,20}; } What is the output of: int[] vals = {1,2}; modify(vals); System.out.println(vals[0]);
48A developer writes a method that accepts an array and returns the sum of all elements. Which implementation is correct if the array might be null?
49What is the result of the following code? int[] arr = new int[3]; arr[3] = 5; System.out.println(arr[3]);
50A developer wants to sort an array of primitive ints in descending order. Which approach will work without using third-party libraries?
51Which two statements about method parameter passing in Java are true? (Choose two.)
52Which three statements about arrays are correct? (Choose three.)
53A company manages employee data stored in an array of Employee objects. The HR application frequently needs to find an employee by ID. The current implementation uses a linear search through the array each time. Performance reports indicate that this search is becoming a bottleneck as the company grows. The array is not sorted, and the company does not want to sort it because the order is meaningful for display. The array is large and frequently updated. The development team considers several options to improve the search performance without changing the array order. Which approach should they implement?
54A team is developing a financial application that processes large arrays of market data. They have a method that calculates moving averages by copying a subarray for each window. The current implementation creates a new array for each window using System.arraycopy. The application is running slowly in production. The team identifies that the method is called millions of times per minute. The array is large and the window size is small. The garbage collector overhead is high due to many short-lived arrays. Which optimization should they apply to reduce object creation and improve performance?
55In a login system, the authentication method receives an array of user roles as a String[] and checks if a specific role is present. The array may be large (thousands of roles), and the method is called frequently for each user request. Performance is critical. The array is static and does not change after initialization. Which approach is most efficient for repeated checks?
56A new developer writes a method that accepts an array and intends to swap the first and last elements. The code is: public static void swapEnds(int[] arr) { int temp = arr[0]; arr[0] = arr[arr.length-1]; arr[arr.length-1] = temp; } What is a potential issue with this method if the array is empty?
57A developer is writing a method that should double every element in an int array passed as an argument. The method signature is public static void doubleAll(int[] nums). The developer wants the caller's original array to reflect the doubled values. Which implementation correctly achieves this?
58A developer writes a utility method that accepts a variable number of int arguments and prints their sum. The method is declared as public static int sum(int... values). Which statement correctly describes how this method can be invoked?
59A developer at a logistics company defines this method: public static void addFee(double[] charges) { charges = new double[]{5.0, 10.0}; charges[0] = 99.0; } It is invoked as: double[] fees = {1.0, 2.0}; addFee(fees); System.out.println(fees[0]); What is printed?
60A developer is reviewing a method that searches a two-dimensional int array for a target value. The method must return true if the target appears anywhere in the array. Which two statements about passing and using a two-dimensional array as a method parameter are correct? (Choose two.)
61A developer writes the following code: public class Test { public static void main(String[] args) { int[] a = {1, 2, 3}; int[] b = a; b[0] = 99; System.out.println(a[0] + " " + b[0]); } } What is the output?
62A developer is reviewing code that iterates over a one-dimensional int array named readings using a standard for loop. The loop must access every element exactly once without any out-of-bounds errors. Which loop condition should be used?
63A developer writes a method that is intended to double each element in an int array. The method is: public static void doubleAll(int[] nums) { for (int i = 0; i < nums.length; i++) { nums[i] *= 2; } } What is the effect when the following code is executed? int[] values = {1, 2, 3}; doubleAll(values); System.out.println(values[1]);
64A developer writes a method that accepts a two-dimensional int array representing a grid of scores and returns the sum of all elements. Which construct correctly visits every element in every row?
65A payroll system stores weekly hours in an int array. A developer writes a method that should double every element in the array and return the sum of the modified values. The method signature is public static int process(int[] hours). Inside the method, the developer uses a for-each loop: for (int h : hours) { h = h * 2; } and then sums the elements. What is the result when the method is called with an array containing {1, 2, 3}?
66A developer writes a utility that must return two values, a minimum and a maximum, from an array of measurements. The method below is proposed: public static int[] minMax(int[] values) { // implementation omitted return new int[]{min, max}; } Which statement about this method signature and return statement is correct?
67A developer defines a method to compute the average of an int array: public static double average(int[] values). The developer calls average(3, 7, 11) and the code fails to compile. What is the reason?
68A developer is writing a method that accepts a two-dimensional array of sensor readings and returns the average of the first row. The parameter is declared as int[][] grid. Which expression correctly obtains the number of columns in the first row of the argument?
69A developer writes a method that receives an int array and intends to reverse its contents in place. Inside the method the parameter is reassigned to a newly created array holding the reversed values. After the method returns, the caller's original array is unchanged. Why?
70A class contains these two methods: public static void log(String message, int... codes) { } public static void log(String message, int code) { } A developer calls log("AUDIT", 42). Which statement describes what happens?
71A developer defines a class with two methods: public void log(String message) and public void log(String message, int level). A call site uses log("Ready"). Which statement best describes what happens at compile time and runtime?
72A developer is writing a method that must accept a two-dimensional array of int values and compute the sum of all elements. The array is declared as int[][] grid. The array may be ragged, meaning rows can have different lengths. Which two statements are correct about iterating over grid to compute the total? (Choose two.)
Deep-dive questions
The most-searched questions in this domain — detailed explanations, worked examples, full answer breakdowns.
Be able to trace short array code by hand: predict default values, indexing results, aliasing effects, and exceptions. The single most important thing is that array variables hold references and Java passes those references by value, so element writes persist but reassigning the parameter does not.
The Courseiva 1Z0-811 question bank contains 72 questions in the Arrays and Methods domain, covering the 14% of the exam attributed to this domain in the official Oracle blueprint. Click any question to see the full explanation and answer breakdown.
Start with a 10-question focused session to identify your baseline accuracy in this domain. Read every explanation — even for questions you answer correctly — to understand the reasoning. Once you score consistently above 80%, move to a 20–30 question session to confirm depth before moving to the next domain.
Yes — the session launcher on this page draws questions exclusively from the Arrays and Methods domain. Choose 10, 20, 30, or 50 questions for a focused session, or click individual questions to review them one by one.
Save your results, see per-domain analytics, and get readiness scores — free, for every certification.
Sign Up FreeFree forever · Every certification included