LIWA

String Handling

ICSE Class 10 · Computer Applications · Chapter 8

What this chapter covers

Key points

Questions answered

Define the String class and state the package it belongs to.
The String class is the built-in Java class whose objects store sequences of characters and whose methods process them. It belongs to `java.lang`, which is imported automatically.
Why is a Java string said to be immutable?
Once a String object is created its characters cannot be changed. Methods such as `toUpperCase()` and `replace()` return a new string and leave the original as it was.
Give the output of `System.out.println("Ans: " + 2 + 3);` and `System.out.println(2 + 3 + " Ans");`.
`Ans: 23` and `5 Ans`. In the first, the string comes first, so each number is joined. In the second, the numbers are added before the string appears.
Differentiate between `length` and `length()`.
`length` is a property of an array that gives the number of elements and is written without brackets. `length()` is a String method that gives the number of characters and is written with brackets.
For `s = "HYDERABAD"`, find `s.indexOf('A')`, `s.lastIndexOf('A')` and `s.charAt(s.length() - 2)`.
The indices are H0 Y1 D2 E3 R4 A5 B6 A7 D8. Therefore `indexOf('A')` returns 5, `lastIndexOf('A')` returns 7, and `charAt(7)` returns `'A'`.
Why should `equals()` be used instead of `==` to compare two strings?
`==` checks whether two variables refer to the same object, while `equals()` checks whether the two strings contain the same characters. Two different objects can hold the same text, so `==` can give `false` for equal strings.
What value does `"CRICKET".compareTo("CRICKETER")` return? Give the reason.
-2. All seven characters of "CRICKET" match the start of "CRICKETER", so the difference of lengths, 7 - 9 = -2, is returned.
What value does `"apple".compareTo("Apple")` return?
32. The first characters differ: `'a'` is 97 and `'A'` is 65, and 97 - 65 = 32.

Past board exam questions

ICSE 2026 board paper · 15 marks

Write a program to accept a word and print the Symbolic of the accepted word. Symbolic word is formed by extracting the characters from the first consonant, then add characters before the first consonant of the accepted word and end with "TR". Example: AIRWAYS Symbolic word is RWAYSAITR BEAUTY Symbolic word is BEAUTYTR

Show answer
``` import java.util.Scanner; class Symbolic { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter a word: "); String w = sc.next().toUpperCase(); int pos = w.length(); for (int i = 0; i < w.length(); i++) { char ch = w.charAt(i); if (Character.isLetter(ch) && "AEIOU".indexOf(ch) == -1) { pos = i; break; } } String sym = w.substring(pos) + w.substring(0, pos) + "TR"; System.out.println("Symbolic word is " + sym); } } ```

ICSE 2026 board paper · 1 mark

To extract the word NOW from the word "ACKNOWLEDGEMENT", the Java statement "ACKNOWLEDGEMENT".substring(3, ____) is used. Choose the correct number to fill in the blank.

  1. 6
  2. 7
  3. 5
  4. 8
Show answer
6

ICSE 2026 board paper · 1 mark

Assertion (A): The substring() method modifies the original String. Reason (R): The substring() method can extract part of a String starting from a specific index.

  1. (A) is true and (R) is false.
  2. (A) is false and (R) is true.
  3. Both (A) and (R) are true and (R) is the correct explanation of (A).
  4. Both (A) and (R) are true, but (R) is not the correct explanation of (A).
Show answer
(A) is false and (R) is true.

ICSE 2026 board paper · 1 mark

``` String a[ ] = {"Atasi", "Aditi", "Anant", "Amit", "Ahana"}; System.out.println(a[1].charAt(1) + "*" + a[2].charAt(2)); ``` The output of the above statement is:

  1. da
  2. d * a
  3. ti
  4. t * i
Show answer
d * a

ICSE 2026 board paper · 1 mark

Which of the following String methods returns a negative value?

  1. length()
  2. equals()
  3. compareTo()
  4. charAt()
Show answer
compareTo()

ICSE 2026 board paper · 2 marks

Give the output of the following program segment: ``` String S = "GRACIOUS".substring(4); System.out.println(S); System.out.println ("GLAMOROUS".endsWith(S)); ```

Show answer
IOUS false ("GRACIOUS".substring(4) gives "IOUS"; "GLAMOROUS" ends with "ROUS", not "IOUS", so endsWith returns false.)

ICSE 2026 board paper · 2 marks

Name the following: (a) The return data type of the method equals(). (b) The String method which has no parameter and returns a String.

Show answer
(a) boolean (b) toUpperCase() (also toLowerCase() or trim())

ICSE 2025 board paper · 15 marks

Define a class to accept a String and Print if it is a Super string or not. A String is Super if the number of uppercase letters are equal to the number of lower case letters. [Use Character & String methods only] Example : “COmmITmeNt” Number of Uppercase letters – 5 Number of Lowercase letters – 5 String is a Super String

Show answer
import java.util.Scanner; class SuperString { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter a string: "); String s = sc.nextLine(); int upper = 0, lower = 0; // counts of uppercase and lowercase letters for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (Character.isUpperCase(ch)) upper++; else if (Character.isLowerCase(ch)) lower++; } System.out.println("Number of Uppercase letters - " + upper); System.out.println("Number of Lowercase letters - " + lower); if …