0% found this document useful (0 votes)
74 views14 pages

Class X Methods

This document discusses user defined methods in Java. It provides examples of function prototypes, parameters, return types, recursion, overloading and different ways functions can be invoked. Multiple choice questions are included to test understanding of key concepts like call by value vs. call by reference, pure vs. impure functions, and advantages of defining methods. Several examples are given to explain concepts like passing arguments, returning values, and modifying parameters.

Uploaded by

ANDROID GAMER
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)
74 views14 pages

Class X Methods

This document discusses user defined methods in Java. It provides examples of function prototypes, parameters, return types, recursion, overloading and different ways functions can be invoked. Multiple choice questions are included to test understanding of key concepts like call by value vs. call by reference, pure vs. impure functions, and advantages of defining methods. Several examples are given to explain concepts like passing arguments, returning values, and modifying parameters.

Uploaded by

ANDROID GAMER
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
You are on page 1/ 14

Chapter 5

User Defined Methods

Class 10 - APC Understanding Computer Applications with BlueJ

Fill in the blanks

Question 1

A function can return only one value to its caller program.

Question 2

If a function does not return any value its return type is void.

Question 3

A function indicating the function name, return type along with function
arguments is known as function header/prototype.

Question 4

The variables used to receive the values in function header are known
as formal parameters.

Question 5

Method in a Java program resides in package.

Question 6

The entire function body is enclosed under curly brackets.

Question 7

The procedural function performs some actions without returning any output.

Question 8

A function contains header and body.

Question 9
By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed
Functions used with same name but different types of arguments are known
as function overloading.

Question 10

A function that is called by itself in its body is known as recursive function.

Tick the correct option

Question 1

The function which changes the state of an object is known as:

1. pure function
2. impure function ✓
3. replace function
4. none of the above

Question 2

Parameters used in function call statement are known as:

1. defined parameter
2. passed parameter
3. actual parameter ✓
4. formal parameter

Question 3

Parameters used in the function definition are called:

1. forward parameter
2. actual parameter
3. formal parameter ✓
4. none of the above

Question 4

The process of calling a function in such a way that the change in the formal
arguments reflects on the actual parameter is known as:
By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed
1. call by reference ✓
2. call by value
3. call by method
4. none

Question 5

A function with many definitions is called:

1. multiple function
2. function overloading ✓
3. floating function
4. none

Question 6

A function may be associated with:

1. return ✓
2. call
3. promote
4. none

Question 7

Which of the following type can be used for a non-returnable function?

1. int
2. float
3. double
4. void ✓

Question 8

A function body is enclosed within a pair of:

1. { } ✓
2. [ ]
By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed
3. ( )
4. under a rectangular box

Question 9

A function is invoked through an:

1. object ✓
2. system
3. parameter
4. none

Write TRUE or FALSE

Question 1

Calling and invoking a function is same.


True

Question 2

A method can use a single return statement.


True

Question 3

Overloading of methods even depends on return type.


False

Question 4

A function cannot be defined without parameters.


False

Question 5

A function body is enclosed within curly brackets.


True

Answer the following

By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed


Question 1

Define a function. What is meant by function prototype?

A function or a method is a sequence of statements grouped together and


given a name. This group of statements can be called at any point in the
program using its name to perform a specific task.
First line of function definition that tells about the type of value returned by
the function and the number and type of arguments is called function
prototype.

Question 2

What are the two ways of invoking functions?

Two ways of invoking functions are:

1. Pass by value.
2. Pass by reference.

Question 3

When a function returns the value, the entire function call can be assigned to a
variable. Do you agree with the statement?

Yes, when a function returns a value, we can assign the entire function call to
a variable. Below example illustrates the same:

public class Example {

public int sum(int a, int b) {


int c = a + b;
return c;
}

public static void main(String args[]) {


Example obj = new Example();
int x = 2, y = 3;
int z = obj.sum(x, y);
System.out.println(z);
}
}

By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed


Question 4

When a function is invoked how many values can be returned from the
function?

A function can only return a single value.

Question 5

Debug the errors and rewrite the following function prototypes:

(a) int sum(x,y);

Answer

int sum(int x, int y)


(b) float product(a,int y);

Answer

float product(float a, int y)


(c) float operate(int x, float=3.4);

Answer

float operate(int x, float y)


(d) float sum(int x,y);

Answer

float sum(int x, float y)

Question 6

Write down the main function which calls the following function:

int square(int a)
{
return(a*a);
}

Answer

By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed


public static void main(String args[]) {
int sq = square(4);
}

Question 7

What happens when a function is passed by reference? Explain.

Pass by reference means that the arguments of the function are a reference
to the original objects and not a copy. So any changes that the called function
makes to the objects are visible to the calling function. Consider the below
example:

