0% found this document useful (0 votes)
14 views2 pages

FUNCTIONS

The document discusses various concepts related to functions/methods in Java including: accessing non-static members from static methods; differences between actual/formal parameters, call by value/reference; static/non-static member functions; return/break statements; pure/impure functions; defining a method to return the product of two integers; data types that can be passed by value; method overloading; output of a code sample; completing code to return incremented/decremented value based on a condition; and reasons for using functions/methods.

Uploaded by

bhaktikhanijo16
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)
14 views2 pages

FUNCTIONS

The document discusses various concepts related to functions/methods in Java including: accessing non-static members from static methods; differences between actual/formal parameters, call by value/reference; static/non-static member functions; return/break statements; pure/impure functions; defining a method to return the product of two integers; data types that can be passed by value; method overloading; output of a code sample; completing code to return incremented/decremented value based on a condition; and reasons for using functions/methods.

Uploaded by

bhaktikhanijo16
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/ 2

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.
@

3. void is keyword which specifies that function is not returning ay value.

4. A function can return only one value at a time.


A

5. A function can return either primitive data types(byte,short,int,long,float,double,boolean,char) or reference


type(object of class).
V

6. The function overloading is a way to implement POLYMORPHISM.

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.

You might also like