Review Questions |
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.
|
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. |
5.7 |
Which one of these for statements is valid?
Select the one correct answer.
|
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.
|
5.9 |
Which statements are valid when occurring on their own?>
Select the three correct answers.
|
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.
|
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.
|
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.
|
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.
|
5.14 |
Which statements are true?
Select the two correct answers.
|