Which statement correctly creates an array of 10 integers?
What is the valid index range of an array created with new double[8]?
What is the default value of every element of new boolean[4]?
What is the default value of every element of new String[4]?
How do you obtain the number of elements of an array a?
What happens at run time when a program reads a[a.length]?
Which declaration is not legal Java?
What is the output?
int[] a = {3, 6, 9};
int[] b = a;
b[1] = 99;
System.out.print(a[1]);
Which statement really copies the contents of a into a new array?
What is the output of the following Java program?
public class TestArrayCopy {
public static void main(String[] args) {
int[] src = {10, 20, 30, 40, 50};
int[] dest = {1, 2, 3, 4, 5};
System.arraycopy(src, 1, dest, 2, 2);
for (int i : dest) System.out.print(i + " ");
}
}
What is the output?
int[] a = {2, 4, 6, 8};
int sum = 0;
for (int x : a) sum += x;
System.out.print(sum);
Which task can a for-each loop not perform on an int[]?
What is printed?
int[] a = new int[4];
a[2] = 7;
System.out.print(a[0] + a[2] + a.length);
What does System.out.println(a) print for int[] a = {1,2,3}?
Which call prints the contents of a one-dimensional array in a readable form?
A method with the heading static void reset(int[] a) sets every element to 0. After
the call, the caller's array:
What is the output?
public class Grow {
static void grow(int[] a) { a = new int[10]; a[0] = 5; }
public static void main(String[] args) {
int[] nums = {1, 2, 3};
grow(nums);
System.out.print(nums.length + " " + nums[0]);
}
}
Linear search returns which value when the key is absent?
Binary search may be used only when the array is:
How many comparisons does binary search need, at most, on a sorted array of 1000 elements?
In the binary search code, what is the purpose of low = mid + 1?
Which algorithm repeatedly selects the smallest remaining element and swaps it into position?
What is the time complexity of selection sort on n elements?
What is the output?
int[] a = {5, 1, 4};
int t = a[0]; a[0] = a[2]; a[2] = t;
System.out.print(a[0] + "" + a[1] + a[2]);
Which of the following statements correctly creates an empty two-dimensional array with dimensions 2 × 2 in Java?
For int[][] m = new int[3][5];, what are m.length and m[0].length?
Which line causes an ArrayIndexOutOfBoundsException?
String[][] matrix = new String[1][2];
matrix[0][0] = "Don't think you are, know you are."; // m1
matrix[0][1] = "I'm trying to free your mind Neo"; // m2
matrix[1][0] = "Is all around you"; // m3
How many elements does new int[4][3] contain in total?
What is printed?
int[][] t = {{1,2,3},{4,5},{6}};
System.out.print(t.length + " " + t[1].length + " " + t[2][0]);
An array whose rows have different lengths is called:
What is the output?
int[][] m = {{1,2},{3,4}};
int sum = 0;
for (int r = 0; r < m.length; r++)
for (int c = 0; c < m[r].length; c++)
if (r == c) sum += m[r][c];
System.out.print(sum);
What does the loop in the previous question compute?
Which loop correctly totals the elements of the second row of int[][] m?
In a partially filled array with capacity 100 and count 12, where is the next
value stored?
Which call sorts an int[] in ascending order using the standard library?
Explain why int[] b = a; is not a copy, and write the loop that
makes a genuine copy of a into a new array b.
A method must return both the largest value of an array and its index. Describe two different ways to do this in Java, given that a method returns only one value.
Write a method public static int findMode(int[] array) that
returns the mode (the most frequent value) of a sorted array. If several values tie, return the
first one. Test it with {2,5,6,7,7,10,10,11,11,13,15,15,15,19,20,20}, whose mode is 15.
Write a method public static int leastFrequent(int[] arr) that
returns the element occurring the fewest times; if several tie, return the one with the lowest
index. For {1,3,2,1,2,2,3,1} the answer is 3, and for {10,20,30} it is 10.
Write a program that computes and prints the matrix sum
C = A + B for
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] and
B = [[9, 8, 7], [6, 5, 4], [3, 2, 1]], using two-dimensional arrays and nested
loops, and printing C as a properly aligned grid.