User-defined Methods

1. What is a method(function)?

Ans- A method is a set of codes to be executed to peform a specific task.

Example :-

public void work( )

{

int a=5, b=6;

System.out.println(a+b);

}

A function is a set of instructions.

2. What is the need of a method?

Ans- i)Program Modular – Divide the program into smaller units called Modules or functions. Hence, making a program easy to debug and maintain.

ii) Error Detection easier – Whenever an error reported to the program, we only need to debug the function in which the error has occured rather than whole program.

iii) Black box – It hides the details of how the problem is being solved.

iv) Redundnecy – Once a function is defined in a program, it can be called anywhere and reused any number of times in a program to solve a specific problem.

v) Better memory management – Use of function ensures better memory management because functions are called whenever they are required.

vi) Easily updated – Whenever a function update, it does not effect other functions.

vii) Scalability – The functional design povides scalability so that you can easily assign the functions to different developers.

3. Write down the syntax of a function.

Ans- [access specifier] [static] [return type] method-name (formal parameters)

{

Statements;

}

4. Discuss the parts of a function.

Ans- a) Access Specifier – It determine the amount of access the function has been given by the rest of the program.

Three types of Access Specifier ae :-

i) Public (Default Access Specifier) – It can be accessed by member as well as non-member of the class.

ii) Private – Only member of the class can access.

iii) Protected – Member of class as well as sub class can access.

b) Static Keyword – A function can be called without creating an object.

c) Return type – It determine the data.

In case of void keyword, the function does not return any value.

d) Function name – It is the name of the function and the naming of function must be follow the rule of an Identifier.

e) Parentheses – Parentheses are used to define the parameters to input the data.

f) Formal Parameters – Formal Parameters are the variables which are defined within the parentheses to receive a value from the user.

Example :- public void work(int n)

g) Function or Method block – The statements are written in a function by using two curly braces.

5. What is a method definition?

Ans- To declare a method with code of header is called method definition.

Example:- i) public void work( )

ii) public int work( )

6. Give an example of defining a method.

Ans- i) public void work( )

{

System.out.println(“Ant”);

}

ii) public int work( )

{

int a=2, b=3;

retun a+b;

}

7. What do you mean by non access modifiers?

Ans- Non access modifiers means while declaring a function, these modifiers are not needed but whenever it required it was mention in function.

Example:- i) Static modifier

ii) Final modifier

iii) Abstract modifier

8. Write down the use of Non Access Modifiers.

Ans- i) Static modifier – To create a class, methods and variable.

ii) Final modifier – For finalising the implementation of classes, methods and variables.

iii) Abstract modifier – To create abstract classes and methods.

9. Write down the types of methods.

Ans- There are two types of methods:-

i) Predefined Library Method

ii) User defined Method

10. What is the difference between Predefined Library Method and User defined Method?

Ans- Predefined Library Method

i) These methods are in-built (predefined) methods, already defined in a class.

ii) Ex:-pow( ), sqrt( ), etc.

User defined Method

i) These methods are user-defined methods.

ii)Ex:-work( )

11. What is a main method?

Ans- A main method is a method in Java is static and it can be called directly by the JVM(Java Virtual Machine). In main method, all the executions are by JVM(Interpreter).

12. What is an actual parameter?

Ans- Actual parameters are the value which are assigned to a formal parameter.

13. What is a formal parameter?

Ans- Formal parameters are the variables which are defined in a function to receive the value from the user.

14. Give an example of actual and formal parameter.

Ans- public int work(int a, int b)

{

return a+b;

}

15. Write down the syntax to call a function by an object.

Ans- <object name> <function name>

Ex:- Std10 Alex=new Std10( );

Alex.work( );

16. What do you mean by calling mechanism?

Ans- Calling mechanism means to call a function by using call by value and call by reference in a program.

Example:-

public class callbyvalue

{

public void add(int a, int b, int c, int d, int e)

{

S=a+b+c+d+e;

System.out.println(S);

}

public void mul(int a, int b, int c, int d, int e)

{

M=a*b*c*d*e;

System.out.println(M);

}

}

public class call

{

public void main( )

{

callbyvalue ob=new callbyvalue( )

ob.add(10,20,30,40,50)

ob.mul(10,20,30,40,50)

}

}

class callbyreference

{

public callbyreference(int a, int b, int c, int d, int e)

{

M1=a;

M2=b;

M3=c;

M4=d;

M5=e;

}

public void add(callbyrefeence ob)

{

T=ob.M1+ob.M2+ob.M3+ob.M4+ob.M5;

System.out.println(T);

}

public void mul(callbyreference ob)

{

M=ob.M1*ob.M2*ob.M3*ob.M4*ob.M5;

System.out.println(M);

}

}

public class call

{

public void main( )

{

callbyreference ob=new callbyreference(10,20,30,40,50)

ob.add(ob);

ob.mul(ob);

}

}

17. What is the difference between call by value and call by reference?

Ans- Call by value

i) Values are passed.

ii) Ex:- public void work(int a)

ob.work(5)

Call by reference

i) Objects are passed.

ii) Ex:- public void work(class name object)

18. Write down the categories of methods.

Ans- Functions are divided into two categories:-

i) Pure functions or Accessor Methods

ii) Impure functions or Mutator Methods

19. What is a pure function?

Ans- The function which does not change the state is called pure function.

Ex:- public int work(int a, int b)

{

c=a+b;

return c;

}

20. What is an impure function?

Ans- The function which change the state of an object is called impure function.

Ex:- public void work(int a, int b)

{

c=a+b;

System.out.println(c);

}

21. Wite down the classification of methods accoding to method definition.

Ans- The function can be classified in four types:-

i) Function which does not have argument or a return type.

ii) Function which have argument but no return type.

iii) Function which have no argument but return type.

iv) Function which have both argument and return type.

23. What do you mean by function overloading?

Ans- A program having more than one function with same name but different parameter list is called function overloading.

24. What is the need of function overloading?

Ans- Objects have certain characteristics and associated behaviour, but an object may have differently in different situations. To simulate real world objects programming, function overloading is required.

Function overloading solve the principle of polymorphism.

25. What is an early binding and static binding?

Ans- The compiler select the appropriate function for a call at compile time itself is called early binding and static binding.

26. What is the role of “this” keyword in Java program?

Ans- This keyword is used inside any method to refer the correct object. They refer to variables of current objects of the class.

public Box(double w, double h, double d)

{

this.width=w;

this.height=h;

this.depth=d;

}

Leave a Reply

Your email address will not be published. Required fields are marked *