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

Java Quiz 12: Unary Operators

DZone's Guide to

Java Quiz 12: Unary Operators

Check out the answer to the last quiz about branching statements and test your Java knowledge when it comes to unary operators!

· Java Zone ·
Free Resource

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

Before we start with this week's quiz, here is the answer to Java Quiz 11: Branching Statements

  1. The body of the while loop while(i < 3) is executed for the first time when i = 0. The statement x++; increments the value of x by one. So, x = 0 + 1 = 1.

  2. The body of the for-loop for(int j = 0; j < 4; j++) is executed for the first time when j = 0. The statement x++; increments the value of x by one. So, x = 1 + 1 = 2.

  3. The body of the for-loop for(int j = 0; j < 4; j++) is executed for the second time when j = 1. The statement x++; increments the value of x by one. So, x = 2 + 1 = 3.

  4. The body of the for-loop for(int j = 0; j < 4; j++) is executed for the third time when j = 2. The statement x++; increments the value of x by one. So, x = 3 + 1 = 4.

  5. Now, the value of j is equal to 2. So the statement if(j == 2) returns true.

  6. The return; statement terminates the execution.

The correct answer is: C.

Here is the quiz for today!

What is the output of the following code?

public class MyClass
{
    static int a;

    public static void main(String[] args)
    {
        if(a == a++)
        {
            a += 3;
        }
        else
        {
            a += 5;
        }
        System.out.print(a++ + ++a + a--);
    }
}


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

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

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

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

  • E. This program writes "14" to the standard output.

  • F. Runtime error.

  • G. 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!

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

Topics:
java ,quiz ,unary operators

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}