0% found this document useful (0 votes)
40 views4 pages

CHAPTER5

The document discusses user defined methods in Java. It provides answers to 15 questions about methods, including what a method is, method signatures, defining and invoking methods, return statements, static and non-static methods, call by value vs reference, method overloading, and differentiating pure vs impure methods and simple vs overloaded methods.

Uploaded by

Mukesh
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)
40 views4 pages

CHAPTER5

The document discusses user defined methods in Java. It provides answers to 15 questions about methods, including what a method is, method signatures, defining and invoking methods, return statements, static and non-static methods, call by value vs reference, method overloading, and differentiating pure vs impure methods and simple vs overloaded methods.

Uploaded by

Mukesh
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/ 4

CHAPTER 5

User defined Methods


Section 3: Assignment Questions

1. What is a method? Explain the various parts of a method.


Ans. A method is named block of code within a class. It executes a defined set of instructions when
called from another part of the program.
Let us understand the various parts of a method with a example:
public void HelloMessage()
{
System.out.println ("Hello JavaProgrammer!");
}
public is a modifier.
void is the return type.
HelloMessage() is the method name.

2. What is a method signature?


Ans. The method name along with the list of parameters used in the method prototype is known as
method signature. It is a part of the method declaration.
For example:
public void setMapReference(int xCoordinate, int yCoordinate)
{
//method code
}
The method signature in the above set of statements is setMapReference(int, int).

3. How do you define and invoke a method?


Ans. Methods are defined inside the class.
The syntax of a method definition is:
[access-modifier] type method-name (parameter-list)
{
Method-body;
}
The method is invoked by calling the method. For eg. Displaymessage(); www.bhuvantechs.com

4. What does the return statement do in a method?


Ans. (i) It returns a value to the calling statement.
(ii) It causes an exit from the method and transfers the control back to the calling method.

5. What does void signify in the method prototype?


Ans. A void type means that the method does not return a value. This ensures that the method cannot
be used in an assignment statement. Only methods that return a value can be used in expression and
assignment statements.
6. Explain the difference between actual and formal parameters.
Ans. The parameters that appear in method definition are called formal parameter whereas the
parameters that appear in the method invocation are called actual parameters.
For example:
public int square(int x)
{
int result;
result=x*x;
return result;
}
public void square()
{
int num=4;
int myresult;
myresult=square(num);
}
In above example x is a formal parameter and num is a actual parameter.

7. Explain static and non-static methods.


Ans. Non static – An instance is required to call the non static method. It cannot be called directly.
Static – The static method does not need an instance to be called. It can be called directly using the class
name.

8. What happens when an argument is passed by reference?


Ans. In the pass by reference technique, the reference of the actual parameter is passed to the formal
parameter.

9. When a method has been declared more than once in a class, how does Java determine the
overloading?
Ans. When a method has been declared more than once in a class, then java puts different signatures to
determine overloading.

10. What is an ambiguous invocation? Give an example.


Ans. 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 an ambiguous
invocation. The ambiguous invocation causes a compile-time error.

www.bhuvantechs.com

User defined Methods ~2~


11. Given below are the two method definitions:
i. public static double Check(double x, double y)
ii. public static double Check(int x, double y)

Which of the two methods is invoked for the following?


i. double z = Check (6, 5);
Ans. public static double Check(int x, double y)

ii. double z = Check (5.5, 7.4);


Ans. public static double Check(double x, double y)

12. What is the signature of the following method heading?


public void CoolMethod(int xx, char yy, int zz)
Ans. The signature of the following method is CoolMethod(int xx, char yy, int zz)

13. Which OOP principle implements function (method) overloading? [ICSE-2007]


Ans. Polymorphism

14. A palindromic prime is a prime number and also palindromic. For example, 131, 313, and 757 are
prime numbers and also palindromic prime numbers. Write a program that displays the first 100
palindromic prime numbers.
Ans. import java.util.*;
public class PalindromicPrime
{
public static boolean Prime(int num)
{
int count = 0;
for(int divisor=2;divisor<=num/2;divisor++)
{
if(num%divisor==0)
{
return false; // if the number is not prime return false.
}
}
return true; // if the number is prime return true.
}
public static boolean Palindromic(int num)
{
int result = 0;
www.bhuvantechs.com

int number = num;


while(num!=0)
{
int lastDigit= num%10;
result = result*10+lastDigit;
num /=10;
} // End of while num!=0
if(number==result)
{

User defined Methods ~3~


return true; // if the number is palindromic return true.
}
return false; // if the number is not palindromic return false.
}
public static void main(String[] args)
{
int count=1,number=2;
String result="";
while(count<=100)
{
if(Prime(number) && Palindromic(number)){ // is the number palindromic prime?
if(count%10==0){
result += " "+number+"\n";
}else{
result += " "+number;
}
count++;
}
number++;
}
System.out.print(result); // Print the first palindromic prime numbers.
} // End of main method.
}

15. Differentiate between the following:


i. Call by value and Call by reference
Ans. Call by value method copies the value of an argument into the formal parameter of that function.
Call by reference method copies the address of an argument into the formal parameter. In call by value
method, a copy of the variable is passed whereas, In call by reference method, a variable itself is passed.

ii. Pure and Impure methods


Ans. A method is considered pure if:
a) It does not change the original state of an object.
b) It always returns the same value when the same arguments are given.

Pure method generally returns a value to its calling method.


A method is considered impure if:
a) It does change the original state of an object.
b) Every time it is called, it does not return the same value when the same arguments are given.
www.bhuvantechs.com

Invoking an impure method causes side effects. Impure methods may or may not return a value.

iii. Simple Method and Overloaded method


Ans. A program is a set of instructions given to the computer to solve a particular problem. These
instructions are specified in the form of simple methods in Java. It is possible in Java to define two or
more methods within the same class that have the same name, as long as their parameter declarations
are different. When this is the case, the methods are said to be overloaded, and the process is referred
to as method overloading.

User defined Methods ~4~

You might also like