Arrays and Strings in Java: ISC Class 11 Notes
Updated 15 October 2026 · Fact-checked
An array stores many values of one type under one name, accessed by index, and a String is a sequence of characters with library methods. ISC Class 11 Computer Science covers one- and two-dimensional arrays, searching, maximum and minimum, bubble, selection and insertion sort, solving linear equations, and String operations such as substring, concatenation, replacement and indexOf.
What Arrays and Strings is about
Real programs handle collections of data: the marks of 40 students, a matrix of coefficients, a sentence of words. Arrays and strings are Java's basic structured data types for these.
The CISCE 2028 syllabus (Section B, topic 10) asks for: structured data types: arrays (single and multi-dimensional) and strings; example algorithms: searching, finding maximum/minimum, sorting (bubble, selection, insertion), solving systems of linear equations, and string tasks: substring, concatenation, length, access to char in a string; and the Java library String class with basic operations (accessing characters, substring operations, concatenation, replacement, index-of operations).
Five programs on this page were compiled and run: searching, the three sorts with a pass-by-pass trace, a Gaussian-elimination solver with a matrix transpose, String operations, and a merit list.
Weightage: Arrays and Strings is topic 10 of the ISC Class XI Computer Science syllabus, in Section B - Java. CISCE weights Section B at 30 of the 70 marks in Paper I (Theory). Class XI is examined by the school, so your school sets the paper within this weightage.Source: CISCE ISC Computer Science syllabus, examination year 2028, Class XI unit weightage
Key concepts
- One-dimensional arrays
An array is a fixed-size collection of elements of the same type, stored in consecutive memory and accessed by an index (subscript) starting at 0.
Declaration and creation:
- int[] marks = new int[40]; (elements get default values: 0 for numbers, false for boolean, '\u0000' for char, null for objects)
- int[] marks = {67, 82, 45, 91}; (initialiser list; size 4)
Access: marks[0] is the first element; marks[marks.length − 1] is the last. length is a field (no brackets) giving the number of elements.
Index out of range: using an index < 0 or ≥ length throws ArrayIndexOutOfBoundsException at run time.
Arrays are objects: an array variable holds a reference; passing an array to a method lets the method change its elements.
Traversal: for (int i = 0; i < a.length; i++) ... visits every element: used for sums, averages, counting, maximum and minimum.
Worked example
After int[] a = new int[5];, what are a[2] and a.length? What happens on a[5] = 9;? [2 marks]
Try it first, then show the solutionSolution
- Elements default to 0; length is 5.
- Valid indices are 0 to 4, so a[5] throws ArrayIndexOutOfBoundsException at run time.
Answer: a[2] = 0; a.length = 5; a[5] = 9 throws ArrayIndexOutOfBoundsException.
- Maximum, minimum and searching
Maximum and minimum: start with the first element as the current max (and min); compare every other element and update; record the position if needed.
Linear (sequential) search: compare the key with each element from the start; return the index of the first match, or −1. Works on unsorted data; up to n comparisons.
Binary search (array must be sorted): compare the key with the middle element; if smaller, search the left half; if larger, the right half; repeat until found or the range is empty. Each step halves the range, so at most about log₂ n + 1 comparisons (4 for 9 elements, 10 for 1,000).
// One-dimensional arrays: maximum and minimum, linear search and binary search. public class Searching { static int linearSearch(int[] a, int key) { for (int i = 0; i < a.length; i++) if (a[i] == key) return i; // index of the first match return -1; // not found } static int binarySearch(int[] a, int key) // a must be sorted in ascending order { int lo = 0, hi = a.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; System.out.print("[mid " + mid + " = " + a[mid] + "] "); if (a[mid] == key) return mid; else if (a[mid] < key) lo = mid + 1; // search the right half else hi = mid - 1; // search the left half } return -1; } public static void main(String[] args) { int[] marks = {67, 82, 45, 91, 58, 73}; int max = marks[0], min = marks[0], pos = 0; for (int i = 1; i < marks.length; i++) { if (marks[i] > max) { max = marks[i]; pos = i; } if (marks[i] < min) min = marks[i]; } System.out.println("max " + max + " at index " + pos + ", min " + min); System.out.println("linear search 58 -> " + linearSearch(marks, 58) + ", 60 -> " + linearSearch(marks, 60)); int[] sorted = {12, 19, 25, 33, 47, 58, 64, 71, 86}; int r = binarySearch(sorted, 64); System.out.println("-> found at " + r); r = binarySearch(sorted, 20); System.out.println("-> " + r); } }Output:
max 91 at index 3, min 45 linear search 58 -> 4, 60 -> -1 [mid 4 = 47] [mid 6 = 64] -> found at 6 [mid 4 = 47] [mid 1 = 19] [mid 2 = 25] -> -1The trace shows binary search finding 64 in two comparisons and giving up on 20 after three.
Basis Linear search Binary search Data Sorted or unsorted Must be sorted Method Element by element Halve the range each step Comparisons (worst) n about log₂ n + 1 Suits Small or unsorted lists Large sorted lists Worked example
How many comparisons does binary search make to find 34 in {3, 8, 15, 21, 29, 34, 40, 52}? [1 mark]
Try it first, then show the solutionSolution
- lo = 0, hi = 7: mid 3 → 21 < 34 → lo = 4; mid 5 → 34 found.
Answer: 2 comparisons.
- Sorting: bubble, selection and insertion
Bubble sort: repeatedly compare adjacent elements and swap them if out of order; after pass k, the k largest elements are in their final places at the end. n − 1 passes; pass p makes n − p comparisons.
Selection sort: in pass i, find the smallest element in the unsorted part and swap it into position i. Exactly one swap per pass.
Insertion sort: take each element in turn and insert it into its correct place among the already-sorted elements on its left, shifting larger elements one place right. Efficient for nearly sorted data.
All three are shown below on {29, 10, 14, 37, 13}, printing the array after every pass.
// Bubble, selection and insertion sort (ascending), printing the array after each pass. public class Sorting { static String show(int[] a) { String s = ""; for (int i = 0; i < a.length; i++) s += a[i] + (i < a.length - 1 ? " " : ""); return s; } static void bubble(int[] a) { for (int pass = 1; pass < a.length; pass++) { for (int j = 0; j < a.length - pass; j++) if (a[j] > a[j + 1]) // swap neighbours that are out of order { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } System.out.println("bubble pass " + pass + ": " + show(a)); } } static void selection(int[] a) { for (int i = 0; i < a.length - 1; i++) { int minPos = i; for (int j = i + 1; j < a.length; j++) if (a[j] < a[minPos]) minPos = j; // remember the smallest so far int t = a[i]; a[i] = a[minPos]; a[minPos] = t; System.out.println("selection pass " + (i + 1) + ": " + show(a)); } } static void insertion(int[] a) { for (int i = 1; i < a.length; i++) { int key = a[i]; int j = i - 1; while (j >= 0 && a[j] > key) // shift larger elements right { a[j + 1] = a[j]; j--; } a[j + 1] = key; // insert key in its place System.out.println("insertion pass " + i + ": " + show(a)); } } public static void main(String[] args) { bubble(new int[]{29, 10, 14, 37, 13}); selection(new int[]{29, 10, 14, 37, 13}); insertion(new int[]{29, 10, 14, 37, 13}); } }Output:
bubble pass 1: 10 14 29 13 37 bubble pass 2: 10 14 13 29 37 bubble pass 3: 10 13 14 29 37 bubble pass 4: 10 13 14 29 37 selection pass 1: 10 29 14 37 13 selection pass 2: 10 13 14 37 29 selection pass 3: 10 13 14 37 29 selection pass 4: 10 13 14 29 37 insertion pass 1: 10 29 14 37 13 insertion pass 2: 10 14 29 37 13 insertion pass 3: 10 14 29 37 13 insertion pass 4: 10 13 14 29 37Note that bubble sort's fourth pass changed nothing (the array was already sorted after pass 3): a 'swapped' flag could stop it early.
Worked example
How many comparisons does bubble sort make on 4 elements, and what is the array {5, 3, 8, 1} after the first pass? [2 marks]
Try it first, then show the solutionSolution
- Passes make 3 + 2 + 1 = 6 comparisons.
- Pass 1: (5,3) swap → 3 5 8 1; (5,8) no; (8,1) swap → 3 5 1 8.
Answer: 6 comparisons; after pass 1: 3 5 1 8.
- Two-dimensional arrays and systems of linear equations
A two-dimensional array is an array of arrays: a table of rows and columns.
- int[][] a = new int[3][4]; (3 rows, 4 columns); int[][] b = {{1, 2, 3}, {4, 5, 6}};
- a[i][j] is the element in row i, column j. a.length = number of rows; a[0].length = number of columns.
- Traverse with nested loops: outer over rows, inner over columns.
- Uses: matrices, tables of marks (students × subjects), seating plans.
Typical matrix tasks: row and column sums, diagonal sum (a[i][i]), transpose (t[j][i] = a[i][j]), matrix addition.
Solving a system of linear equations: store the coefficients and constants in an augmented matrix (n rows, n + 1 columns), then:
1. Forward elimination: for each pivot row p, subtract multiples of row p from the rows below to make the entries below the pivot zero.
2. Back substitution: solve the last equation for the last unknown, then work upwards.The program solves 2x + y − z = 8, −3x − y + 2z = −11, −2x + y + 2z = −3, and transposes a 2 × 3 matrix.
// Two-dimensional arrays: solving a system of linear equations by Gaussian elimination // on the augmented matrix, then back substitution. public class Equations { static double[] solve(double[][] m) { int n = m.length; for (int p = 0; p < n; p++) // eliminate below each pivot { for (int r = p + 1; r < n; r++) { double factor = m[r][p] / m[p][p]; for (int c = p; c <= n; c++) m[r][c] -= factor * m[p][c]; } } double[] x = new double[n]; for (int r = n - 1; r >= 0; r--) // back substitution { double s = m[r][n]; for (int c = r + 1; c < n; c++) s -= m[r][c] * x[c]; x[r] = s / m[r][r]; } return x; } public static void main(String[] args) { // 2x + y - z = 8; -3x - y + 2z = -11; -2x + y + 2z = -3 double[][] m = { { 2, 1, -1, 8}, {-3, -1, 2, -11}, {-2, 1, 2, -3} }; double[] x = solve(m); System.out.println("x = " + Math.round(x[0] * 1000) / 1000.0 + ", y = " + Math.round(x[1] * 1000) / 1000.0 + ", z = " + Math.round(x[2] * 1000) / 1000.0); int[][] a = {{1, 2, 3}, {4, 5, 6}}; // 2 rows, 3 columns int[][] t = new int[3][2]; for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++) t[j][i] = a[i][j]; // transpose for (int i = 0; i < 3; i++) System.out.println(t[i][0] + " " + t[i][1]); System.out.println("rows = " + a.length + ", columns = " + a[0].length); } }Output:
x = 2.0, y = 3.0, z = -1.0 1 4 2 5 3 6 rows = 2, columns = 3Worked example
Find the diagonal sum of {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} and write the loop. [2 marks]
Try it first, then show the solutionSolution
- for (int i = 0; i < 3; i++) d += m[i][i];
- 1 + 5 + 9 = 15.
Answer: 15.
- The String class and its methods
A String is an object representing a sequence of characters. Strings are immutable: methods such as replace or toUpperCase return a new String; the original is unchanged.
Method Returns Example with s = "Computer Science" length() number of characters 16 charAt(i) character at index i charAt(3) = 'p' substring(b) from index b to the end substring(9) = "Science" substring(b, e) from b up to e − 1 substring(0, 8) = "Computer" indexOf(ch or str) first index, or −1 indexOf('e') = 6; indexOf("Sci") = 9 lastIndexOf(ch) last index 15 concat(t) or + joined string "Computer Science XI" replace(old, new) new string with replacements replace('e', '*') toUpperCase(), toLowerCase() changed case "COMPUTER SCIENCE" equals(t), equalsIgnoreCase(t) boolean content comparison compareTo(t) 0 if equal; negative if this comes first; positive if after "apple".compareTo("banana") = −1 trim() without leading and trailing spaces Compare strings with equals, not == (== compares references).
// The String class: length, charAt, substring, concatenation, replace, indexOf, and a few algorithms. public class StringOps { static boolean isPalindrome(String s) { s = s.toLowerCase(); int i = 0, j = s.length() - 1; while (i < j) { if (s.charAt(i) != s.charAt(j)) return false; i++; j--; } return true; } static int countWords(String s) { s = s.trim(); if (s.length() == 0) return 0; int words = 1; for (int i = 1; i < s.length(); i++) if (s.charAt(i) == ' ' && s.charAt(i - 1) != ' ') words++; return words; } public static void main(String[] args) { String s = "Computer Science"; System.out.println("length = " + s.length()); System.out.println("charAt(3) = " + s.charAt(3)); System.out.println("substring(9) = " + s.substring(9) + ", substring(0, 8) = " + s.substring(0, 8)); System.out.println("indexOf('e') = " + s.indexOf('e') + ", lastIndexOf('e') = " + s.lastIndexOf('e') + ", indexOf(\"Sci\") = " + s.indexOf("Sci") + ", indexOf('z') = " + s.indexOf('z')); System.out.println("replace = " + s.replace('e', '*')); String t = s.concat(" XI"); System.out.println("concat = " + t + ", original still = " + s); System.out.println("toUpperCase = " + s.toUpperCase()); System.out.println("equals: " + "java".equals("Java") + ", equalsIgnoreCase: " + "java".equalsIgnoreCase("Java")); System.out.println("compareTo: " + "apple".compareTo("banana")); String rev = ""; for (int i = s.length() - 1; i >= 0; i--) rev += s.charAt(i); System.out.println("reversed = " + rev); System.out.println("Madam palindrome? " + isPalindrome("Madam") + ", Mango? " + isPalindrome("Mango")); System.out.println("words = " + countWords(" ISC exams are in February ")); } }Output:
length = 16 charAt(3) = p substring(9) = Science, substring(0, 8) = Computer indexOf('e') = 6, lastIndexOf('e') = 15, indexOf("Sci") = 9, indexOf('z') = -1 replace = Comput*r Sci*nc* concat = Computer Science XI, original still = Computer Science toUpperCase = COMPUTER SCIENCE equals: false, equalsIgnoreCase: true compareTo: -1 reversed = ecneicS retupmoC Madam palindrome? true, Mango? false words = 5Worked example
For s = "Programming", find s.substring(3, 7), s.indexOf('m'), s.charAt(s.length() − 1) and s.replace("mm", "M"). [2 marks]
Try it first, then show the solutionSolution
- Indices: P0 r1 o2 g3 r4 a5 m6 m7 i8 n9 g10. substring(3, 7) = indices 3-6 = "gram".
- indexOf('m') = 6; last char = 'g'; replace gives "PrograMing".
Answer: "gram", 6, 'g', "PrograMing".
- String algorithms
Reverse a string: loop from the last index down to 0, appending each character.
Palindrome test: compare characters from both ends moving inwards (ignoring case if required); stop at the first mismatch.
Count words: after trim(), count positions where a space is followed by a non-space (or a non-space follows a space), so repeated spaces are handled.
Count a character: loop with charAt and compare.
Initials: 'rahul kumar sharma' → 'R. K. Sharma': split into words, take the first letter of each except the last, and capitalise the last word.
Access to characters by code: (char) (ch − 32) converts a lower-case letter to upper case; Character.isLetter, isDigit, isUpperCase help classify characters.
The StringOps program above reverses "Computer Science", tests 'Madam' and 'Mango', and counts 5 words in ' ISC exams are in February ' despite the extra spaces.
Worked example
Write a loop that counts the letter 'a' in "banana", and give the count. [1 mark]
Try it first, then show the solutionSolution
- for (int i = 0; i < w.length(); i++) if (w.charAt(i) == 'a') c++;
Answer: 3.
Arrays and Strings in real life
Each concept, met in an everyday situation. Read the situation, answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Binary search in daily life
Suppose you look for the word 'nectar' in a 1,000-page dictionary by opening the middle, then the middle of the correct half, and so on.
About how many openings do you need at most?
Try it first, then show the solutionSolution
- Each opening halves the pages: 1,000 → 500 → 250 → ... → 1.
- 2¹⁰ = 1,024 ≥ 1,000, so about 10 openings.
Answer: About 10, because binary search halves the range each time (log₂ 1,000 ≈ 10).
Memory hook: Halve and halve again.
Insertion sort with cards
Imagine picking up playing cards one by one and sliding each into its right place among the cards already in your hand.
Which sorting method is this?
Try it first, then show the solutionSolution
- The cards in hand stay sorted; each new card is inserted, shifting bigger cards right.
- That is insertion sort.
Answer: Insertion sort.
Memory hook: Pick a card, slide it in.
2-D arrays as tables
Suppose a class teacher records marks of 40 students in 5 subjects.
How should the data be stored, and how do you get a student's total and a subject's average?
Try it first, then show the solutionSolution
- int[][] marks = new int[40][5]; row = student, column = subject.
- Student i's total: sum of marks[i][j] over j; subject j's average: sum of marks[i][j] over i, divided by 40.0.
Answer: A 40 × 5 array; row sums give student totals and column sums give subject averages.
Memory hook: Rows are students, columns are subjects.
Topics in this chapter
Arrays and Strings: syntax and key facts
- Array indices
- Valid indices 0 to length − 1; last element a[a.length − 1]
- Bubble sort comparisons
- (n − 1) + (n − 2) + ... + 1 = n(n − 1) ÷ 2
- Binary search
- mid = (lo + hi) / 2; worst-case comparisons ≈ ⌊log₂ n⌋ + 1
- 2-D array
- rows = a.length; columns = a[0].length; element a[i][j]
- substring
- s.substring(b, e) has length e − b (indices b to e − 1)
- Transpose
- t[j][i] = a[i][j] (an r × c matrix becomes c × r)
Arrays and Strings: diagrams to practise
Practise each one on paper: draw it from the checklist, then tick off every label.
Array in memory
Explain an array and its indexing.
Draw
- A row of boxes with values 67, 82, 45, 91, 58, 73; indices 0 to 5 written below.
- Variable marks with an arrow to the first box (reference).
- Label marks.length = 6 and 'a[6] → ArrayIndexOutOfBoundsException'.
Label
- index
- element
- reference
- length
Watch out: Indices below the boxes, values inside: make sure the first index is 0 and the last is length − 1.
Sorting pass table
Show the passes of bubble, selection or insertion sort.
Draw
- Rows: initial array, pass 1, pass 2, ...; columns: array positions.
- Underline the elements compared or swapped in each pass; shade the part already in final position.
Label
- pass
- swap
- sorted part
Watch out: Copy the pass outputs from your trace; the shaded sorted region shows how each method works.
How the school exam asks Arrays and Strings
The question patterns to expect, and the method that works for each.
| Question type | How it is asked | Method |
|---|---|---|
| Output and tracing (1-5 marks) | String method results; array contents after a pass of a sort; binary search steps. | Write indices above the characters; show the array after each pass. |
| Short programs (2-5 marks) | Max/min with position; linear search; count vowels or words; reverse or palindrome; diagonal sums. | Standard loops; handle 'not found' and edge cases. |
| Programs (10 marks) | Sort parallel arrays; matrix operations; solve equations; string processing (initials, frequencies). | Algorithm, then code with clear variable names; trace with sample data. |
Tricks to remember Arrays and Strings
Short memory aids. Say each one aloud once, then use it on a question the same day.
Bubble = neighbours swap; selection = pick smallest; insertion = slide in
B swaps pairs, S selects the minimum, I inserts into a sorted part.
Use it when: Three sorts.
Binary needs sorted
No order, no halving.
Use it when: Binary search condition.
substring(b, e): e is excluded
Length = e − b.
Use it when: String indices.
length vs length()
Arrays: a.length (field); Strings: s.length() (method).
Use it when: Avoiding syntax errors.
Arrays and Strings: exam-level solved questions
Ordered from exam level to HOTS. Cover the solution, write your own, then compare step by step.
Example 1: Binary search requirement · 1 mark · MCQExam level
Binary search can be applied only when the array is:
(a) of size less than 100
(b) sorted
(c) two-dimensional
(d) of type String
Try it first, then show the solutionSolution
- Halving the range relies on order.
Answer: (b) sorted.
Example 2: Immutability · 1 mark · Assertion-reasonHarder exam level
Assertion: After String t = s.concat(" XI");, the string s is unchanged.
Reason: String objects in Java are immutable.
(a) Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
(b) Both Assertion and Reason are true but Reason is not the correct explanation of Assertion.
(c) Assertion is true but Reason is false.
(d) Both Assertion and Reason are false.
Try it first, then show the solutionSolution
- concat returns a new String; s cannot be changed (the program printed 'original still = Computer Science').
Answer: (a)
Example 3: String method results · 2 marks · Short answerExam level
Give the results of "Hello".compareTo("Help") and "abc".compareTo("abcd"), with reasons.
Try it first, then show the solutionSolution
- First difference at index 3: 'l' (108) − 'p' (112) = −4.
- "abc" is a prefix of "abcd": the result is the difference in lengths, 3 − 4 = −1.
Answer: −4 and −1 (as the compiled check printed).
Example 4: Selection vs insertion trace · 5 marks · Short answerHarder exam level
For {29, 10, 14, 37, 13}, show the array after each pass of selection sort and of insertion sort.
Try it first, then show the solutionSolution
- Selection pass 1: smallest 10 swapped to position 0 → 10 29 14 37 13.
- Pass 2: smallest of the rest, 13, swapped to position 1 → 10 13 14 37 29; pass 3: 14 already in place → unchanged; pass 4: 29 and 37 swapped → 10 13 14 29 37.
- Insertion pass 1: insert 10 before 29 → 10 29 14 37 13; pass 2: insert 14 → 10 14 29 37 13.
- Pass 3: 37 stays → 10 14 29 37 13; pass 4: insert 13 after 10 → 10 13 14 29 37.
- These match the compiled Sorting program's output.
Answer: Selection: 10 29 14 37 13 / 10 13 14 37 29 / 10 13 14 37 29 / 10 13 14 29 37. Insertion: 10 29 14 37 13 / 10 14 29 37 13 / 10 14 29 37 13 / 10 13 14 29 37.
Example 5: Linear equations by elimination · 5 marks · Short answerHOTS / competency
Using the augmented-matrix method, solve x + y = 5 and 2x − y = 1, showing the matrix after elimination.
Try it first, then show the solutionSolution
- Augmented matrix: [1 1 | 5], [2 −1 | 1].
- Eliminate below the pivot: R2 = R2 − 2R1 → [0 −3 | −9].
- Back substitution: y = −9 ÷ −3 = 3.
- x = (5 − 1 × 3) ÷ 1 = 2.
- Check: 2 + 3 = 5; 4 − 3 = 1. The Equations.solve method gives 2.0 and 3.0.
Answer: x = 2, y = 3.
Example 6: Merit list program · 10 marks · Long answerHOTS / competency
Names and marks of six students are stored in parallel arrays: Anu 78, Dev 91, Isha 85, Kabir 66, Mehr 91, Ravi 59. Write a program that sorts them in descending order of marks by selection sort (keeping names matched), prints the merit list, the average and the number of students above average.
Try it first, then show the solutionSolution
- Selection sort on marks in descending order: in pass i find the index of the largest remaining mark.
- Swap marks[i] with marks[best] and names[i] with names[best], so each name stays with its mark.
- Sum the marks and compute avg = (double) total ÷ n (avoid integer division).
- Loop again to print rank, name and mark, and count marks greater than avg.
- Output (compiled program): 1. Dev 91, 2. Mehr 91, 3. Isha 85, 4. Anu 78, 5. Kabir 66, 6. Ravi 59; average = 78.33, above average = 3.
Answer: See the compiled program:
Output:// Parallel arrays of names and marks, sorted in descending order of marks by selection sort,
// with the class average and the number above average.
public class MeritList
{
public static void main(String[] args)
{
String[] names = {"Anu", "Dev", "Isha", "Kabir", "Mehr", "Ravi"};
int[] marks = {78, 91, 85, 66, 91, 59};
int n = marks.length;
for (int i = 0; i < n - 1; i++)
{
int best = i;
for (int j = i + 1; j < n; j++)
if (marks[j] > marks[best])
best = j;
int tm = marks[i]; marks[i] = marks[best]; marks[best] = tm; // swap marks
String tn = names[i]; names[i] = names[best]; names[best] = tn; // and the matching name
}
int total = 0;
for (int i = 0; i < n; i++)
total += marks[i];
double avg = (double) total / n;
int above = 0;
for (int i = 0; i < n; i++)
{
System.out.println((i + 1) + ". " + names[i] + " " + marks[i]);
if (marks[i] > avg)
above++;
}
System.out.println("average = " + Math.round(avg * 100) / 100.0 + ", above average = " + above);
}
}1. Dev 91
2. Mehr 91
3. Isha 85
4. Anu 78
5. Kabir 66
6. Ravi 59
average = 78.33, above average = 3
Case-study practice (Section D style)
Read the situation, then answer every part. In a school exam set on the board pattern this is a 5-mark question split into 2 + 2 + 1 marks.
Library catalogue
Suppose a school library stores 2,000 book titles in a String array sorted alphabetically, and the number of copies of each in a parallel int array. A student searches for a title; a librarian wants to know which book has the most copies and to print each title in capital letters.
(i) · 2 marks
Which search should be used, and about how many comparisons are needed at most?
Answer
Answer: Binary search, because the titles are sorted; at most about ⌊log₂ 2000⌋ + 1 = 11 comparisons (2¹¹ = 2048), using compareTo to compare titles.
1 mark method, 1 mark comparisons.
(ii) · 2 marks
How do you find the book with the most copies?
Answer
Answer: Traverse the copies array keeping the maximum and its index; print titles[index] at the end (parallel arrays share indices).
2 marks.
(iii) · 1 mark
Which String method prints a title in capitals, and does it change the stored title?
Answer
Answer: toUpperCase(); it returns a new String, leaving the stored title unchanged (Strings are immutable).
1 mark.
Where the marks are
How a typical answer is split into step marks. Show every step: a right final answer alone rarely earns full marks.
compareTo results (2 marks) (2 marks)
| Step | Marks |
|---|---|
| First differing characters and their code difference | 1 |
| Prefix case gives length difference | 1 |
Linear equations by elimination (5 marks) (5 marks)
| Step | Marks |
|---|---|
| Augmented matrix | 1 |
| Elimination step | 2 |
| Back substitution for both unknowns | 1 |
| Check | 1 |
Merit list program (10 marks) (10 marks)
| Step | Marks |
|---|---|
| Arrays declared and initialised | 1 |
| Selection sort in descending order | 3 |
| Parallel swap keeping names matched | 2 |
| Average without integer division | 2 |
| Printing and counting above average | 2 |
Where students lose marks in Arrays and Strings
The slips examiners see most often, and what to write instead.
Mistake: Loop condition i <= a.length
The last valid index is length − 1; a[length] throws an exception.
Do this: Use i < a.length.
Usually costs: 1-2 marks.
Mistake: Comparing strings with ==
== compares references, not characters.
Do this: Use equals or equalsIgnoreCase; compareTo for ordering.
Usually costs: 1 mark.
Mistake: Swapping only the marks in parallel arrays
Names no longer match their marks.
Do this: Swap the matching elements of every parallel array.
Usually costs: 2 marks.
Mistake: Applying binary search to unsorted data
Halving assumes order; results become wrong.
Do this: Sort first or use linear search.
Usually costs: 1-2 marks.
Practice questions on Arrays and Strings
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 · MCQExam level
What is the index of the last element of an array a?
(a) a.length
(b) a.length − 1
(c) a.length() − 1
(d) 0Hint
Indices start at 0.
Answer
(b) a.length − 1.
Q2 · 1 mark · Very short answerExam level
What does "Computer".substring(3) return?
Hint
From index 3.
Answer
"puter".
Q3 · 2 marks · Short answerExam level
Distinguish between linear and binary search on two bases.
Hint
Order and speed.
Answer
Linear search works on sorted or unsorted arrays, checking elements one by one (up to n comparisons). Binary search needs a sorted array and halves the search range each step (about log₂ n + 1 comparisons), so it is much faster for large arrays.
Q4 · 5 marks · Short answerHarder exam level
Write a loop that prints the initials of a full name, for example 'rahul kumar sharma' → 'R. K. Sharma'.
Hint
Split into words.
Answer
String[] parts = name.split(" "); String init = ""; for (int i = 0; i < parts.length - 1; i++) init += Character.toUpperCase(parts[i].charAt(0)) + ". "; String last = parts[parts.length - 1]; init += Character.toUpperCase(last.charAt(0)) + last.substring(1); For 'rahul kumar sharma' it gives 'R. K. Sharma'.
Q5 · 2 marks · Short answerHarder exam level
Show the array {5, 3, 8, 1} after each pass of bubble sort.
Hint
Three passes.
Answer
Pass 1: 3 5 1 8; pass 2: 3 1 5 8; pass 3: 1 3 5 8.
Q6 · 2 marks · Short answerHarder exam level
Write a method int[] columnSums(int[][] m) and give the result for {{2, 4}, {6, 8}, {1, 3}}.
Hint
Outer loop on columns.
Answer
int[] s = new int[m[0].length]; for (int j = 0; j < m[0].length; j++) for (int i = 0; i < m.length; i++) s[j] += m[i][j]; return s; Result: {9, 15}.
Q7 · 2 marks · Short answerHOTS / competency
Why does the insertion-sort pass that inserts 13 into 10 14 29 37 shift three elements?
Hint
Elements greater than the key.
Answer
Key 13 is compared with 37, 29 and 14, all larger, so each shifts one place right; then 10 < 13 stops the loop and 13 goes into index 1: 10 13 14 29 37.
Q8 · 5 marks · Short answerHOTS / competency
Write a method that returns the frequency of each vowel in a string (as an int array of size 5 for a, e, i, o, u), and give the result for "Education is an investment".
Hint
indexOf on "aeiou".
Answer
int[] f = new int[5]; String v = "aeiou"; s = s.toLowerCase(); for (int i = 0; i < s.length(); i++) { int k = v.indexOf(s.charAt(i)); if (k >= 0) f[k]++; } return f; For "Education is an investment": a = 2, e = 3, i = 3, o = 1, u = 1.
Q9 · 10 marks · Long answerHOTS / competency
Write a program to read a 3 × 3 matrix and check whether it is symmetric (equal to its transpose). Explain with {{1, 7, 3}, {7, 4, 5}, {3, 5, 6}}.
Hint
Compare a[i][j] with a[j][i].
Answer
Read the 9 values into int[][] a = new int[3][3] with nested loops (Scanner). Set boolean sym = true; for i from 0 to 2, for j from 0 to 2, if a[i][j] != a[j][i] then sym = false. Print 'Symmetric' or 'Not symmetric'. For the given matrix, a[0][1] = a[1][0] = 7, a[0][2] = a[2][0] = 3, a[1][2] = a[2][1] = 5, so it is symmetric. Checking only j > i halves the comparisons.
Q10 · 2 marks · Short answerHOTS / competency
What is the value of "Madam".toLowerCase().equals("madam") and why might a palindrome test ignore case?
Hint
Case changes characters.
Answer
true. 'M' and 'm' are different characters, so without ignoring case "Madam" would fail a strict palindrome test; converting to one case first compares letters, not their case.
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.First index of an array?Show answer
0.
2.Array length?Show answer
a.length (field).
3.Binary search needs?Show answer
A sorted array.
4.Bubble sort comparisons for n elements?Show answer
n(n − 1) ÷ 2.
5.substring(b, e) length?Show answer
e − b.
6.Compare two Strings for equal content?Show answer
equals().
Best order to study it
- 1-D arrays: declaration, indexing, traversal.
- Maximum, minimum, linear and binary search.
- Bubble, selection and insertion sort with traces.
- 2-D arrays; matrix tasks; linear equations.
- String class methods.
- String algorithms: reverse, palindrome, words, initials.
Quick revision checklist
The night before the exam: can you do each of these without looking?
- Array: same type, fixed size, index 0 to length − 1; out of range → exception.
- Linear search any order (n comparisons); binary search sorted (≈ log₂ n + 1).
- Bubble swaps neighbours; selection picks the minimum; insertion slides elements in.
- 2-D: a[i][j], a.length rows, a[0].length columns; nested loops.
- String immutable: length(), charAt, substring, indexOf, concat, replace, compareTo, equals.
- Linear equations: augmented matrix, forward elimination, back substitution.
Arrays and Strings: frequently asked questions
Which sorting method should I use in exams?
Use the one the question names. If free to choose, selection sort is simplest to write correctly; insertion sort is best for nearly sorted data; bubble sort is easiest to trace.
Why does s.replace('e', '*') not change s?
Strings are immutable; replace returns a new String. Assign it if needed: s = s.replace('e', '*');
Can a 2-D array have rows of different lengths?
Yes; Java's 2-D arrays are arrays of arrays, so rows can differ (jagged arrays). In school problems, matrices are usually rectangular, so a[0].length gives the column count.
Based on: CISCE ISC 2028 Computer Science syllabus (Classes XI and XII); CISCE Regulations for the ISC Examination Year 2028 (Class XI internal examination)