ProgrammingModerateProduct-company placement · Java

What is printed?

What is printed?
// static methods of the main class:
static void up(String s)         { s = s.concat("X"); }
static void add(StringBuilder b) { b = new StringBuilder("new"); b.append("!"); }
// in main:
String s = "abc";
StringBuilder b = new StringBuilder("old");
up(s); add(b);
System.out.print(s + " " + b);
  1. A. abcX old
  2. B. abc old
  3. C. abc new!
  4. D. abcX new!

Correct answer: B. abc old

Both methods only reassign their own parameter copies; the append goes to the new builder. Nothing reachable from the caller's variables is changed.

More OOP with C++ and Java questions

Exams that ask this topic