Java OCA OCP Practice Question 717

Question

What is the output of the following?

5:  String line = new String("-"); 
6:  String anotherLine = line.concat("-"); 
7:  System.out.print(line == anotherLine); 
8:  System.out.print(" "); 
9:  System.out.print(line.length()); 
  • A. false 1
  • B. false 2
  • C. true 1
  • D. true 2


A.

Note

A String is immutable so a different object is returned on line 6.

The object anotherLine points to is of length 2 after line 6 completes.

The original line reference still points to an object of length 1.

Option A is correct.




PreviousNext

Related