FUNCTIONS
FUNCTIONS
1. Can a non-static method/variable be accessed in a static method? If yes, how, if no, why not?
Ans: A non-static member/method can be accessed in a static method by creating an object of class.
2. Differentiate between:
actual and formal parameters
R
call by value and call by reference
static and non-static member functions of a class
O
return and break statement
pure and impure functions
Ans: Notes
PO
3. Define a method int product( int c , int d ) that returns the product of two integers c and d.
Ans:
int product(int c,int d)
{
A
return c*d;
}
4. What type of data can be passed to a function by value?
K
Ans : All primitive data types
5. Can two methods have same name in Java? Write the feature of object oriented programming
that illustrates this concept. T
Ans: Yes. Method overloading(Polymorphism)
6. What will be the output of the following?
JA
class Output
{
void Main()
{
RA
Test();
System.out.println(“GOOD EVENING”);
}
void Test()
{
@
display();
System.out.println(“GOOD AFTERNOON”);
}
A
void display()
{
V
System.out.println(“GOOD MORNING”);
}
public static void main(String args[])
JA
{
Output ob=new Output();
ob.Main();
System.out.println(“GOOD NIGHT”);
}
}
Ans:
GOOD MORNING
GOOD AFTERNOON
GOOD EVENING
GOOD NIGHT
R
7. Complete the following code by writing a single statement so that the method returns the
incremented value of x if x is equal to y, otherwise it returns the decremented value of y.
public class xyz
O
{
public int func(int x, int y)
PO
{
…………….. /* Write single line code here */
}
}
Ans:
A
public class xyz
{
K
public int func(int x, int y)
{
return (x==y?++x:--y); T
}
}
8.Why do we use function/method?
JA
Ans: By using function we can make our work easier by diving our task into number of modules. This process of
dividing a task into various tasks is known as “DIVIDE and CONQUER”.
RA
Points:
1. The functions which are available with programming language and can be used directly are called as Pre-defined
or inbuilt functions.
2. The function which are defined or created by the user to carry out a specific task is called as user-defined function.
@
7.The function which does not change or modify the argument(or parameter) or state of object is known as Pure
JA
function.
8. The function which changes or modify the argument(or parameter) or state of object is known as Pure
function.