Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Quiz 11: Branching Statements

DZone's Guide to

Java Quiz 11: Branching Statements

Let's take a look at the answer to the previous quiz about demonstrating an anonymous class before tackling one involving branching statements.

· Java Zone ·
Free Resource

Microservices. Streaming data. Event Sourcing and CQRS. Concurrency, routing, self-healing, persistence, clustering...learn how Akka enables Java developers to do all this out of the box! Brought to you in partnership with Lightbend.

Before we start with this week's quiz, here is the answer to Java Quiz 10: Demonstrating An Anonymous Class

  1. The statement System.out.print(ac.i); writes the value of the instance variable of the class AnonymousClass. So, it writes 5 to the standard output.

  2. The statement height.print(); writes the value of i of the interface Height, which is 3.
       So, it writes 3 to the standard output.

    The correct answer is: A.

Here is the quiz for today!

What happens when the following program is compiled and run?

public class MyClass {
    static int x;

    void print() {
        int i = 0;
        while (i < 3) {
            x++;
            for (int j = 0; j < 4; j++) {
                x++;
                if (j == 2)
                    return;
            }
            i++;
        }
    }
    public static void main(String[] args) {
        new MyClass().print();
        System.out.print(x);
    }
}


  • A. This program writes "12" to the standard output.

  • B. This program writes "15" to the standard output.

  • C. This program writes "4" to the standard output.

  • D. This program writes "10" to the standard output.

  • E. Runtime error.

  • F. Compilation error.

The correct answer and its explanation will be included in the next quiz in two weeks! For more Java quizzes, puzzles, and assignments, take a look at my site!

Microservices. Streaming data. Event Sourcing and CQRS. Concurrency, routing, self-healing, persistence, clustering...learn how Akka enables Java developers to do all this out of the box! Brought to you in partnership with Lightbend. 

Topics:
java ,quiz ,branching statements ,anonymous class

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}