String Methods in Java for ICSE Class 10: 25 Output Questions with Answers
ICSE Class 10 Computer Applications practice on String class methods: length, charAt, indexOf, lastIndexOf, substring, replace, trim, compareTo, equals and more, with 25 board-style output questions and step-by-step answers.

String output questions are guaranteed in Section A of the ICSE Computer Applications paper. They are quick marks, but only if you count indices precisely. This post summarises the String methods and then gives you 25 output questions with worked answers.
Indices start at 0. Write the index under each character on rough paper before answering. It prevents almost every mistake.
The methods in brief
length()returns an int: the number of characters.charAt(i)returns a char.indexOf()andlastIndexOf()return an int position, or −1 if the item is not found.substring(a, b)returns the characters from index a up to b − 1.equals()andequalsIgnoreCase()return a boolean.compareTo()returns an int: 0 if the strings are equal, otherwise the difference between the first characters that differ (or between the lengths).toUpperCase(),toLowerCase(),trim(),replace()andconcat()return a new String. The original never changes.startsWith()andendsWith()return a boolean.
Practice: 25 output questions
Try each one before reading its answer.
1. "COMPUTER".length()
Answer: 8
2. "COMPUTER".charAt(4)
Answer: 'U'. The indices are C0 O1 M2 P3 U4.
3. "COMPUTER".substring(2, 5)
Answer: "MPU", from indices 2, 3 and 4.
4. "COMPUTER".substring(5)
Answer: "TER"
5. "Java Programming".indexOf('a')
Answer: 1
6. "Java Programming".lastIndexOf('a')
Answer: 10. The 'a' in "Programming" is at index 10.
7. "Java Programming".indexOf("gram")
Answer: 8
8. "Mississippi".indexOf("ss", 3)
Answer: 5. The search starts at index 3, so the first "ss" (at 2) is skipped.
9. "Hello".indexOf('z')
Answer: −1
10. "Hello".replace('l', 'L')
Answer: "HeLLo"
11. " ICSE ".trim().length()
Answer: 4
12. "ICSE".concat("2027")
Answer: "ICSE2027"
13. "abc".compareTo("abd")
Answer: −1, which is 'c' − 'd'.
14. "Apple".compareTo("apple")
Answer: −32, which is 'A' (65) − 'a' (97).
15. "ab".compareTo("abc")
Answer: −1. The strings match up to the shorter length, so the result is the difference in length, 2 − 3.
16. "TRUE".equalsIgnoreCase("true")
Answer: true
17. "Java".equals("java")
Answer: false. equals() is case-sensitive.
18. "ISC2027".endsWith("27")
Answer: true
19. "program".charAt(3)
Answer: 'g'
20. "A" + 1 + 2
Answer: "A12". Once a String is involved, + joins text from left to right.
21. 1 + 2 + "A"
Answer: "3A". The two numbers are added first, then joined with "A".
22. 'A' + 1 + "B"
Answer: "66B". 'A' + 1 is the int 66, which is then joined with "B".
23. String s = "Java"; s.length() + s.indexOf('v')
Answer: 4 + 2 = 6
24. "SCHOOL".toLowerCase().charAt(0) == 's'
Answer: true
25. String s = "madam"; s.toUpperCase(); System.out.println(s);
Answer: "madam". Strings are immutable, and the uppercase result was never stored.
Common traps
substring(a, b)excludes index b.charAt()returns a char, so"AB".charAt(0) + 1gives the int 66, not "B" or "A1".compareTo()gives the difference of the first mismatch only, not of the whole string.- Forgetting immutability, as in question 25, is the most frequent error.
Put these methods into practice with our 10 most important String programs for ICSE Class 10.
Related Posts
Continue learning with these related articles
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.