Review Questions

graphics/rq_icon.gif

3.25

What will be printed when the following program is run?

public class ParameterPass {
    public static void main(String[] args) {
        int i = 0;
        addTwo(i++);
        System.out.println(i);
    }

    static void addTwo(int i) {
        i += 2;
    }
}

Select the one correct answer.

  1. 0

  2. 1

  3. 2

  4. 3

3.26

What will be the result of attempting to compile and run the following class?

public class Passing {
    public static void main(String[] args) {
        int a = 0; int b = 0;
        int[] bArr = new int[1]; bArr[0] = b;

        inc1(a); inc2(bArr);

        System.out.println("a=" + a + " b=" + b + " bArr[0]=" + bArr[0]);
    }

    public static void inc1(int x) { x++; }

    public static void inc2(int[] x) { x[0]++; }
}

Select the one correct answer.

  1. The code will fail to compile, since x[0]++; is not a legal statement.

  2. The code will compile and will print "a=1 b=1 bArr[0]=1" when run.

  3. The code will compile and will print "a=0 b=1 bArr[0]=1" when run.

  4. The code will compile and will print "a=0 b=0 bArr[0]=1" when run.

  5. The code will compile and will print "a=0 b=0 bArr[0]=0" when run.

3.27

Given the class

// Filename: Args.java
public class Args {
    public static void main(String[] args) {
        System.out.println(args[0] + " " + args[args.length-1]);
    }
}

what would be the result of executing the following on the command line?

java Args In politics stupidity is not a handicap

Select the one correct answer.

  1. The program will throw ArrayIndexOutOfBoundsException.

  2. The program will print "java handicap".

  3. The program will print "Args handicap".

  4. The program will print "In handicap".

  5. The program will print "Args a".

  6. The program will print "In a".

3.28

Which statements would cause a compilation error if inserted in the location indicated in the following program?

public class ParameterUse {
    static void main(String[] args) {
        int a = 0;
        final int b = 1;
        int[] c = { 2 };
        final int[] d = { 3 };
        useArgs(a, b, c, d);
    }

    static void useArgs(final int a, int b, final int[] c, int[] d) {
        // INSERT STATEMENT HERE.
    }
}

Select the two correct answers.

  1. a++;

  2. b++;

  3. b = a;

  4. c[0]++;

  5. d[0]++;

  6. c = d;



     
    ASPTreeView.com
     
    Evaluation has »ГТЛВОИСЩїіexpired.
    Info...