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 steps to the numbered slots on the right, or tap a step then tap a slot.
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 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);
}
}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;
}
}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 matchException in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at ArrayExample.main(ArrayExample.java:6)Consider 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?