LIWA

User-Defined Methods

ICSE Class 10 · Computer Applications · Chapter 3

What this chapter covers

Key points

Questions answered

Define a user-defined method and give two reasons for using methods in a program.
A user-defined method is a named block of statements, written by the programmer inside a class, that performs one specific task and can be called when needed. Methods allow code to be reused without repetition, and they make a program modular, so errors are easier to find and correct.
Distinguish between a method prototype and a method signature, using `public static int sum(int a, int b)` as the example.
The prototype is the whole first line, `public static int sum(int a, int b)`, including the access specifier, modifier and return type. The signature is only the name with the parameter types, `sum(int, int)`.
Write the prototype of a method `isPalindrome` that accepts an integer and returns `true` or `false`.
`boolean isPalindrome(int n)`
Identify the actual and formal parameters in: `double avg(int x, int y) { return (x + y) / 2.0; }` called as `double r = avg(m1, m2);`.
`m1` and `m2` are the actual parameters; `x` and `y` are the formal parameters.
What is the value returned by `avg(7, 8)` in Question 4, and why is `2.0` used instead of `2`?
(7 + 8) / 2.0 = 7.5. Dividing by `2.0` makes the division a `double` division; dividing by `2` would do integer division and give 7, which would then be stored as 7.0.
Why does the statement `int x = display();` give an error if `display()` is declared as `void display()`?
A `void` method returns no value, so there is nothing to assign to `x`. A `void` method must be called as a statement on its own.
Give the output, and state which way of passing is used. `static void inc(int n) { n++; }` with `int a = 5; inc(a); System.out.println(a);`
The output is `5`. The value of `a` is passed by value, so only the copy `n` becomes 6.
Why can `main()` call `Math.sqrt()` without creating an object, but not a non-static method of its own class?
`Math.sqrt()` is static, so it belongs to the class and is called with the class name. A non-static method belongs to an object; `main()` is static and has no object, so it must create one and call the method through it.

Past board exam questions

ICSE 2026 board paper · 15 marks

Define a class to overload the method format as follows: void format (): To print the following pattern using Nested for loops only: 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5 int format (String s): To calculate and return the sum of ASCII codes of each character of the String. Example: CAB Output: 67 + 65 + 66 198 void format (int n): To calculate and display the sum of natural numbers up to n given by the formula. (n(n+1))/(2)

Show answer
``` class Overload { void format() { for (int i = 1; i <= 5; i++) { for (int j = i; j <= 5; j++) System.out.print(j + " "); System.out.println(); } } int format(String s) { int sum = 0; for (int i = 0; i < s.length(); i++) sum += (int) s.charAt(i); return sum; } void format(int n) { int sum = n * (n + 1) / 2; System.out.println("Sum of natural numbers up to " + n + " = " + sum); } public static void main(String args[]) { Overload ob = new Overload(); ob.format(); System.out.println(ob.format("CAB")); …

ICSE 2026 board paper · 1 mark

In the following method prototype to accept a character, an integer and return YES or NO, fill in the blank to complete the method prototype. public ________ someMethod (char ch, int n)

  1. boolean
  2. String
  3. int
  4. double
Show answer
String

ICSE 2026 board paper · 1 mark

In a calculator which Java feature allows multiple methods named calculate() for the different operations?

  1. abstraction
  2. inheritance
  3. encapsulation
  4. polymorphism
Show answer
polymorphism

ICSE 2025 compartment paper · 15 marks

Define a class to overload the method transform as follows: int transform(int n) – to return the sum of the digits of the given number Example: n = 458 output : 17 void transform(String s) – to convert the given String to upper case and print Example: if S = "Blue" Output : BLUE void transform (char ch) – to print the character ch in 3 rows and 3 columns using nested loops. Example: if ch = '@' Output : @@@ @@@ @@@

Show answer
class Overload { int transform(int n) { int sum = 0; n = Math.abs(n); while (n > 0) { sum = sum + n % 10; // add last digit n = n / 10; } return sum; } void transform(String s) { System.out.println(s.toUpperCase()); } void transform(char ch) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) System.out.print(ch); System.out.println(); } } public static void main(String args[]) { Overload ob = new Overload(); System.out.println(ob.transform(458)); // 17 …

ICSE 2025 board paper · 15 marks

Define a class to overload the method print( ) as follows: void print ( ) – To print the given format using nested loops. ``` @#@#@ @#@#@ @#@#@ @#@#@ ``` double print(double a, double b) – To display the sum of numbers between a and b with difference of 0.5. e.g. if a = 1.0, b = 4.0 output is: 1.0 + 1.5 + 2.0 + 2.5 + ……………. + 4.0 int print(char ch1, char ch2) – compare the two characters and return the ASCII code of the largest character.

Show answer
class Overload { void print() { for (int i = 1; i <= 4; i++) // 4 rows { for (int j = 1; j <= 5; j++) // 5 symbols in each row { if (j % 2 == 1) System.out.print("@"); else System.out.print("#"); } System.out.println(); } } double print(double a, double b) { double sum = 0; for (double x = a; x <= b; x += 0.5) sum += x; System.out.println("Sum = " + sum); return sum; } int print(char ch1, char ch2) { if (ch1 > ch2) return (int) ch1; …

ICSE 2025 sample paper · 15 marks

Define a class to overload the method display() as follows: void display(): To print the following format using nested loop. ``` 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 ``` void display (int n, int m) : To print the quotient of the division of m and n if m is greater than n otherwise print the sum of twice n and thrice m. double display (double a, double b, double c) – to print the value of z where z = p × q p = (a+b)/(c) q = a + b + c

Show answer
```java class Overload { void display() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 5; j++) { if (j % 2 == 1) System.out.print("1 "); else System.out.print("2 "); } System.out.println(); } } void display(int n, int m) { if (m > n) System.out.println("Quotient = " + (m / n)); else System.out.println("Sum = " + (2 * n + 3 * m)); } double display(double a, double b, double c) { double p = (a + b) / c; double q = a + b + c; double z = p * q; …

ICSE 2025 compartment paper · 1 mark

Which statement is correct for the method prototype given below: int check(char ch, String s)

  1. check( ) does not return any value
  2. check( ) has return type int
  3. check method has two actual parameters
  4. check( ) is a constructor
Show answer
check( ) has return type int

ICSE 2025 compartment paper · 1 mark

The statement given below is termed as: pubic void Accept(int a)

  1. Method signature
  2. Method block
  3. Method prototype
  4. Constructor
Show answer
Method prototype