5.5 | What will be the result of attempting to compile and run the following code? class MyClass { public static void main(String[] args) { boolean b = false; int i = 1; do { i++; b = ! b; } while (b); System.out.println(i); } } Select the one correct answer. -
The code will fail to compile, since b is an invalid conditional expression for the do-while statement. -
The code will fail to compile, since the assignment b = ! b is not allowed. -
The code will compile without error and will print 1 when run. -
The code will compile without error and will print 2 when run. -
The code will compile without error and will print 3 when run. |
5.6 | What will be the output when running the following program? public class MyClass { public static void main(String[] args) { int i=0; int j; for (j=0; j<10; ++j) { i++; } System.out.println(i + " " + j); } } Select the two correct answers. -
The first number printed will be 9 . -
The first number printed will be 10 . -
The first number printed will be 11 . -
The second number printed will be 9 . -
The second number printed will be 10 . -
The second number printed will be 11 . |
5.7 | Which one of these for statements is valid? Select the one correct answer. -
int j=10; for (int i=0, j+=90; i<j; i++) { j--; } -
for (int i=10; i=0; i--) {} -
for (int i=0, j=100; i<j; i++, --j) {;} -
int i, j; for (j=100; i<j; j--) { i += 2; } -
int i=100; for ((i>0); i--) {} |
5.8 | What will be the result of attempting to compile and run the following program? class MyClass { public static void main(String[] args) { int i = 0; for ( ; i<10; i++) ; // (1) for (i=0; ; i++) break; // (2) for (i=0; i<10; ) i++; // (3) for ( ; ; ) ; // (4) } } Select the one correct answer. -
The code will fail to compile, since the for statement (1) is missing the expression in the first section. -
The code will fail to compile, since the for statement (2) is missing the expression in the middle section. -
The code will fail to compile, since the for statement (3) is missing the expression in the last section. -
The code will fail to compile, since the for statement (4) is invalid. -
The code will compile without error, and the program will run and terminate without any output. -
The code will compile without error, but will never terminate when run. |
5.9 | Which statements are valid when occurring on their own?> Select the three correct answers. -
while () break; -
do { break; } while (true); -
if (true) { break; } -
switch (1) { default: break; } -
for (;true;) break; |
5.10 | Given the following code fragment, which of the following lines will be a part of the output? outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { if (i == j) { continue outer; } System.out.println("i=" + i + ", j=" + j); } } Select the two correct answers. -
i=1, j=0 -
i=0, j=1 -
i=1, j=2 -
i=2, j=1 -
i=2, j=2 -
i=3, j=3 -
i=3, j=2 |
5.11 | What will be the result of attempting to compile and run the following code? class MyClass { public static void main(String[] args) { for (int i = 0; i<10; i++) { switch(i) { case 0: System.out.println(i); } if (i) { System.out.println(i); } } } } Select the one correct answer. -
The code will fail to compile, owing to an illegal switch expression in the switch statement. -
The code will fail to compile, owing to an illegal conditional expression in the if statement. -
The code will compile without error and will print the numbers through 10 when run. -
The code will compile without error and will print the number when run. -
The code will compile without error and will print the number twice when run. -
The code will compile without error and will print the numbers 1 through 10 when run. |
5.12 | Which of the following implementations of a max() method will correctly return the largest value? // (1) int max(int x, int y) { return (if (x > y) { x; } else { y; }); } // (2) int max(int x, int y) { return (if (x > y) { return x; } else { return y; }); } // (3) int max(int x, int y) { switch (x < y) { case true: return y; default: return x; }; } // (4) int max(int x, int y) { if (x>y) return x; return y; } Select the one correct answer. -
Implementation labeled (1). -
Implementation labeled (2). -
Implementation labeled (3). -
Implementation labeled (4). |
5.13 | Given the following code, which statement is true? class MyClass { public static void main(String[] args) { int k=0; int l=0; for (int i=0; i <= 3; i++) { k++; if (i == 2) break; l++; } System.out.println(k + ", " + l); } } Select the one correct answer. -
The program will fail to compile. -
The program will print 3, 3 when run. -
The program will print 4, 3 when run if break is replaced by continue . -
The program will fail to compile if break is replaced by return . -
The program will fail to compile if break is simply removed. |
5.14 | Which statements are true? Select the two correct answers. -
{{}} is a valid statement block. -
{ continue; } is a valid statement block. -
block: { break block; } is a valid statement block. -
block: { continue block; } is a valid statement block. -
The break statement can only be used in a loop ( while , do-while or for ) or a switch statement. |