What happens when the following code executes?
int[] arr = new int[3];
System.out.println(arr[0]);
arr[5] = 10;
Loading test…
12 questions
What happens when the following code executes?
int[] arr = new int[3];
System.out.println(arr[0]);
arr[5] = 10;
What is the output of the following code?
int[] arr = {5, 10, 15, 20};
System.out.println(arr.length);
System.out.println(arr[2]);
What is the output of the following code?
String[] words = {"cat", "bird", "ant", "dog"};
String first = words[0];
for (String w : words) {
if (w.compareTo(first) < 0) {
first = w;
}
}
System.out.println(first);
What is the output of the following code?
int[] a = {1, 2, 3};
int[] b = a;
b[1] = 99;
System.out.println(a[1]);
What is the output of the following code?
int[] arr = {10, 20, 30, 40, 50};
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
What is the output of the following code?
int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
arr[i] = i * i;
}
System.out.println(arr[3]);
What is the value of result after the following code executes?
int[] arr = {2, 5, 3, 8, 1};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
result++;
}
}
What is printed by the following code?
int[] arr = {3, 7, 2, 9, 4};
int sum = 0;
for (int val : arr) {
sum += val;
}
System.out.println(sum / arr.length);
What is the output of the following code?
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
System.out.println(arr[0] + " " + arr[2] + " " + arr[4]);
What is the output of the following code?
int[] nums = {3, 1, 4, 1, 5};
int max = nums[0];
for (int n : nums) {
if (n > max) {
max = n;
}
}
System.out.println(max);
What is the output of the following code?
int[] arr = {4, 2, 7, 1, 5};
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
System.out.println(arr[0] + " " + arr[1]);
What is the output of the following code?
int[] arr = {5, 3, 8, 1, 9};
int target = 8;
int index = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
index = i;
}
}
System.out.println(index);