19. Tricky Rules


Answer:

art
applecart

Tricky Rules

The method

public String substring(int beginIndex )

creates a new string. The original string is not changed. There are two tricky rules:

  1. If beginIndex is exactly equal to the length of the original string, a substring is created, but it contains no characters (it is an empty string)
  2. If beginIndex is greater than the length of the original string, or a negative value, an IndexOutOfBoundsException is thrown (and for now, your program crashes).


Question 19:

What string is formed by the following expressions? (click on the button when you have decided)

    ExpressionNew StringComment
    String snake = "Rattlesnake";
    snake.substring(6)
    snake.substring(0)
    snake.substring(10)
    snake.substring(11)
    snake.substring(12)