Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views7 pages

Methods

The document provides a comprehensive overview of functions and methods in programming, particularly in Java. It covers topics such as the definition of functions, return types, method signatures, static vs non-static methods, and the concepts of pure and impure functions. Additionally, it discusses function overloading, parameter types, and various programming principles related to methods.

Uploaded by

Aishi Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Methods

The document provides a comprehensive overview of functions and methods in programming, particularly in Java. It covers topics such as the definition of functions, return types, method signatures, static vs non-static methods, and the concepts of pure and impure functions. Additionally, it discusses function overloading, parameter types, and various programming principles related to methods.

Uploaded by

Aishi Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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

Q. 2 What is void return type?


Ans. Void is a keyword used in Java to indicate a method does not return any value.

Q. 3 Do we need to write return in method body which has a return type of void?
Ans. No. return is optional.

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

Q. 5 Give the prototype/method signature of following


1. Function find which receives a sentence sent and word wrd and returns 1 or 0
2. Method isCorrect which receives a character ch and a integer n and returns true or false
3. Function power that takes in in two integer values and returns result of double type.
Ans.
1. int find( String sent, String wrd)
2. boolean isCorrect(char ch, int n)
3. double power ( int a, int b)

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

Q. 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
}

Q. 8 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 uses only static
variable(s).

Q. 9 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.

Q. 10 What is the difference between function call by value and function call by reference?
Ans.
Call by value Call by reference
In this method fundamental or primitive data types In this only reference type
are passed (eg. short, int, float, long, char, double arguments (object, arrays etc.) or non-
etc.). primitive data types are passed.
The changes made in formal arguments (if any) The changes made in formal arguments (if any)
does not reflect to actual arguments are automatically reflected in the actual
arguments

Q. 11 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);
}
}

Q. 12 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);
}

Q. 13 What is the difference between formal and actual parameter?

Ans.
Actual parameter Formal parameter
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.

Q. 14 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 it. Refers to variables outside of method.
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;
…}
Q. 15 What is the difference between Local and Instance variables?
Ans.
Local Variables Instance Variables
Local variables, also called member variables are Instance variables are defined inside the class and
defined and used only within the scope of the their value is unique to each instance of the class.
method.
e.g. void f() { e.g. class Student {
int i; int age;
i = 1; // OK: in scope } }
Q. 16 What is the difference between Global and Class variables?
Ans.
Global Variables Class Variables
public and static class variables are When a single copy of variable exists irrespective of number
called as Global variables. 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;
} }

Q. 17 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);

Q. 18 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;
}

Q19. What is a method signature?


Answer
The number and types of list of parameters used in the method prototype is known as method
signature.
Example:int fn(int,int)-fn signature

Fn prototype
Q20
What does void signify in the method prototype?
Answer
void in method prototype means that the method does not return any value.
Q21.Explain static and non-static methods.
Answer
Static methods are created with static keyword in their method prototype as
shown below:
public static return-type method-name(parameter-list)
{
Method-body
}
Static methods can be called directly using class name but they can't access
instance variables and non-static methods of the class.
The non-static methods are created without the static keyword in their method
prototype as shown below:
public return-type method-name(parameter-list)
{
Method-body
}
An instance of the class is required to call non-static methods.

Q22
What happens when an argument is passed by reference?
In pass by reference, the reference of the actual parameter is passed to the
formal parameter. Both actual parameter and formal parameter represent the
same memory location so any changes made to the formal parameters get
reflected in the actual parameters.
Q23
What is an ambiguous invocation? Give an example.
Answer
Sometimes there are two or more possible matches in the invocation of an
overloaded method. In such cases, the compiler cannot determine the most
specific match. This is referred to as ambiguous invocation. It causes a compile
time error. For example, the below program will cause a compile time error due
to ambiguous invocation:
public class AmbiguousDemo {

public static double max(double num1, int num2) {


if (num1 > num2)
return num1;
else
return num2;
}

public static double max(int num1, double num2) {


if (num1 > num2)
return num1;
else
return num2;
}

public static void main(String args[]) {


/*
* This line will cause a compile time error
* as reference to max is ambiguous
*/
System.out.println(max(19, 20));
}
}

Q 24
Which OOP principle implements function (method) overloading?
Answer
Polymorphism
Q25 Diffference between Simple Method and Overloaded method
Answer

Simple Method Overloaded method

Simple Methods have unique In case of Overloaded methods, there are two or more
names. methods with the same name.

We can identify the method being We need to examine the method's number and type of
invoked by looking at its name. parameters to determine which method will be invoked.

You might also like