3.
double type
4. boolean type
Question 4
Parameters in the method definition are
called ...........
1. actual parameters
2. formal parameters ✓
3. informal parameters
4. void parameters
Question 5
The parameters that are passed to the
method when it is invoked are called
...........
1. formal parameters
2. actual parameters ✓
3. informal parameters
4. void parameters
Question 6
The method that changes the state of an
object is known as ...........
1. pure method
2. impure method ✓
3. perfect method
4. imperfect method
Question 7
The scope of a local variable is limited to
the ...........
1. Windows
2. Multiple programs
3. Class
4. Method or block it is declared in ✓
Question 8
The technique in which the change in the
formal parameter gets reflected in the
actual parameter is known as ...........
1. call by reference ✓
2. call by value
3. call by argument
4. call by method
Question 9
In which technique are the values of actual
parameters copied to the formal
parameters?
1. call by reference
2. call by value ✓
3. call by argument
4. call by method
Question 10
A method with many definitions is known
as ...........
1. many method
2. multiple method
3. void method
4. overloaded method ✓
State whether the given statements
are True or False
Question 1
A method may contain any number of
return statements.
True
Question 2
The non-static methods need an instance
to be called.
True
Question 3
A method can return more than one value.
False
Question 4
Methods defined as void must return a
value.
False
Question 5
The static methods need an instance to be
called.
False
Question 6
In Java, all primitive types are passed by
value and all reference types are passed
by reference.
True
Question 7
You can place the return statement in a
void method without any expression.
True
Question 8
If a method returns a value, then it must
be of the same data type as defined in the
method prototype.
True
Question 9
Parameters in the method definition are
called dummy parameters.
True
Question 10
Methods reside in a class in Java.
True
Question 11
Method overloading is one of the ways by
which Java implements polymorphism.
True
Question 12
The scope of a local variable is limited to
the method or the block it is declared in.
True
Question 13
The keyword static makes a method a
class method.
True
Question 14
An impure method always returns the
same value when the same arguments are
given.
False
Assignment Questions
Question 1
What is a method? Explain the various
parts of a method.
Answer
A method is a named block of code within
a class. It executes a defined set of
instructions when called from another part
of the program. The different parts of the
method are access-modifier, type,
method-name, parameter-list and
method-body.
Question 2
What is a method signature?
Answer
The method name along with the list of
parameters used in the method prototype
is known as method signature.
Question 3
How do you define and invoke a method?
Answer
The syntax of a method definition is:
[access-modifier] type method-name
(parameter-list)
{
method-body;
}
To invoke a method, write the name of the
method and specify the value of
arguments of its parameter list. Below
example shows the definition and
invocation of a method:
public class DisplayMessageDemo {
public void DisplayMessage() {
System.out.println("Hello World!"
}
public void MyMessage() {
DisplayMessage();
}
}
Question 4
What does the return statement do in a
method?
Answer
Return statement sends back the value
given to it from the called method to the
caller method. It also transfers the
program control back to the caller method
from the called method.
Question 5
What does void signify in the method
prototype?
Answer
void in method prototype means that the
method does not return any value.
Question 6
Explain the difference between actual and
formal parameters.
Answer
The parameters that appear in the method
definition are called formal or dummy
parameters whereas the parameters that
appear in the method call are called actual
parameters.
Question 7
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(par
{
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‐
{
Method‐body
}
An instance of the class is required to call
non-static methods.
Question 8
What happens when an argument is
passed by reference?
Answer
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.
Question 9
When a method has been declared more
than once in a class, how does Java
determine the overloading?
Answer
Overloading is determined based on the
number and type of parameters of the
method.
Question 10
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,
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(int num1, do
if (num1 > num2)
return num1;
else
return num2;
}
public static void main(String args[]
/*
* This line will cause a compile
* as reference to max is ambiguo
*/
System.out.println(max(19, 20));
}
}
Question 11
Given below are the two method
definitions:
1. public static double Check(double x,
double y)
2. public static double Check(int x,
double y)
Which of the two methods is invoked for
the following?
1. double z = Check (6, 5);
2. double z = Check (5.5, 7.4);
Answer
1. double z = Check (6, 5); invokes
public static double Check(int x,
double y)
2. double z = Check (5.5, 7.4); invokes
public static double Check(double x,
double y)
Question 12
What is the signature of the following
method heading?
public void CoolMethod(int xx, char yy, int
zz)
Answer
Signature of this method is:
CoolMethod(int xx, char yy, int zz)
Question 13
Which OOP principle implements function
(method) overloading?
Answer
Polymorphism
Question 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.
Answer
public class KboatPalindromicPrime
{
public static boolean isPrime(int n)
int c = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}
boolean prime = c == 2;
return prime;
}
public static boolean isPalindrome(in
int num = n, revNum = 0;
while (num != 0) {
int digit = num % 10;
num /= 10;
revNum = revNum * 10 + digit;
}
boolean palin = revNum == n;
return palin;
}
public static void main(String args[]
int x = 1, count = 0;
while (count < 100) {
if (isPrime(x) && isPalindrom
count++;
System.out.println(x);
}
x++;
}
}
}
Output
Question 15
Differentiate between the following:
1. Call by value and Call by reference
Answer
Call by
Call by value
reference
Reference of
Values of actual
actual
parameters are
parameters is
copied to formal
passed to formal
parameters.
parameters.
Changes made to Changes made to
formal formal
parameters are parameters are
not reflected back reflected back to
to actual actual
Call by value Call by reference
parameters. parameters.
2. Pure and Impure methods
Answer
Impure
Pure methods
methods
Pure methods take Impure
objects and/or methods
primitive data types change the
as arguments but state of
does not modify the received
objects. objects.
Pure methods Impure
doesn't have side methods have
effects. side effects.
3. Simple Method and Overloaded
method
Answer
Simple
Overloaded method
Method
Simple In case of Overloaded
Methods have methods, there are
unique two or more methods
names. with the same name.
We can We need to examine
identify the the method's number
Simple
Overloaded method
Method
and type of
method being
parameters to
invoked by
determine which
looking at its
method will be
name.
invoked.
Video Explanations