What does the following statement sequence print? String str = "Java Is Good"; int n = str.length(); String mystery = str.substring(n - 4, n) + str.charAt(4) + str.substring(0, 4); System.out.println(mystery);

Disable ads (and more) with a premium pass for a one time $4.99 payment

Prepare for the Arizona State University CSE110 Exam 1. Study with flashcards and multiple choice questions, each question has hints and explanations. Get ready for success!

To understand what the statement sequence prints, let's break down the components involved.

  1. The variable str is initialized with the value "Java Is Good". The length of this string is calculated using str.length(), which returns n = 12 as there are 12 characters in "Java Is Good".

  2. The expression str.substring(n - 4, n) retrieves a substring from str starting at index n - 4 (which is 12 - 4 = 8) up to index n (which is 12). This means it extracts the substring starting from index 8 to the end of the string. The substring from "Java Is Good" starting at index 8 is "Good".

  3. Next, we have str.charAt(4). The method charAt(4) returns the character at index 4 of the string. In "Java Is Good", index 4 corresponds to the character ' ' (a space).

  4. The final part str.substring(0, 4) retrieves the substring from the beginning of the string up to index 4. This is the substring "Java".

Now, we concatenate these three parts

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy