Functions and Methods, JAVA
Functions and Methods, JAVA
What is a function?
Ans A function is a program module used simultaneously at different instances in the
program
How are the following passed -a. primitive types b.reference types
Ans: Primitive types by value and reference types by reference
What is a return
statement?
Ans: Areturn statement whích sehds back the value from a method to a caller program s
known as.tetuirn stategrent. Afeturnstatement is used to terminaterthe execution of a
method
Amethod withvoid retuirtype' may onily have return statement ofthe following form
return;
A method with any other return type must have at least one return statement of the form
returnexpression
What are the two functions performed by the return statement?
Ans: The two functions performed by the return statement are
a. It passes the control of execution to the calling function.
b. t returns a value to the calling function.
How would you define a function/ method?
Ans: A method is defined as follows:
<Access specifier><specific modifier><return type><Method name> (Parameter list)
Where
Access Specifier is Public or Private. A method declared without Access
Specifier is by
default treated as Public
Type specifies the data type of the value to be returned from the method.
Function name or Method name may be any meaningful name related to the method
Parameter list are the variables that receive the values passed from the arguments duri.
Method call
Syntax of function
w.t SAAAm whww
prototype
Access
ist
Specifiersemodifier Sreturndatatype tuNction_name ( parameter
mbnc WA M t n ,0
What is the function of
the. (dot) operator?
Ans The (oot) operator (a.k.a. member operator) int BOnm (>tmin Antr
enables us to access instance variables of any objects within a
class/
helps to store values of instance variables of an object
s used to call object methods.
What is a function
prototype?
Ans: A function prototype is the first line of the function definitiogglt refep[ to the return Hhpalh
type, function name and the parameter list .
It helps the compiler to identify which function is called, what type of value is to be
returned and what parameters are being sent.
function'sprototype
Why is function signature s0, important
Ans: A function
signature indicatésthe number and the types of arguments passecd as
parameters in the functionprototype. It is important in function overloading where tws o
more functions have
tHesaméname but perform different operations. Function overloaling
is possiblewith the help ofgjfferent signatures.
14 Write the advantagesof using functions in a program? I0
Ans: The advantages of using functions in a program are :
a
Functions lessen the complexity of the programs.
b. Theyenhance the reusability of code.
cThey hide the implementation details.
What
Ans: Pass
is Pass by Value orCalby Válue itn muran)
by value is
processof passing a copy of the actual parameters to the formal
a
parameters. Any change made in the formal parameter does not reflect in the actual
parameter.
What
Ans: Pass
is
Pass byReference or Call by Reference? ( On
BYreference is a process of passing the reference (Address) of actual argument
to the
formal parameters. It works with the original copy of the actual parameters.
Thereforéany change made in the formal parameters will be reflected on the actual
arguments. Arrays and objects are passed by reference.
24 What is the difference between pass by value and pass by reference? J9, d|2
Ans:Call by value PAnn hm Vame
a Inthis the method creates its new set of variables
(formal parameters) to copy the
value of the actual parameters and works with them
b Any change in the formal parameter is not reflected in the actual parameter
CPrimitive data types are passed by call by value.
ttti
Call by reference
a Here the actual parameters are passed onto the method and new set
not created Here the formal
of variahia.
parameters refer to the memory location of thea a
parameter.
b Any change in the formal parameter is reflected in the actual parameter.
Reference types (objets,arrays)are passed by call by reference, i.e. the
8Iven class type
objects
are passed as parameters.
1 What is the statement
specifically called that invokes a function called?
Ans Function call
statement
What is the condition'of using a function in an expression ?
Ans If a function s used,in anexpression, it muatreturna value.
What is the principle reason for
Ans The principle reason for
of actual parameters, even
passing
passing
arguments iay valyeG
arguments byWalue'is that theydo hofalter the value
though there may be a change in the fórmal parameters.
Jit itidt
What is side effect?
Ans: When object passed
are as references, any changeintheformal parameters is
reflected in the actual parameters. This is
known as side éfiect.
What function
is
overloading?
Ans: Function Overloading is the proçess af défining a number
of functions or methods
having the same function names buttdifferént dumber and types of parameters.
Polymorphism in Java is achieved thiough function overloading
e.g
add (int a, int b),
add (float a, int b
add(int aint b, intcl
Knw
8 What is the need for FUnction
overloading? t
Ans Function
oyer16ading is needed in a program Because objects behave differently at
different situatións With the help of function
overlading we can design the structure of a
program such a way that whatever may be the pbssible requirement of a user he may
use
the'samejunction to extract the output.
Function.overloading helps in implementing polymorphism by reducing the number of
Comparisons in a program and thereby makes the program run faster.
What factors make two definitions within the same function name significantly different?
Ans The number and type of
parameters make two definitions with the same function
name significantly different.
The string object being reference types are passed by references. But, changes if any are
not reflected back to them. Why?
Ans: The String objects are immutable in Java, which means once they are created, they
cannot be changed That is why, even though
Strings are passed by reference, they cannt
be changed
19
Here thefirst line is the header, which contains the return type, Method name and thet
parameter list
The function body contains a set of statements for the related operation. The statemens
are enclosed within a pair of braces called a block.
The type of header indicated here which does not pass any value is also known as Methad
Signature.
functions?
30
30) How is polymorphism implemented through overloading of
Ans Polymorphism means the ability of an object to behave differently in dif fer
Circumstances. Function overloading means a function having several definitions th
compiler differentiates by the name or the types of arguments. 1.e. signature. Thais mo
ne
the
the same functignnamebehaves differently. depending on the circumstan circumstan
(Signature).Hence oNerloadihg is one of the means of implementing polymorphism.
variable
Static Variable can be accessed without creating an object. It can beaccéssed with the he
of a class name and dot operator in another class. Hence it iscalled a Class variable.
-Static variàble is created when the class is first referred
Static variable is destroyed when the program is
to
oyer. Qpunguay
Any changes made in the value of static variableís visjble fo allthe objects of the given clas
InstanceVariable
- Instance variable's value is not shared by.differept ohjécts formed fromthe same class.
Instance variable can be accessed only withethe help of objects(instance) and using to
operator. Thus it is called instance variabJe.
Instance variable is created when an objectof the class is created.
Instance yariable is destroyed when the bject is destroyed.
Changesimadein he Instnge varjabledoes not effect the same variable belonging to a
1
different object.