5.10. Sample exam questions

Q5-1.

What’s the output of the following code?

class Loop2 {
    public static void main(String[] args) {
        int i = 10;

        do
            while (i < 15)
                i = i + 20;
        while (i < 2);
        System.out.println(i);
    }
}
  1. 10
  2. 30
  3. 31
  4. 32

Q5-2.

What’s the output of the following code?

class Loop2 {
    public static void main(String[] args) {
        int i = 10;
        do
            while (i++ < 15)
                i = i + 20;
        while (i < 2);
        System.out.println(i);
    }
}
  1. 10
  2. 30
  3. 31
  4. 32

Q5-3.

Which of the following statements is true?

  1. The enhanced for loop can’t be used within a regular for loop.
  2. The enhanced for loop can’t be used within a while loop.
  3. The enhanced for loop can be used within a do-while loop.
  4. The enhanced for loop can’t be used within a switch construct.
  5. All of the above ...

Get OCA Java SE 8 Programmer I Certification Guide 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.