THINK FIRST·CODE LATER

← Java Programming
Chapter 9 · Week 12–14

Arrays and Two-Dimensional Arrays

Answered 0/40 Correct 0
Sign in to save progress across devices
Q1

Which statement correctly creates an array of 10 integers?

Q2

What is the valid index range of an array created with new double[8]?

Q3

What is the default value of every element of new boolean[4]?

Q4

What is the default value of every element of new String[4]?

Q5

How do you obtain the number of elements of an array a?

Q6

What happens at run time when a program reads a[a.length]?

Q7

Which declaration is not legal Java?

Q8

What is the output?

int[] a = {3, 6, 9};
int[] b = a;
b[1] = 99;
System.out.print(a[1]);
Q9

Which statement really copies the contents of a into a new array?

Q10

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 + " ");
    }
}
Q11

What is the output?

int[] a = {2, 4, 6, 8};
int sum = 0;
for (int x : a) sum += x;
System.out.print(sum);
Q12

Which task can a for-each loop not perform on an int[]?

Q13

What is printed?

int[] a = new int[4];
a[2] = 7;
System.out.print(a[0] + a[2] + a.length);
Q14

What does System.out.println(a) print for int[] a = {1,2,3}?

Q15

Which call prints the contents of a one-dimensional array in a readable form?

Q16

A method with the heading static void reset(int[] a) sets every element to 0. After the call, the caller's array:

Q17

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]);
    }
}
Q18

Linear search returns which value when the key is absent?

Q19

Binary search may be used only when the array is:

Q20

How many comparisons does binary search need, at most, on a sorted array of 1000 elements?

Q21

In the binary search code, what is the purpose of low = mid + 1?

Q22

Which algorithm repeatedly selects the smallest remaining element and swaps it into position?

Q23

What is the time complexity of selection sort on n elements?

Q24

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]);
Q25

Which of the following statements correctly creates an empty two-dimensional array with dimensions 2 × 2 in Java?

Q26

For int[][] m = new int[3][5];, what are m.length and m[0].length?

Q27

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
Q28

How many elements does new int[4][3] contain in total?

Q29

What is printed?

int[][] t = {{1,2,3},{4,5},{6}};
System.out.print(t.length + " " + t[1].length + " " + t[2][0]);
Q30

An array whose rows have different lengths is called:

Q31

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);
Q32

What does the loop in the previous question compute?

Q33

Which loop correctly totals the elements of the second row of int[][] m?

Q34

In a partially filled array with capacity 100 and count 12, where is the next value stored?

Q35

Which call sorts an int[] in ascending order using the standard library?

Q36 Short answer

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.

Q37 Short answer

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.

Q38 Programming

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.

Q39 Programming

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.

Q40 Programming

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.