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

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

 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!

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

Topics:
java ,java objects ,java methods

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}