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.

  1. The code will fail to compile, since b is an invalid conditional expression for the do-while statement.

  2. The code will fail to compile, since the assignment b = ! b is not allowed.

  3. The code will compile without error and will print 1 when run.

  4. The code will compile without error and will print 2 when run.

  5. The code will compile without error and will print 3 when run.

5.6 What will ...

Get Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.