Java OCA OCP Practice Question 2008

Question

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

class MyClass {//from w  w  w  . j  a  v a  2  s.c  o  m
  public static void main(String[] args) {
    String str1 = "str1";
    String str2 = "str2";
    String str3 = "str3";

    str1.concat(str2);
    System.out.println(str3.concat(str1));
  }
 }

Select the one correct answer.

  • (a) The code will fail to compile because the expression str3.concat(str1) will not result in a valid argument for the println() method.
  • (b) The program will print str3str1str2, when run.
  • (c) The program will print str3, when run.
  • (d) The program will print str3str1, when run.
  • (e) The program will print str3str2, when run.


(d)

Note

The program will print str3str1 when run.

The concat() method will create and return a new String object, which is the concatenation of the current String object and the String object given as an argument.

The expression statement str1.

concat(str2) creates a new String object, but its reference value is not stored.




PreviousNext

Related