Java OCA OCP Practice Question 2417

Question

What is the output of the following code?

public class Main { 
    public static void main(String args[]) { 
        String ejg = "game".replace('a', 'Z').trim().concat("Aa"); 
        ejg.substring(0, 2); 
        System.out.println(ejg); 
    } 
} 
a  gZmeAZ
b  gZmeAa
c  gZm
d  gZ
e  game


b

Note

When chained, methods are evaluated from left to right.

The first method to execute is replace, not concat.

Strings are immutable.

Calling the method substring on the reference variable ejg doesn't change the contents of the variable ejg.

It returns a String object that isn't referred to by any other variable in the code.

In fact, none of the methods defined in the String class modify the object's own value.

They all create and return new String objects.




PreviousNext

Related