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

Java Quiz 13: Passing Objects to a Method

DZone's Guide to

Java Quiz 13: Passing Objects to a Method

Welcome back to the Java Quiz series! In this post, we take a look at the answer to the last quiz, as well as introduce the next question.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

 Before we start with this week's quiz, here is the answer to Java Quiz 12: Unary Operators.

  1. The statement if(a == a++) returns true because the value of a is incremented by one after the evaluation.

  2. The statement a += 3; increments the value of a by 3.

  3. The statement System.out.print(a++ + ++a + a++); prints the sum of a++, ++a and a-- to the standard output. a++ = 4, ++a = 6, because the value of a is incremented by one a step before. The value of the last a-- remains 6. The statement writes 4 + 6 + 6 = 16 to the standard output.

    The correct answer is: D.

Here is the quiz for today!

What is the output of the following code?

public class MyClass
{
public static void myMethod(StringBuilder s1, StringBuilder s2)
{
s1.append("r");
s2.append("s");
s1 = s2;
}
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder("p");
StringBuilder sb2 = new StringBuilder("q");
myMethod(sb, sb2);
System.out.print(sb + ", " + sb2);
}
}

Select the correct answer

  • A. This program writes "qs, qs" to the standard output.

  • B. This program writes "pr, qs" to the standard output.

  • C. This program writes "pr, pr" to the standard output.

  • D. This program writes "r, s" to the standard output.

  • E. This program writes "p, q" 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!

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,java objects ,java methods

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}