Tricky Rules

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

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 greather than the length of the original string, or a negative value, a IndexOutOfBoundsException is thrown (and for now, your program crashes).

QUESTION 19:

Decide on what string is formed by the following expressions:

Expression New String Comment
String snake = "Rattlesnake";
snake.substring(6)
snake.substring(0)
snake.substring(10)
snake.substring(11)
snake.substring(12)