0% found this document useful (0 votes)
10 views3 pages

Java User-Defined Methods Guide

The document provides an answer key for a Class X ICSE Board examination on user-defined methods in Java, including multiple choice questions and short answer questions. It covers definitions, syntax, method overloading, and examples of methods such as calculating factorial, checking for palindromes, and counting vowels. Additionally, it highlights the advantages of using methods in programming.

Uploaded by

goku011111
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Java User-Defined Methods Guide

The document provides an answer key for a Class X ICSE Board examination on user-defined methods in Java, including multiple choice questions and short answer questions. It covers definitions, syntax, method overloading, and examples of methods such as calculating factorial, checking for palindromes, and counting vowels. Additionally, it highlights the advantages of using methods in programming.

Uploaded by

goku011111
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Nalanda Public School – Class X (ICSE Board)

Answer Key: User-Defined Methods (Java)

Part A – Multiple Choice Questions (Answers)

1. (c) sum()
2. (b) Type of value returned
3. (a) No return value
4. (b) Body
5. (c) Both
6. (a) returnType methodName()
7. (b) Overloaded
8. (a) By value
9. (b) void
10. (a) Only one value

Part B – Short Answer Questions (Solutions)

1. Define a user-defined method with an example.


A user-defined method is a block of code created by the programmer to perform a specific task and can
be called multiple times. Example:
int add(int a, int b) { return a + b; }.

2. Write the syntax of a method definition in Java.


Syntax:
accessModifier returnType methodName(parameterList) { // method body }

3. Differentiate between pure and impure methods.


Pure methods always return the same result for same inputs and do not have side effects (do not modify
external state). Impure methods may perform I/O or modify global/state data.

4. What is the purpose of a return statement?


The return statement sends a value back to the caller and ends the method execution. For void methods,
return can be used alone to exit early.

5. Explain actual and formal parameters with an example.


Actual parameters (arguments) are values passed in the method call, formal parameters are variables
declared in the method definition that receive the values. Example: in sum(5,3), 5 and 3 are actuals; in
int sum(int a, int b), a and b are formals.

6. What is method overloading? Write one example.


Method overloading is having multiple methods with same name but different parameter lists in the same
class. Example:
int area(int r) and double area(double r).

Page 1
7. Explain call by value with an example.
Java uses call by value — a copy of the variable is passed. For primitives, changes inside the method
don't affect the caller. Example:
void demo(int x) { x = x + 5; } calling with demo(10) does not change the caller's 10.

8. State any two advantages of using methods in programming.


1. Reusability of code. 2. Better organization and easier debugging/maintenance.

Page 2
Part C – Programming Questions (Sample Solutions)

1. Method to calculate factorial of a number


/* Returns factorial of n (n >= 0) */ int factorial(int n) { int f = 1; // initialize
result for (int i = 1; i <= n; i++) { f *= i; // multiply by i } return f; } //
Sample usage in main: public static void main(String[] args) { int num = 5; // Input
[Link](factorial(num)); // Output: 120 }
Sample I/O: Input: 5 Output: 120

2. Overloaded methods area(): circle and rectangle


/* Area of circle */ double area(double r) { return [Link] * r * r; // use [Link]
for precision } /* Area of rectangle */ int area(int l, int b) { return l * b; } //
Sample usage: public static void main(String[] args) { [Link](area(7));
// For circle, radius 7 -> prints area [Link](area(5, 4)); // For
rectangle -> prints 20 }
Sample I/O: Input: radius=7 -> Output: 153.938... ; Input: length=5,breadth=4 -> Output: 20

3. Method to check palindrome number


/* Returns true if n is palindrome */ boolean isPalindrome(int n) { int rev = 0; int
temp = n; while (temp > 0) { rev = rev * 10 + (temp % 10); temp /= 10; } return rev
== n; } // Sample usage: public static void main(String[] args) {
[Link](isPalindrome(121)); // Output: true
[Link](isPalindrome(123)); // Output: false }
Sample I/O: Input: 121 -> Output: true ; Input: 123 -> Output: false

4. Method findMax() to return greatest of three integers


int findMax(int a, int b, int c) { if (a >= b && a >= c) return a; else if (b >= a &&
b >= c) return b; else return c; } // Sample usage: public static void main(String[]
args) { [Link](findMax(12, 7, 25)); // Output: 25 }
Sample I/O: Input: 12,7,25 -> Output: 25

5. Method to count vowels in a string


/* Counts vowels in the given string */ int countVowels(String str) { int count = 0;
str = [Link](); for (int i = 0; i < [Link](); i++) { char ch =
[Link](i); if ("aeiou".indexOf(ch) != -1) { count++; } } return count; } //
Sample usage: public static void main(String[] args) {
[Link](countVowels("Education")); // Output: 5 }
Sample I/O: Input: Education -> Output: 5

Page 3

You might also like