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.

5. What is the output of the below statements?
int a = 10, b = 20;
System.out.print(a+b);
System.out.print(""+a+b);

3030 301020 Error Any expression in which a String is added, the result will be a String. Hence a+b will be a String concatenation in the second statement but in the first statement, it is the normal integer addition as there is no String involved in the expression

results matching ""

    No results matching ""