1. What is the value that is printed out on the console?

String a = "abc";
a.concat("def");
System.out.println(a);

abcdef abc String is immutable so the statement a.concat("def") creates a new string but that string is not referenced in any variable. You would hold the refernece of the new String by declaring another variable and assigning the returned string to it. e.g.; String b = a.contact("def"); Note that 'a' variable still contains the original string 'abc' and it cannot be changed as String is immutable.

2. What is the value that is printed out on the console?

StringBuilder a = new StringBuilder("abc");
a.append("def");
System.out.println(a);

abcdef abc StringBuilder is mutable so you are able to change the original value of 'a' by appending the new string.

3. Select all the ways in which a new String is created

String a = "abc"; String a = new String("abc"); String a = "a"+"b"+"c";

4. Is this a valid array
String[] myArray = {"mango", 10, true};

No Yes An array contains elements of the same type. Since myArray is String data type, all its elements should be strings. This array has a boolean and integer as its elements. So it is invalid and does not compile.

results matching ""

    No results matching ""