1Z0-811 Arrays and Methods • Complete Question Bank
Complete 1Z0-811 Arrays and Methods question bank — all 0 questions with answers and detailed explanations.
Drag a concept onto its matching description — or click a concept then click the description.
Executes a block based on a boolean condition
Selects one of many code blocks based on a value
Iterates a fixed number of times
Repeats while a condition is true
Executes at least once then repeats while condition true
Refer to the exhibit.
public class ArrayTest {
public static void main(String[] args) {
int[] arr = new int[3];
arr[0] = 5;
arr[1] = 10;
arr[2] = 15;
modify(arr);
System.out.println(arr[0]);
}
public static void modify(int[] a) {
a = new int[3];
a[0] = 100;
}
}public class Example {
public static void main(String[] args) {
int[] nums = {1, 2, 3};
int[] result = process(nums);
System.out.println(result[0]);
}
public static int[] process(int[] input) {
int[] output = new int[input.length];
for (int i = 0; i < input.length; i++) {
output[i] = input[i] * 2;
}
return output;
}
}Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at ArrayExample.main(ArrayExample.java:6)A payroll application must accumulate totals from a variable number of daily pay amounts. A developer writes:
public static double total(double... amounts) {double sum = 0;
for (double a : amounts) { sum += a; }
return sum;
}Which two statements about invoking this method are correct? (Choose two.)
Drag or tap steps into the slots.
What 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));
} }
```
Refer to the exhibit.
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
double result = add(5, 10);
System.out.println(result);
}
}What 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
Refer to the exhibit.
public class Search {
public static int find(int[] data, int target) {
for (int i = 0; i < data.length; i++) {
if (data[i] == target) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] values = {3, 7, 2, 9, 4};
System.out.println(find(values, 7) + " " + find(values, 5));
}
}javac Test.java
Test.java:5: error: reference to print is ambiguous
print(null);
^
both method print(String[]) in Test and method print(String...) in Test matchConsider 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?
Consider 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?
A 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?
Given 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?
Given 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?
A 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?
A 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?
A 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]);
A 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?
A 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?