0% found this document useful (0 votes)
23 views

Functions and Methods, JAVA

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Functions and Methods, JAVA

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNCTIONS AND METHODS

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 are the three types of functions in Java?


Ans: Computational functions- The functions that calculate or compute someyalde.ane
returm the computed value. Eg. Math.sqrt (),
etc.
Manipulative functions - The functions that manipulate information andetuma sucte s
or failurecode.
Procedural functions The functions that perform an action and havenoexpliit return
value.e.g.5ystem.out.printin().
What is the function ofvoid in a method? O T
Ans: When the word void is used, it means that the function itis associated with does not
return a value. AnyNOid function has no value and cannot be used like a variable like
function that returns a value.
hm o m
What is a function terminator?
Ans: Generally the return statement is used atthe end of a function to send back a valu:
to its caller program. Thus it is also calleg afunction terminator.

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)

Body of the method;

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.

What is a function signature?


ti yunah.
Ans A function signature basically refers to thenumbeand types of arguments in the
parameter list.
It is a part of the function prototype.A function signature and the return type make a

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 are function parameters?


Ans: Parameters to a function are the variables along with function name under
parentheses whichjucentain the value passed from a caller function to be further
manipulated in the fuhction body.

16 What points to be remembered while retunning a function?


are the
Ans: following points are to be taken into accoint while returning a function:
The
The return of the function
type must be compatible to the value returned from the
function
The return type should be int incase anint value is passed. If the function does not retuin
any value then the return type used before the function name must be 'void'.
18
At the most how many values
can a function return at a time?
Ans:One

Differentiate between actual and formal


Ans The parameters?
parameters that appear in the function definition
are called formal
whereas the parameters that parameter
appear in a function call statement are called actual
parameters. The formal parameters are preceded by a data type whereas actual
parameters are not preceded by a data type.
What is the difference between a
parameter and an
argument?
Ans: Parameter is a variable used with method
function call. They are called formal
signature, which receives a value duripg
parameters whereas values that are passedto the
method during its call from the caller method are
called arguments or
actyaparaíeters
20 State the difference between actual parameters and formal
Ans:Actual parameters parametefs
a. The parameters that appear in the function call
statément áre called actal
parameters.
b. These parameters are used to pas the yalues
Formal parameters
Functions which change the state ,
of the argumients received are called impure
functions.
The result depends the
on
modifcatjonsof the árguments received.
21
How are functions invoked? 20IA (2)
Ans:A function is invoked by a. Call-by veJue
l
b . Call-by reference.
All primitive type of data are passed Bv.yalúe and all reference type data (objects, arrays
are passed by reference.

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

What is static binding?


Ans: During function overloading the compiler at compile-time selects the appropriate
function for a call at the compile-time itself This is known as early binding or static
bind1ng

What is dynamic binding?


Ans. The process of linking a function call with the function signature at runtime, i.e. dur ng
the execution of a program is called dynamic binding.

23 What is a recursive function?


Ans: A function designed in such a way that it calls itself in its body till the giverkcogditijon is
true is called Recursive function.

What is a pure function?


Ans: A pure function is one that returns a value to its caller module. Iisalsotálled
accessor method as it does not change the state of an object.

What is an impure function?


Ans: An impure function may not return a value. It basically.chapges the state of an object
eachtime itiscalled. Thus we can say that a method thateiangs.the state of an object is
imeure method or Mutator.
Statethedifference between pure and impure functiop[?
Ans: Pure functions
a. A pure function does not changeormodify the state of their parameters received.
b A pure functions returns a value
Impure functions
Functions which may gr may not change the state of their argument received are
called as impure
functions
An impure funtiomay ofmay not returna a

27 a t are the components of a method?


= A n s : A methodcontáins a header and a bod
publicinfdemó ()
eturn (value);

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.

Note Only functions returning a value can be used in expressions.

What isthe use of 'this' keyword? 1h


auny
Ans: 'this' keyword is used inside a method to refer to currentobject.'t is always a
reference to the object on which the method was invoked.

anun itn imaln )


29 What does the static keyword in a function indicate?
function or method to be called without creatin
Ans The static keyword enables a g an
function is not static then object has to be created to access it.
object If a

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.

31 Differentiate between.statid aable and Instandely


Staticvariable (Class Variable)
Static variable's value is common for all objects created from the same clas. The value
the static variable is shared by all the objects formed from the
clags whichas sta a

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.

What is message passing among objects


Ans. The interacation betwéen objects, by means function calls is termed as message
passing among pbjects. For instance a bank customér object will want to interact with the
account object for yiewing the balance amount and the two will need to interact.

You might also like