Questions on User - defined Methods
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.
Syntax:
2. What is void return type?
Ans. Void is a keyword used in Java to indicate a method does not return any value.
3. Do we need to write return in method body which has a return type of void?
Ans. No. return is optional.
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
5. Give the prototype/method signature of following
a. Function find which receives a sentence sent and word wrd and returns 1 or 0
b. Method isCorrect which receives a character ch and a integer n and returns true
or false
c. Function power that takes in in two integer values and returns result of double
type.
Ans.
a. int find( String sent, String wrd)
1
b. boolean isCorrect(char ch, int n)
c. double power ( int a, int b)
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.
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
}
1. 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
can uses only static variable(s). They cannot use instance variables.
8. 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.
9. What is the difference between function call by value and function call by reference?
Call by value Call by reference
2
In this method fundamental or primitive In this only reference type
data types are passed (eg. short, int, float, arguments (object, arrays etc.) or non-
long, char, double etc.). primitive data types are passed.
The changes made in formal arguments (if The changes made in formal arguments (if
any) does not reflect to actual arguments any) are automatically reflected in the actual
arguments
10. 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);
}
}
11. 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);
}
12. What is the difference between formal and actual parameter?
Ans.
Actual parameter Formal parameter
3
Parameters which appear in the function call Parameters which appear in the function
statement are known as actual parameter. definition statement are known as formal
parameter.
Example: Example:
simply(125,70,80) // here 125, 70,80 are actual public static void simply(int code, float price)
parameters. {
Set of statements;
}
Above example values are passed to the
parameters given in function definition Above example arguments receive actual
values from the function call statement.
13. 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 Refers to variables outside of method.
it.
e.g. e.g.
public class TestMax { public class TestMax {
/* Return the max between two int */ highestNo = 23;
public static int max(int n1, int n2) public static int max(int n1, int n2)
{ …; {
Return n1; …
} if (n1 < highestNo)
result = highestNo;
…}
14. What is the difference between Local and Instance variables?
Ans.
Local Variables Instance Variables
Local variables, also called member Instance variables are defined inside the
variables are defined and used only within class and their value is unique to each
the scope of the method. instance of the class.
e.g. void f() { e.g. class Student {
int i; int age;
i = 1; // OK: in scope } }
4
15. What is the difference between Global and Class variables?
Ans.
Global Variables Class Variables
public and static class variables are called When a single copy of variable exists
as Global variables. irrespective of number of instances of
variable, it is called Class Variable
e.g. public class mathFunctions{ e.g. class School {
public static float PI =3.14; static int totalStudents;
} }
16. 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);
17. 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;
}
5
6