Practice TestsICSEClass 10Computer Applications

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.

12 min read
String Methods in Java for ICSE Class 10: 25 Output Questions with 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() and lastIndexOf() 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() and equalsIgnoreCase() 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() and concat() return a new String. The original never changes.
  • startsWith() and endsWith() 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) + 1 gives 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.