User – defined Methods
- Categories Functions in Java, Notes
In this post we will cover loops in java. You can watch our videos on Java Functions & Examples And Java Function Exam Questions
Q. 1 What is a Method or Function? Why do we use function (or method)?
Ans. A function is a named block of code that together performs a specific task. It can be called any number of times. Functions are used to:
- Divide a large code into modules, due to this we can easily debug and maintain the code.
- It helps in reusing the code.
- Program becomes easy to understand.
Q. 2 What is void return type?
Ans. Void is a keyword used in Java to indicate a method does not return any value.
Q. 3 Do we need to write return in method body which has a return type of void?
Ans. No. return is optional.
Q. 4 Can we call return from the middle/start of method body?
Ans. Yes. A program can have multiple return statements but only one first encountered will be executed
Q. 5 Give the prototype/method signature of following
- Function find which receives a sentence sent and word wrd and returns 1 or 0
- Method isCorrect which receives a character ch and a integer n and returns true or false
- Function power that takes in in two integer values and returns result of double type.
Ans.
- int find( String sent, String wrd)
- boolean isCorrect(char ch, int n)
- double power ( int a, int b)
Q. 6 Why main() function so special?
Ans. The main() method is the entry point into the application. The main() function is invoked in the system by default. Hence as soon as the command for execution of the program is used, control directly reaches the main() function.
Q. 7 Define Formal and Actual arguments. Explain with the help of one example.
Ans.
Formal arguments : The list of variable(s) along with their data type(s) given within the brackets with function prototype and function definition are known as formal arguments (or parameters).
Actual arguments : The list of variables without any data type given within the brackets at the time function call (or invoke) is known as actual arguments (or parameters).
Example:
public static void process(int a, float b){
a= a+ 2; // ‘a’ and ‘b’ are formal arguments
b= b* 3;
System.out.println(a + "t" + b);
}
public static void main(){
int x= 2; float y= 4;
process(x, y ); // ’x’ and ‘y’ are actual arguments
}
Q. 8 What is static variable and static method in Java?
Ans. The Static variables are shared by all instances of class. The static variable is also known as class variable and declared using keyword “static”.
The method which begins with static keyword is known as static method. These function uses only static variable(s).
Q. 9 What is the feature of static keyword in connection with function?
Ans. A static function is a function that belongs to a class rather than an instance of a class.
- The static keyword makes a method/ function as a class function ( also known as static method or function),
- They do not require an instance of class to be created.
- It can access only static methods of the class.
Q. 10 What is the difference between function call by value and function call by reference?
Ans.
Call by value | Call by reference |
In this method fundamental or primitive data types are passed (eg. short, int, float, long, char, double etc.). | In this only reference type arguments (object, arrays etc.) or non- primitive data types are passed. |
The changes made in formal arguments (if any) does not reflect to actual arguments | The changes made in formal arguments (if any) are automatically reflected in the actual arguments |
Q. 11 What do you mean by pure function? Give one example.
Ans. The function which does not change or modify the state of the argument/variable that are received when called or invoked it is known as pure function.
Example:
class purefun{ public int show(int x, int y){ //function definition with int x, int y if(x>3) return x; // value x is only checked and not changed else return y; } public void main() //main function invokes pure function show() { int a=4, b=3; //variable initialization int r= show(a,b); System.out.println(r); } }
Q. 12 Define an impure functions?
Ans. Impure Function change the state of the object arguments they have received. The following functions is the example of an impure function:
public static Time increment(Time obj, double secs) { time.seconds+=secs; return(Time); }
Q. 13 What is the difference between formal and actual parameter?
Ans. Actual parameter Formal parameter Parameters which appear in the function call statement are known as actual parameter. Parameters which appear in the function definition statement are known as formal parameter. Example:
simply(125,70,80) // here 125, 70,80 are actual parameters.
Above example values are passed to the parameters given in function definitionExample:
public static void simply(int code, float price)
{
Set of statements;
}
Above example arguments receive actual values from the function call statement.
Q. 14 What is the difference between pure and impure function?
Ans.
Pure Function | Impure Function |
---|---|
It does not modify the arguments | It modifies the state of arguments received or |
Relies only on parameters passed to it. | Refers to variables outside of method. |
e.g. public class TestMax { /* Return the max between two int */ public static int max(int n1, int n2) { …; Return n1; } | e.g. public class TestMax { highestNo = 23; public static int max(int n1, int n2) { … if (n1 < highestNo) result = highestNo; … } |
Q. 15 What is the difference between Local and Instance variables?
Ans. Local Variables Instance Variables Local variables, also called member variables are defined and used only within the scope of the method. Instance variables are defined inside the class and their value is unique to each instance of the class. e.g. void f() {
int i;
i = 1; // OK: in scope }e.g. class Student {
int age;
}
Q. 16 What is the difference between Global and Class variables?
Ans.Global Variables Class Variables public and static class variables are called as Global variables. When a single copy of variable exists irrespective of number of instances of variable, it is called Class Variable e.g. public class mathFunctions{
public static float PI =3.14;
}e.g. class School {
static int totalStudents;
}
Q. 17 What is exit function in Java?
Ans. This function is used to terminate the entire program from any stage and cause the control to got to the last closing brace ‘}’ of the class.
Syntax: System.exit(0);
Q. 18 What is Function Overloading?
Ans. When we define two or more methods within the same class that share the same name, with different parameter declarations, the methods are said to be overloaded, and the process is referred to as method overloading.
Method overloading is one of the ways that Java implements polymorphism.
Overloaded methods must differ in the type and/or number of their parameters. You cannot overload return types
e.g.
public static int max(int num1, int num2) {
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(double num1, double num2) {
if (num1 > num2)
return num1;
else
return num2;
}