Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 18:38:34 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 5: Arrays

Sections 5.2 Declaring Array Variables and Creating Arrays
1   What is the representation of the third element in an array called a?

A. a[2]
B. a(2)
C. a[3]
D. a(3)

2   Which of the following is incorrect?

A. int[] a = new int[2];
B. int a[] = new int[2];
C. int[] a = new int(2);
D. int a = new int[2];
E. int a() = new int[2];

Section 5.3 Initializing and Processing Arrays
3   Analyze the following code.

public class Test {
   public static void main(String[] args) {
     int[] x = new int[3];
     System.out.println("x[0] is " + x[0]);
   }
}

A. The program has a syntax error because the size of the array wasn't specified when declaring the array.
B. The program has a runtime error because the array elements are not initialized.
C. The program runs fine and displays x[0] is 0.
D. None of the above.

4   Which of the following statements is valid?

A. int i = new int(30);
B. double d[] = new double[30];
C. int[] i = {3, 4, 3, 2};
D. char[] c = new char();
E. char[] c = new char[4]{'a', 'b', 'c', 'd'};

5   How can you initialize an array of two characters to 'a' and 'b'?

A. char[] charArray = new char[2]; charArray = {'a', 'b'};
B. char[2] charArray = {'a', 'b'};
C. char[] charArray = {'a', 'b'};
D. char[] charArray = new char[]{'a', 'b'};

6   What would be the result of attempting to compile and run the following code?
                
public class Test {
   public static void main(String[] args) {
     double[] x = new double[]{1, 2, 3};
     System.out.println("Value is " + x[1]);
   }
}

A. The program has a syntax error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}.
B. The program has a syntax error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3};
C. The program has a syntax error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0};
D. The program compiles and runs fine and the output "Value is 1.0" is printed.
E. The program compiles and runs fine and the output "Value is 2.0" is printed.

7   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     int[] x = new int[5];
     int i;
     for (i = 0; i < x.length; i++)
       x[i] = i;
     System.out.println(x[i]);
   }
}

A. The program displays 0 1 2 3 4.
B. The program displays 4.
C. The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBounds exception.
D. The program has syntax error because i is not defined in the last statement in the main method.

Section 5.4 Passing Arrays to Methods
8   When you pass an array to a method, the method receives __________.

A. a copy of the array
B. a copy of the first element
C. the reference of the array
D. none of the above

9   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     int[] x = {1, 2, 3, 4, 5};
     xMethod(x, 5);
   }

   public static void xMethod(int[] x, int length) {
     System.out.print(" " + x[length - 1]);
     xMethod(x, length - 1);
   }
}

A. The program displays 1 2 3 4 5.
B. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBounds exception.
C. The program displays 5 4 3 2 1.
D. The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBounds exception.

10   Analyze the following code:

public class Test1 {
   public static void main(String[] args) {
     xMethod(new double[]{3, 3});
     xMethod(new double[5]);
     xMethod(new double[3]{1, 2, 3});
   }

   public static void xMethod(double[] a) {
     System.out.println(a.length);
   }
}

A. The program has a syntax error because xMethod(new double[]{3, 3}) is incorrect.
B. The program has a syntax error because xMethod(new double[5]) is incorrect.
C. The program has a syntax error because xMethod(new double[3]{1, 2, 3}) is incorrect.
D. The program has a runtime error because a in null.
E. None of the above.

Section 5.4 Copying Arrays
11   In the following code, what is the printout for list2?

class Test {
   public static void main(String[] args) {
     int[] list1 = {1, 2, 3};
     int[] list2 = {1, 2, 3};
     list2 = list1;
     list1[0] = 0; list1[1] = 1; list2[2] = 2;

     for (int i = 0; i<list2.length; i++)
       System.out.print(list2[i] + " ");
   }
}

A. 1 2 3
B. 1 1 1
C. 0 1 2
D. 0 1 3
E. None of the above.

12   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     int[] x = {1, 2, 3, 4};
     int[] y = x;

     x = new int[2];

     for (int i = 0; i < y.length; i++)
       System.out.print(y[i] + " ");
   }
}

A. The program displays 1 2 3 4
B. The program displays 0 0
C. The program displays 0 0 3 4
D. The program displays 0 0 0 0

13   Analyze the following code.

int[] list = new int[5];
list = new int[6];

A. The code has syntax errors because the variable list cannot be changed once it is assigned.
B. The code has runtime errors because the variable list cannot be changed once it is assigned.
C. The code can compile and run fine. The second line assigns a new array to list.
D. None of the above.

14   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     int[] a = new int[4];
     a[1] = 1;
     a = new int[2];
     System.out.println("a[1] is " + a[1]);
   }
}

A. The program has a syntax error because new int[2] is assigned to a.
B. The program has a runtime error because a[1] is not initialized.
C. The program displays a[1] is 0.
D. The program displays a[1] is 1.

15   The __________ method copies the sourceArray to the targetArray.

A. System.copyArrays(sourceArray, 0, targetArray, 0, sourceArray.length);
B. System.copyarrays(sourceArray, 0, targetArray, 0, sourceArray.length);
C. System.arrayCopy(sourceArray, 0, targetArray, 0, sourceArray.length);
D. System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

Section 5.6 Multidimensional Arrays
16   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     boolean[][] x = new boolean[3][];
     x[0] = new boolean[1]; x[1] = new boolean[2];
     x[2] = new boolean[3];
 
     System.out.println("x[2][2] is " + x[2][2]);
   }
}
A. The program has a syntax error because new boolean[3][] is wrong.
B. The program has a runtime error because x[2][2] is null.
C. The program runs and displays x[2][2] is null.
D. The program runs and displays x[2][2] is true.
E. The program runs and displays x[2][2] is false.