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

Java Quiz 7: Using Unary Operators

DZone's Guide to

Java Quiz 7: Using Unary Operators

Catch up with the answer to the previous Java quiz and test your knowledge about unary operators with this week's test.

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

Before we start with this week's quiz, here is the answer to Java Java Quiz 6: Calling Constructors by Using the Keyword This.

  1. The statement MyClass mc = new MyClass(); calls the no-argument constructor.

  2. The statement this("z"); calls the one-argument constructor.

  3. The statement this("x", "y"); calls the two-argument constructor.

  4. The statement this.str += str; and this.str2 += str2; adds the string x to str and y to str2. So, the statement System.out.print(this.str + this.str2); inside the two argument constructor writes xy to the standard output.

  5. The statement this.str += str; adds z to the str. So, the value of str becomes xz.
    The statement System.out.print(this.str + str2); inside the one-argument constructor writes xzy to the standard output.

  6. The statement System.out.print(str + str2); inside the one-argument constructor writes xzy again to the standard output. So, the program writes xyxzyxzy to the standard output.

    The correct answer is: D.

Here is the quiz for today!

What happens when the following program is compiled and run?

public class MyClass {
    int x;
    int y;

    MyClass(int x, int y) {
        this.x = x++;
        this.y = ++y;
    }
    int method(MyClass mc) {
        mc.x += 9;
        mc.y += 2;
        return mc.x + mc.y;
    }
    public static void main(String[] args) {
        MyClass mc = new MyClass(3, 3);
        System.out.println(mc.method(mc));
    }
}


Select the correct answer.

  • A. This code writes "11" to the standard output.

  • B. This code writes "17" to the standard output.

  • C. This code writes "18" to the standard output.

  • D. This code writes "19" to the standard output.

  • E. This code writes "0" to the standard output.

  • F. This program writes nothing to the standard output.

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!

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.

Topics:
java ,java quiz ,unary operators ,constructors

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}