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. -
static { alive = true; count = 0; } -
static { STEP = count; } -
static { count += STEP; } -
static ; -
static {;} -
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. -
The code will fail to compile, since the instance initializer block tries to assign a value to a static field. -
The code will fail to compile, since the field k will be uninitialized when it is used. -
The code will compile without error and will print 50, 70, 0, 20, 0 when run. -
The code will compile without error and will print 50, 70, 0, 20, 20 when run. -
The code will compile without error and will print 5, 70, 0, 20, 0 when run. -
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. -
instance { active = true; } -
MyClass { gap += 5; } -
{ gap = 5; length = (active ? 100 : 200) + gap; } -
{ ; } -
{ length = 4.2; } -
{ 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. -
The program will fail to compile. -
The program will compile without error and will print 1 , 2 , and 3 when run. -
The program will compile without error and will print 2 , 3 , and 1 when run. -
The program will compile without error and will print 3 , 1 , and 2 when run. -
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. -
The program will fail to compile. -
The program will compile without error and will print 1 , 2 , and 3 when run. -
The program will compile without error and will print 2 , 3 , and 1 when run. -
The program will compile without error and will print 3 , 1 , and 2 when run. -
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. -
Line A -
Line B -
Line C -
Line D -
Line E | |