LIWA

Arrays

ICSE Class 10 · Computer Applications · Chapter 7

What this chapter covers

Key points

Questions answered

Define an array. Why is an array called a composite data type?
An array is a collection of a fixed number of values of the same type, stored in consecutive memory locations under one name and reached by an index. Arrays are composite because they are built from many values of primitive types and hold all of them under a single name.
Write a statement to (a) declare an array `price` of 20 `double` values and (b) initialise a `char` array with the letters of the word "INDIA".
(a) `double price[] = new double[20];` (b) `char w[] = {'I', 'N', 'D', 'I', 'A'};`
What is the difference between `length` and `length()`?
`length` is a property of an array that gives its number of elements and is written without brackets. `length()` is a method of the `String` class that gives the number of characters in a string.
How many bytes does `long x[] = new long[15];` occupy, and what is the largest valid index?
15 × 8 = 120 bytes; the largest index is 14.
What happens when the statement `a[a.length] = 5;` is executed?
The program compiles, but at run time Java stops it with an `ArrayIndexOutOfBoundsException`, because the last valid index is `a.length - 1`.
Give the output: `int p[] = {2, 7, 1, 8, 2, 8}; int c = 0; for (int i = 0; i < p.length; i++) if (p[i] % 2 == 0) c++; System.out.println(c);`
The even elements are 2, 8, 2 and 8, so the output is `4`.
State two differences between linear search and binary search.
Linear search works on sorted or unsorted arrays, while binary search needs a sorted array. Linear search checks elements one by one from the start, while binary search checks the middle element and discards half of the remaining array each time, so it is much faster on large arrays.
Why does binary search give a wrong result on an unsorted array?
Binary search decides which half to discard by assuming every element to the left of the middle is smaller and every element to the right is larger. In an unsorted array that assumption is false, so the key may be in the discarded half.

Past board exam questions

ICSE 2026 board paper · 15 marks

Write a program to accept the designations of 100 employees in a single dimensional array. Accept the designation from the user and print the total number of employees with the designation given by the user as input. Example: | Trainee | Manager | Chef | Manager | Director | Manager | |---|---|---|---|---|---| Input: Manager Output: 3

Show answer
``` import java.util.Scanner; class Designation { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String d[] = new String[100]; System.out.println("Enter designations of 100 employees:"); for (int i = 0; i < 100; i++) d[i] = sc.nextLine(); System.out.print("Enter designation to search: "); String key = sc.nextLine(); int count = 0; for (int i = 0; i < 100; i++) { if (d[i].equalsIgnoreCase(key)) count++; } System.out.println("Total number of employees with designation " + key + " = " + count); } } ```

ICSE 2026 board paper · 15 marks

Write a program to accept a two-dimensional integer array of order 4 x 5 as input from the user. Check if it is a Sparse Matrix or not. A matrix is considered to be a sparse, if the total number of zero elements is greater than the total number of non-zero elements. Print appropriate messages. Example: | 4 | 3 | 0 | 1 | 0 | |---|---|---|---|---| | 1 | 0 | 0 | 2 | 0 | | 1 | 0 | 1 | 0 | 0 | | 0 | 3 | 2 | 0 | 0 | Number of zero elements = 11 Number of non zero elements = 9 Matrix is a Sparse Matrix

Show answer
``` import java.util.Scanner; class SparseMatrix { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a[][] = new int[4][5]; int zero = 0, nonZero = 0; System.out.println("Enter 20 elements of the 4 x 5 matrix:"); for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { a[i][j] = sc.nextInt(); if (a[i][j] == 0) zero++; else nonZero++; } } System.out.println("Number of zero elements = " + zero); System.out.println("Number of non zero elements = " + …

ICSE 2026 board paper · 1 mark

An array with 3 elements is arranged in ascending order as follows: [4, 1, 3] → (4 and 1 are swapped) → [1, 4, 3] → (4 and 3 are swapped) → [1, 3, 4] Name the technique used:

  1. Bubble sort
  2. Linear Search
  3. Selection sort
  4. Binary Search
Show answer
Bubble sort

ICSE 2026 board paper · 1 mark

The sales made by 5 salesmen selling 5 products is stored in a two-dimensional array of integer data type. How many bytes does the array occupy?

  1. 25
  2. 200
  3. 50
  4. 100
Show answer
100

ICSE 2026 board paper · 2 marks

The following program segment swaps the first element and the second element of the given array without using the third variable, fill in the blanks with appropriate java statements: ``` void swap() { int x[ ] = {4, 8, 19, 24, 15}; (1) __________; (2) __________; x[0] = x[0] / x[1]; System.out.println(x[0]+" "+x[1]); } ```

Show answer
(1) x[0] = x[0] * x[1]; (2) x[1] = x[0] / x[1]; (x[0] becomes 32, x[1] becomes 32/8 = 4, then x[0] becomes 32/4 = 8; output: 8 4)

ICSE 2026 board paper · 2 marks

int X[ ][ ] = {{4, 5}, {7, 2}, {19, 4}, {7, 4}}; Write the index of the maximum element and the index of the minimum element of the array.

Show answer
Maximum element 19 is at index X[2][0] (row 2, column 0). Minimum element 2 is at index X[1][1] (row 1, column 1).

ICSE 2025 board paper · 15 marks

Define a class to accept values into a 4 x 4 integer array. Calculate and print the NORM of the array. NORM is the square root of sum of squares of all elements. | 1 | 2 | 1 | 3 | |---|---|---|---| | 5 | 2 | 1 | 6 | | 3 | 6 | 1 | 2 | | 3 | 4 | 6 | 3 | Sum of squares of elements = 1+4+1+9+25+4+1+36+9+36+1+4+9+16+36+9 = 201 NORM = Squareroot of 201 = 14.177446878757825

Show answer
import java.util.Scanner; class Norm { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a[][] = new int[4][4]; // the 4 x 4 array int sum = 0; // sum of squares of all elements System.out.println("Enter 16 integers:"); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { a[i][j] = sc.nextInt(); sum += a[i][j] * a[i][j]; } } double norm = Math.sqrt(sum); System.out.println("Sum of squares of elements = " + sum); System.out.println("NORM = " + norm); } }

ICSE 2025 compartment paper · 15 marks

Define a class to accept values into a 3x3 integer array and print the product of each row elements. Example: | 3 | 1 | 2 | |---|---|---| | 4 | 2 | 1 | | 5 | 1 | 2 | Output: Row 0 – 6 Row 1 – 8 Row 2 – 10

Show answer
import java.util.Scanner; class RowProduct { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a[][] = new int[3][3]; // 3x3 array System.out.println("Enter 9 integers:"); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) a[i][j] = sc.nextInt(); for (int i = 0; i < 3; i++) { long p = 1; // product of row i for (int j = 0; j < 3; j++) p = p * a[i][j]; System.out.println("Row " + i + " - " + p); } } }