Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

given the following declaration: string s = \apples and bananas are yum…

Question

given the following declaration: string s = \apples and bananas are yummy.\;
evaluate the expression: s.substring(1, 3)

  1. \app\
  2. \appl\
  3. \apple\
  4. \pp\
  5. \ppl\
  6. \pple\
  7. \p\
  8. \pl\
  9. \ple\
  10. none of these

your answer should be the number (1 - 10) next to the correct value.

Explanation:

Step1: Identify string indices

In Java, substring(startIndex, endIndex) uses 0-based indexing. For s = "Apples and bananas are yummy.", index 0 = 'A', index 1 = 'p', index 2 = 'p', index 3 = 'l'.

Step2: Extract substring

The method returns characters from startIndex (inclusive) to endIndex (exclusive). So s.substring(1, 3) takes characters at index 1 and 2: 'p' and 'p'.

Answer:

  1. "pp"