Java OCA OCP Practice Question 606

Question

What will be the result of attempting to compile and run the following code?

public class Main{ 
   public static void main (String args [] ){ 
      String str1 = "str1"; 
      String str2 = "str2"; 
      System .out.println ( str1.concat (str2) ); 
      System .out.println (str1); 
    } 
} 

Select 1 option

  • A. The code will fail to compile.
  • B. The program will print str1 and str1.
  • C. The program will print str1 and str1str2
  • D. The program will print str1str2 and str1
  • E. The program will print str1str2 and str1str2.


Correct Option is  : D

Note

String objects are immutable.

The original object will remain the same and a new object will be returned no matter what operation you do.

The statement str 1.concat(str2) creates a new String object which is printed but its reference is lost after the printing.




PreviousNext

Related