Review Questions

graphics/rq_icon.gif

8.6

Given the following class, which of these static initializer blocks can be inserted after the comment?

public class MyClass {
    private static int count = 5;
    final static int STEP = 10;
    boolean alive;
    // INSERT STATIC INITIALIZER BLOCK HERE
}

Select the three correct answers.

  1. static { alive = true; count = 0; }

  2. static { STEP = count; }

  3. static { count += STEP; }

  4. static ;

  5. static {;}

  6. static { count = 1; }

8.7

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

public class MyClass {
    public static void main(String[] args) {
        MyClass obj = new MyClass(l);
    }
    static int i = 5;
    static int l;
    int j = 7;
    int k;

    public MyClass(int m) {
        System.out.println(i + ", " + j + ", " + k + ", " + l + ", " + m);
    }
    { j = 70; l = 20; } // Instance Initializer Block
    static { i = 50; }  // Static Initializer Block
}

Select the one correct answer.

  1. The code will fail to compile, since the instance initializer block tries to assign a value to a static field.

  2. The code will fail to compile, since the field k will be uninitialized when it is used.

  3. The code will compile without error and will print 50, 70, 0, 20, 0 when run.

  4. The code will compile without error and will print 50, 70, 0, 20, 20 when run.

  5. The code will compile without error and will print 5, 70, 0, 20, 0 when run.

  6. The code will compile without error and will print 5, 7, 0, 20, 0 when run.

8.8

Given the following class, which instance initializer block inserted at the indicated location will allow the class to compile without errors?

public class MyClass {
    static int gap = 10;
    double length;
    final boolean active;

    // INSERT CODE HERE
}

Select the one correct answer.

  1. instance { active = true; }

  2. MyClass { gap += 5; }

  3. { gap = 5; length = (active ? 100 : 200) + gap; }

  4. { ; }

  5. { length = 4.2; }

  6. { active = (gap > 5); length = 5.5 + gap;}

8.9

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

public class Initialization {
    private static String msg(String msg) {
        System.out.println(msg); return msg;
    }

    public Initialization() { m = msg("1"); }

    { m = msg("2"); }

    String m = msg("3");

    public static void main(String[] args) {
        Object obj = new Initialization();
    }
}

Select the one correct answer.

  1. The program will fail to compile.

  2. The program will compile without error and will print 1, 2, and 3 when run.

  3. The program will compile without error and will print 2, 3, and 1 when run.

  4. The program will compile without error and will print 3, 1, and 2 when run.

  5. The program will compile without error and will print 1, 3, and 2 when run.

8.10

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

public class Initialization {
    private static String msg(String msg) {
        System.out.println(msg); return msg;
    }

    static String m = msg("1");

    { m = msg("2"); }

    static { m = msg("3"); }

    public static void main(String[] args) {
        Object obj = new Initialization();
    }
}

Select the one correct answer.

  1. The program will fail to compile.

  2. The program will compile without error and will print 1, 2, and 3 when run.

  3. The program will compile without error and will print 2, 3, and 1 when run.

  4. The program will compile without error and will print 3, 1, and 2 when run.

  5. The program will compile without error and will print 1, 3, and 2 when run.

8.11

Which of the labeled lines in the following code can be uncommented by removing the // characters and still allow the code to compile correctly?

class GeomInit {
//  int width = 14;             /* Line A */
    {
//      area = width * height;  /* Line B */
    }
    int width = 37;
    {
//      height = 11;            /* Line C */
    }
    int height, area;
//  area = width * height;      /* Line D */
    {
//      int width = 15;         /* Line E */
        area = 100;
    }
};

Select the two correct answers.

  1. Line A

  2. Line B

  3. Line C

  4. Line D

  5. Line E