Java String Methods: Tracing the Output (ICSE Class 10)
Updated 17 October 2026 · Fact-checked
A String is a sequence of characters indexed from 0, so the last character is at length() - 1. indexOf and lastIndexOf return the position of a character (or -1), charAt returns the character at an index, substring(b, e) takes characters from b up to e - 1, and compareTo returns the difference of the first unequal characters (or of the lengths). Strings are never changed by these methods: each returns a new value.
Write the index under every letter first: substring(2, 5) stops before index 5 (the dashed box), and the last index is length() minus 1.
Illustration drawn for QuestionBuddy.
Understand String Methods: Output Tracing
Indexing. In String s = "Computer"; the characters are C0 o1 m2 p3 u4 t5 e6 r7. s.length() is 8; the last index is 7. Always write the index under each character before tracing.
**Methods in the syllabus (with s = "Computer")**
int length(): 8char charAt(int n):charAt(3)is'p'int indexOf(char ch): first position,indexOf('t')is 5; absent gives-1(soindexOf('z')is -1)int lastIndexOf(char ch): last position,lastIndexOf('m')is 2String substring(int b): from b to the end,substring(3)is"puter"String substring(int b, int e): from b to e - 1,substring(2, 5)is"mpu"String toUpperCase(),toLowerCase():"COMPUTER"String trim(): removes spaces at both ends onlyString concat(String t):concat(" Lab")is"Computer Lab"String replace(char old, char new):replace('o', '0')is"C0mputer"boolean startsWith(String t),endsWith(String t):startsWith("Com")is trueString valueOf(x): converts any value to a String;String.valueOf(25) + 5is"255"
Comparing.
equals(t): true if the contents are the same, case sensitive;equalsIgnoreCase(t)ignores case.int compareTo(t): compares character by character using ASCII codes. At the first position where the characters differ it returns (character of s) minus (character of t). If one string is the start of the other it returns the difference in lengths. It returns 0 when equal; negative means s comes before t.compareToIgnoreCaseignores case.
Strings are not changed. s.toUpperCase(); alone leaves s the same; store the result: s = s.toUpperCase();.
Run-time error. charAt(8) or substring(3, 20) outside the length gives StringIndexOutOfBoundsException.
Only the String methods listed above are in the syllabus.
String Methods: Output Tracing: key facts
- Positions
First index 0 Last index length() - 1 indexOf not found -1 - Write indices under the characters.
- substring
substring(b) from b to the end substring(b, e) from b to e - 1 Length of result e - b - End index is excluded.
- compareTo result
First differing character s char minus t char One a prefix of the other length of s minus length of t Equal 0 - Uses ASCII codes: A is 65 and a is 97.
- Equality methods
equals case sensitive equalsIgnoreCase ignores case compareToIgnoreCase ignores case - equals returns boolean; compareTo returns int.
How to answer String Methods: Output Tracing questions
Use this routine for string method output questions.
- 1Write the string with an index under every character (spaces count).
- 2Apply each method exactly: length counts all characters; indexOf gives the first match; substring excludes the end index.
- 3For compareTo, find the first position where the characters differ and subtract their ASCII codes; if none, subtract lengths.
- 4Remember methods return new values; the original string stays unchanged unless it is reassigned.
- 5Join results with + using the printed separators.
String Methods: Output Tracing in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Extracting a surname
Suppose a school list stores names like "Riya Sen" and the program must pull out the surname.
Which two methods locate and extract it, and what do they give for "Riya Sen"?
Try it first, then show the solutionSolution
- indexOf(' ') gives the space position: R0 i1 y2 a3 space4, so 4.
- substring(4 + 1) = substring(5) gives "Sen".
Answer: indexOf(' ') = 4 and substring(5) = "Sen".
Memory hook: Find the space, cut after it.
Tricks to remember String Methods: Output Tracing
Short memory aids. Say each one aloud once, then use it on a question the same day.
Start in, end out
substring(b, e) includes b and excludes e.
Use it when: substring outputs.
Index starts at zero
First letter 0, last letter length - 1.
Use it when: charAt and loops.
Minus the first difference
compareTo = first unequal character of s minus that of t.
Use it when: compareTo values.
String Methods: Output Tracing: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Basic methods · 2 marks · Short answerBoard level
For String s = "Computer"; give the value of: s.length(), s.charAt(3), s.indexOf('t'), s.substring(3).
Try it first, then show the solutionSolution
- Indices: C0 o1 m2 p3 u4 t5 e6 r7, so length() = 8 and charAt(3) = 'p'. (1 mark)
- indexOf('t') = 5 and substring(3) = "puter". (1 mark)
Answer: 8, 'p', 5, "puter"
Example 2: trim and length · 3 marks · Short answerHarder board level
Write the output:
String t = " Java Rocks ";
System.out.println("[" + t.trim() + "]");
System.out.println(t.length());
System.out.println(t.trim().length());Try it first, then show the solutionSolution
- trim() removes the two spaces at each end: [Java Rocks]. (1 mark)
- The original has 2 + 10 + 2 = 14 characters (Java Rocks is 10 including the space), and trim() does not change t. (1 mark)
- The trimmed string has 10 characters. (1 mark)
Answer: [Java Rocks]
14
10
Example 3: compareTo values · 5 marks · Short answerHarder board level
Find the value of each expression:
(i) "apple".compareTo("apricot")
(ii) "Java".compareTo("java")
(iii) "abc".compareTo("abcde")
(iv) "same".compareTo("same")
(v) "Java".compareToIgnoreCase("java")
Try it first, then show the solutionSolution
- (i) First difference at index 2: 'p' (112) minus 'r' (114) = -2. (1 mark)
- (ii) 'J' (74) minus 'j' (106) = -32. (1 mark)
- (iii) The first string is a prefix of the second, so the result is the length difference 3 - 5 = -2. (1 mark)
- (iv) Equal strings: 0. (1 mark)
- (v) Ignoring case the strings are equal: 0. (1 mark)
Answer: (i) -2 (ii) -32 (iii) -2 (iv) 0 (v) 0
Example 4: Case study: working with Education · 4 marks · Case-basedHOTS / competency
A program uses String s = "Education";
(i) s.substring(2, 6) (1)
(ii) s.indexOf('c') and s.indexOf('e') (1)
(iii) s.replace('a', '@') and s.lastIndexOf('o') (1)
(iv) s.toLowerCase().indexOf('e') and explain the difference from (ii). (1)
Try it first, then show the solutionSolution
- Indices: E0 d1 u2 c3 a4 t5 i6 o7 n8.
- (i) Characters at 2, 3, 4, 5: "ucat".
- (ii) indexOf('c') = 3; there is no lower-case e in Education (the first letter is capital E), so indexOf('e') = -1.
- (iii) replace gives "Educ@tion" and lastIndexOf('o') = 7.
- (iv) After toLowerCase the string is "education", so indexOf('e') = 0. Searches are case sensitive, which explains -1 in (ii).
Answer: (i) ucat (ii) 3 and -1 (iii) Educ@tion and 7 (iv) 0, because the search is case sensitive
Example 5: concat, valueOf and equality · 3 marks · Short answerHarder board level
Write the output:
String a = "hi";
String b = a.concat("!");
System.out.println(b);
System.out.println(String.valueOf(25) + 5);
System.out.println(a.equals("HI") + " " + a.equalsIgnoreCase("HI"));Try it first, then show the solutionSolution
- concat gives a new string hi!. (1 mark)
- String.valueOf(25) is the text "25", and + 5 joins: 255. (1 mark)
- equals is case sensitive (false), equalsIgnoreCase ignores case (true): false true. (1 mark)
Answer: hi!
255
false true
Where students lose marks in String Methods: Output Tracing
The slips examiners see most often, and what to write instead.
Mistake: Including the end index in substring
substring(b, e) stops at e - 1.
Wrong working
"Computer".substring(2, 5) = "mput"
Right working
"Computer".substring(2, 5) = "mpu"
Do this: Count b up to e - 1.
Usually costs: 1 mark
Mistake: Treating strings as changed by a method call
Methods return new strings.
Wrong working
s.toUpperCase(); println(s) prints COMPUTER
Right working
println(s) still prints Computer; use s = s.toUpperCase();
Do this: Assign the result.
Usually costs: 1 mark
Mistake: Writing compareTo as just -1, 0 or 1
compareTo returns the actual difference of codes or lengths.
Wrong working
"apple".compareTo("apricot") = -1
Right working
"apple".compareTo("apricot") = -2
Do this: Subtract the first unequal codes.
Usually costs: 1 mark
Exam tips
- Always write indices under the characters, including spaces.
- For compareTo, show the two characters and their codes.
- Quote the return type of each method in definition questions.
- Remember that indexOf and lastIndexOf are case sensitive.
Practice questions on String Methods: Output Tracing
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
What is the value of "hello".charAt(1)?
(a) h (b) e (c) l (d) oHint
Index 0 is h.
Answer
(b) e
Q2 · 1 mark · Very short answerBoard level
What is the return type of the method indexOf(char ch)?
Hint
It returns a position or -1.
Answer
int
Q3 · 2 marks · Short answerHarder board level
Write the output:
String s = "banana"; System.out.println(s.indexOf('a') + " " + s.lastIndexOf('a') + " " + s.replace('a', 'o'));Hint
b0 a1 n2 a3 n4 a5.
Answer
1 5 bonono
Q4 · 3 marks · Short answerHarder board level
Given String s = "Hello World"; write one expression using indexOf and substring that gives the part after the space, and state its value.
Hint
The space is at index 5.
Answer
s.substring(s.indexOf(' ') + 1) gives "World"; indexOf(' ') is 5, so substring(6) is taken.
Q5 · 1 mark · Assertion-reasonHOTS / competency
Assertion (A): "Zebra".compareTo("apple") returns a negative value.
Reason (R): Upper-case letters have smaller ASCII codes than lower-case letters.
(a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.Hint
Compare Z (90) with a (97).
Answer
(a) Both are true: Z (90) minus a (97) = -7, and the smaller code of the capital letter is the reason.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.What does indexOf return if the character is absent?Show answer
-1
2.What is the last index of a string of length n?Show answer
n - 1
3.Which method ignores case when comparing for equality?Show answer
equalsIgnoreCase.
4.What does trim() remove?Show answer
Spaces at the beginning and end only.
5.What does substring(2, 5) take?Show answer
Characters at positions 2, 3 and 4.
6.What does compareTo return for equal strings?Show answer
0
String Methods: Output Tracing: frequently asked questions
Why is "A".compareTo("a") equal to -32?
The ASCII code of A is 65 and of a is 97; 65 minus 97 is -32.
Does replace change the original string?
No. It returns a new string with the replacement made.
What is the difference between equals and compareTo?
equals returns true or false for the same contents; compareTo returns an int that shows the order as well.