Java OCA OCP Practice Question 720

Question

What will be written to the standard output when the following program is run?

public class Main{ 
   public static void main (String args []){ 
      String blank  = " ";  // one space 
      String line = blank + "hello" + blank + blank; 
      line.concat ("world"); 
      String newLine  =  line.trim (); 
      System.out.println ((int)(line.length () + newLine.length ())); 
    } 
} 

Select 1 option

  • A. 25
  • B. 24
  • C. 23
  • D. 22
  • E. None of the above.


Correct Option is  : E

Note

Note that line.concat("world") does not change line itself.

It creates a new String object containing " hello world " but it is lost because there is no reference to it.

Calling trim() does not change the object itself.

So the answer is 8 + 5 = 13 !




PreviousNext

Related