class PassByReferenceExample {
public void demoRef(int a[]) {
for (int i = 0; i < 5; i++) {
a[i] = i;
}
}

public static void main(String args[]) {


PassByReferenceExample obj = new PassByReferenceExample();
int arr[] = { 10, 20, 30, 40, 50 };
System.out.println("Before call to demoRef value of arr");
for (int i = 0; i < 5; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
obj.demoRef(arr);
System.out.println("After call to demoRef value of arr");
for (int i = 0; i < 5; i++) {
System.out.print(arr[i] + " ");
}
}
}
The output of this program is:

Before call to demoRef value of arr


10 20 30 40 50
After call to demoRef value of arr
01234

By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed


Here demoRef changes the values of array a and these changes are reflected
in the array in the main method as well.

Question 8

In what situation does a function return a value?

For a function to return a value, it should have a return type other than void
in its function prototype and it should return a value of the corresponding
type using the return statement in the function body.

Question 9

Differentiate between pure and impure functions.

Pure functions Impure functions

Pure functions take objects and/or primitive data types Impure functions change the
as arguments but does not modify the objects. state of received objects.

Impure functions have side


Pure functions doesn't have side effects.
effects.

Question 10

Write a function which is used to swap the values of two memory locations.

(a) by using a third variable.

Answer

void swap(int a, int b) {


int c = a;
a = b;
b = c;
System.out.println("a = " + a + "\t" + "b = " + b);
}
(b) without using a third variable.

Answer
By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed
void swap(int a, int b) {
a = a + b;
b = a - b;
a = a - b;
System.out.println("a = " + a + "\t" + "b = " + b);
}

Question 11

Call by value Call by reference

Actual parameters are copied to formal Formal parameters refer to actual


parameters. Any changes to formal parameters. The changes to formal
parameters are not reflected onto the actual parameters are reflected onto the actual
parameters. parameters.

All reference data types like arrays and


All primitive data types are passed using Call
objects of classes are passed using Call by
by value.
reference.

Differentiate between call by value and call by reference.

Question 12

What are the advantages of defining a method in a program?

Advantages of defining methods in a program are:

1. Methods help to manage the complexity of the program by dividing a


bigger complex task into smaller, easily understood tasks.
2. Methods are useful in hiding the implementation details.
3. Methods help with code reusability.

Question 13

What is meant by function overloading? In what way it is advantageous?

Function overloading is the process of defining functions/methods within a


class, that have the same name but differ in the number and/or the data
types of their arguments. Advantages of function overloading are:
By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed
1. Function overloading is one of the ways in which Java implements the
object oriented concept of Polymorphism.
2. With Function overloading, programmers don't have to create and
remember different names for functions doing the same thing for
different data types.

Question 14

Define the following:

(a) Return data type

Return data type specifies the type of value that the method should return. It
is mentioned before the method name in the method prototype. It can be
any valid primitive or composite data type of Java. If no value is being
returned, it should be void.

(b) Access specifier

Access specifiers determine the type of access to the method. It can be either
public, private or protected.

(c) Parameter list

Parameter list is a comma-separated list of variables of a method along with


their respective data types. The list is enclosed within a pair of parentheses.
Parameter list can be empty if the method doesn't accept any parameters
when it is called.

(d) Recursive function

A function that calls itself inside its body is called a Recursive function.

(e) Method signature

Method signature comprises of the method name and the data types of the
parameters. For example, consider the below method:

int sum(int a, int b) {


int c = a + b;
return c;
}

By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed


Its method signature is:
sum(int, int)

Question 15

Explain the function of a return statement in Java programming.

A function returns a value through the return statement. Once a return


statement is executed, the program control moves back to the caller function
skipping the remaining statements of the current function if any. A function
can have multiple return statements but only one of them will be executed.
For example, consider the below method:

int sum(int a, int b) {


int c = a + b;
return c;
}
It uses a return statement to return a value of int type back to its caller.

Question 16

Differentiate between formal parameter and actual parameter.

Formal parameter Actual parameter

Formal parameters appear in function Actual parameters appear in function call


definition. statement.

They represent the values received by the They represent the values passed to the
called function. called function.

Question 17

What is the role of the keyword void in declaring functions?

The keyword 'void' signifies that the function doesn't return a value to the
calling function.

Question 18

By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed


If a function contains several return statements, how many of them will be
executed?

A function can have multiple return statements but only one of them will be
executed because once a return statement is executed, the program control
moves back to the caller function skipping the remaining statements of the
current function.

Question 19

Which OOP principle implements function overloading?

Polymorphism implements function overloading.

Question 20

How are the following data passed to a function?

 Primitive types
By value
 Reference types
By reference

Give output of the following function definitions and also write what
mathematical operations they carry out

Question 1

void test1(int n)
{
for(int x=1; x<=n; x++)
if(n%x == 0)
System. out.println(x);
}
if 12 is passed to n.

Output

1
2
3
4
By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed
6
12

Explanation

This function finds the factors of n.

Question 2

void test2(int a, int b)


{
while( a != b)
{
if ( a > b)
a = a — b;
else
a = b — a;
}
System.out.println(a);
}
if 4 and 17 are passed to the function.

Output

Infinite Loop

Explanation

Initial value of a is 4 and b is 17 as given in the question. As a and b are not


equal, condition of while loop is true, first iteration starts. a is less than b so if
condition is false, a = b - a is executed and a becomes 17 - 4 = 13. Condition of
while loop is true so second iteration starts. Again, if condition is false. This
time a becomes 17 - 13 = 4. Like this, the value of a keeps oscillating between
13 and 4 resulting in an infinite loop.

Question 3

void test3(char c)
{
System.out.println( (int) c);
}

By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed


if 'm' is passed to c.

Output

109

Explanation

This function prints the ASCII code of its argument. When 'm' is passed to this
function, its ASCII code which is 109 gets printed as the output.

Question 4

void test4(String x, String y)


{
if(x.compareTo(y) > 0)
System.out.println(x);
else
System.out.println(y);
}
if "AMIT" and "AMAN" are passed to the function.

Output

AMIT

Explanation

The first differing characters of "AMIT" and "AMAN" are 'I' and 'A',
respectively. So output of "AMIT".compareTo("AMAN") will be ASCII Code of 'I'
- ASCII Code of 'A' ⇒ 73 - 65 ⇒ 8. The if condition is true so string x which is
"AMIT" gets printed as the output.

By Sir Sanjoy Banerjee(MCA,M.Sc(Maths),B.Ed

You might also like