OCA Java SE 8 Mock Exam 2 - OCA Mock Question 39








Question

What is the output of the following code?

public class Main {
  public static void main(String args[]) {
    String s = "java2s".replace('a', 'Z').trim().concat("Aa");
    s.substring(0, 2);
    System.out.println(s);
  }
}
  1. javZ2sAa
  2. jZvZ2sAa
  3. jZv
  4. jZvZ2s
  5. jZvZ




Answer



B

Note

When chained, methods are evaluated from left to right.

The first method to execute is replace, not concat.

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