Review Questions

graphics/rq_icon.gif

4.1

Given the following declaration, which expression returns the size of the array, assuming the array has been initialized?

int[] array;

Select the one correct answer.

  1. array[].length()

  2. array.length()

  3. array[].length

  4. array.length

  5. array[].size()

  6. array.size()

4.2

Is it possible to create arrays of length zero?

Select the one correct answer.

  1. Yes, you can create arrays of any type with length zero.

  2. Yes, but only for primitive data types.

  3. Yes, but only for arrays of object references.

  4. No, you cannot create zero-length arrays, but the main() method may be passed a zero-length array of String references when no program arguments are specified.

  5. No, it is not possible to create arrays of length zero in Java.

4.3

Which one of the following array declaration statements is not legal?

Select the one correct answer.

  1. int []a[] = new int [4][4];

  2. int a[][] = new int [4][4];

  3. int a[][] = new int [][4];

  4. int []a[] = new int [4][];

  5. int [][]a = new int [4][4];

4.4

Which of these array declaration statements are not legal?

Select the two correct answers.

  1. int[] i[] = { { 1, 2 }, { 1 }, {}, { 1, 2, 3 } };

  2. int i[] = new int[2] {1, 2};

  3. int i[][] = new int[][] { {1, 2, 3}, {4, 5, 6} };

  4. int i[][] = { { 1, 2 }, new int[ 2 ] };

  5. int i[4] = { 1, 2, 3, 4 };

4.5

What would be the result of attempting to compile and run the following program?

// Filename: MyClass.java
class MyClass {
    public static void main(String[] args) {
        int size = 20;
        int[] arr = new int[ size ];

        for (int i = 0; i < size; ++i) {
                System.out.println(arr[i]);
        }
    }
}

Select the one correct answer.

  1. The code will fail to compile because the array type int[] is incorrect.

  2. The program will compile, but will throw an ArrayIndexOutOfBoundsException when run.

  3. The program will compile and run without error, but will produce no output.

  4. The program will compile and run without error and will print the numbers 0 through 19.

  5. The program will compile and run without error and will print 0 twenty times.

  6. The program will compile and run without error and will print null twenty times.

4.6

Given the following program, which statement is true?

class MyClass {
    public static void main(String[] args) {
        String[] numbers = { "one", "two", "three", "four" };

        if (args.length == 0) {
            System.out.println("no arguments");
        } else {
            System.out.println(numbers[ args.length ] + " arguments");
        }
    }
}

Select the one correct answer.

  1. The program will fail to compile.

  2. The program will throw a NullPointerException when run with zero program arguments.

  3. The program will print "no arguments" and "two arguments" when called with zero and three program arguments, respectively.

  4. The program will print "no arguments" and "three arguments" when called with zero and three program arguments, respectively.

  5. The program will print "no arguments" and "four arguments" when called with zero and three program arguments, respectively.

  6. The program will print "one arguments" and "four arguments" when called with zero and three program arguments, respectively.

4.7

What would be the result of trying to compile and run the following program?

public class DefaultValuesTest {
    int[] ia = new int[1];
    boolean b;
    int i;
    Object o;

    public static void main(String[] args) {
        DefaultValuesTest instance = new DefaultValuesTest();
        instance.print();
    }

    public void print() {
        System.out.println(ia[0] + " " + b + " " + i + " " + o);
    }
}

Select the one correct answer.

  1. The program will fail to compile because of uninitialized variables.

  2. The program will throw a java.lang.NullPointerException when run.

  3. The program will print "0 false NaN null".

  4. The program will print "0 false 0 null".

  5. The program will print "null 0 0 null".

  6. The program will print "null false 0 null".



     
    ASPTreeView.com
     
    Evaluation has ЅР·УМј¶ТВexpired.
    Info...