Java OCA OCP Practice Question 2022

Question

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

public class Main {
 public static void main(String[] args) {
   String str = new String("test");
   str.concat(" mtest");
   StringBuilder strBuilder = new StringBuilder(" main");
   strBuilder.append(" mo");
   System.out.println(str + strBuilder);
 }
}

Select the one correct answer.

  • (a) The program will fail to compile.
  • (b) The program will print test mtest main mo, when run.
  • (c) The program will print mtest main mo, when run.
  • (d) The program will print test main mo, when run.
  • (e) The program will print test mtest main, when run.


(d)

Note

The program will construct an immutable String object containing "test" and a StringBuilder object containing " main".

The concat() method returns a reference value to a new immutable String object containing test mtest, but the reference value is not stored.

The append() method appends the string " mo" to the string buffer.




PreviousNext

Related