ProgrammingModerateProduct-company placement · Java

What is printed?

What is printed?
char[] c = {'h', 'i'};
System.out.print(c);
System.out.print(" " + String.valueOf(c) + " " + c.length);
  1. A. [C@… (a hash) hi 2
  2. B. hi [C@… (a hash) 2
  3. C. hi hi 2
  4. D. h i hi 2

Correct answer: C. hi hi 2

PrintStream has a print(char[]) overload that prints the characters, and String.valueOf(char[]) builds a String from them. Only concatenating the array itself ("" + c) would give the [C@… form.

More OOP with C++ and Java questions

Exams that ask this topic