User-Defined Methods - Questions & Answers
EXERCISEPart A : Objective Type Questions
1. The header is divided into return type, method name and the parameter list and is called as the prototype of the method.
(a) exception list (b) prototype (c) header (d) signature
2. The list of parameters along with their data types is called the method signature.
(a) exception list (b) prototype (c) header (d) signature
3. The void type is used if the method does not return any value.
(a) void (b) null (c) zero (d) leave blank
4. Local variables are those variables that are defined within a method or a block and accessed only within the particular function or block.
(a) Global variables (b) Local variables (c) Instance variables (d) Class variables
5. The parameters passed on the method call are called actual parameters.
(a) actual (b) formal (c) global (d) local
6. Both a and b are those functions that only return the state of an object and does not make any changes to the current state.
(a) Pure functions (b) Accessor functions (c) Both a and b (d) None of these
7. When the parameters passed are of array or class types, it is known as call by reference.
(a) call by value (b) call by reference (c) call by address (d) none of these
8. A function can return maximum one value to the called function.
(a) any (b) one (c) zero (d) none of these
9. When more than one method with the same name exists in the same program with different signatures, this is called method overloading.
(a) underloading (b) overhauling (c) overloading (d) all of these
10. The method call in which the data in actual arguments and formal parameters remains same is known as call by reference.
(a) call by value (b) pure function (c) call by reference (d) impure function
11. Impure methods are also known as Mutator functions.
(a) Accessor (b) Mutator (c) Overloading (d) none of these
12. In Java a class is the basis of all computation.
(a) method (b) constructor (c) class (d) object
13. When primitive data is passed through method call, is known as call by value.
(a) call by value (b) call by reference (c) call by address (d) none of these
Part B : Solved Questions
1. What are two ways of invoking functions?
There are two ways of invoking functions :
Call by Value : When primitive data types are passed as an argument during function call, it is known as call by value. Any change made to the formal parameters will not affect or change the actual parameters.
Call by Reference : When an array or objects are passed as an argument during function call, it is known as call by reference. Any change made to the formal parameters will automatically change the value of the actual parameters.
2. Give the prototype of a function check which receives a character ch and an integer n and returns true or false.
boolean check (char ch, int n)
3. Name the keyword that causes the control to transfer back to the method call.
return
4. What do you understand by the term prototype? What is the need of it?
The header of a method with return type, method name and the parameter list is called the prototype of the method. Using the method prototype, the compiler checks the method during the invocation and compilation, whether the number and type of parameters are proper or not. If the number of parameters are not proper, an error message will be displayed by the compiler.
5. Define the terms pure and impure functions.
Pure functions are those functions that only return the state of an object and does not make any changes to the current state. They are also called Accessor Methods.
Impure functions are those functions that change the value of one or more fields or data members each time they are called. They are also called Mutator Methods.
6. Define the term function overloading. Give a program with functions to calculate area to explain it.
When more than one function with the same name exist in the same class with different signatures, this is called function overloading.
Example:
// Program to demonstrate Function overloading.
public class AreaCalculator
{
float CalculateArea(float r)
// area of circle
{
System.out.print("Area of Circle - ");
float ar=22/7*r*r;
return ar;
}
int CalculateArea (int l, int b)
//area of rectangle
{
System.out.print("Area of Rectangle - ");
return l*b;
}
}
(i)
(a) What is the return type of method test()
(b) What will be the output if n = 12 is given.
(a) void
(b) 1 2 4 6 12
(ii) public class test
{
public boolean result (int x)
{
if (x >= 0)
return true;
else
return false;
}
}
What will be the method result return, if
(a) x = -10
(b) x = +10
(c) return type of method
(d) signature type
(a) false
(b) true
(c) boolean
(d) one variable of type int
(iii) public class Demo
{
int a, b;
Demo()
{
a = 0;
b = 0;
}
public int compare (int p, int q)
{
a = p;
b = q;
if (a>b)
return a;
else
return b;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int x, y, max;
System.out.println("enter x");
x = sc.nextInt(); // say x = 51
System.out.println("enter y");
y = sc.nextInt(); // say y = 50
Demo ob1 = new Demo();
max = ob1.compare(x, y);
System.out.println("Maximum number is = " + max);
}
}
(a) The Actual Arguments :
(b) The Formal Parameters :
(c) Return type of compare() :
(d) Method calling statement :
(e) Output : Maximum number is :
(a) x, y
(b) p, q
(c) int
(d) max = ob1.compare(x, y);
(e) 51
Quick Navigation:
Quick Review Flashcards - Click to flip and test your knowledge!
Question
In Java programming, what is a 'method'?
Answer
A set of statements that can perform a specific task and be used multiple times.
Question
How do methods help in 'reducing complexity' of code?
Answer
They divide a complex problem into smaller, manageable parts that can be called when required.
Question
The ability to define a method once and call it repeatedly anywhere in a program is known as code _____.
Answer
reusability
Question
Why are methods often compared to 'black boxes'?
Answer
The user only needs to know what inputs to pass and what output to expect, without knowing the internal logic.
Question
What happens if a defined method is never called in the program?
Answer
It will never be executed.
Question
What are the two primary parts of a method definition?
Answer
The header (prototype) and the body.
Question
Which method component determines the type of access (e.g., public, private) to the function?
Answer
Access specifier
Question
In a method header, which keyword is used if the method does not return any value?
Answer
void
Question
What is the function of 'parameters' in a method definition?
Answer
They allow the method to receive data (arguments) when it is called.
Question
The block of statements executed when a method is called is surrounded by which symbols?
Answer
Curly braces \{ \}
Question
According to Java naming conventions, what part of speech should a method name typically be?
Answer
A verb
Question
In multi-word method names like 'readInfo', what is the casing convention for the first letter of the second word?
Answer
Upper case
Question
What is the definition of a 'method prototype'?
Answer
The first line of a method that tells the compiler the number and type of parameters and the return type.
Question
How does the 'method signature' differ from the 'method prototype'?
Answer
The signature refers specifically to the list of parameters along with their data types.
Question
What is 'method invocation'?
Answer
The process of calling a defined method for execution.
Question
Static methods are also known as _____ methods.
Answer
Class
Question
How can a static method be invoked directly within its own class?
Answer
By using just the name of the method followed by parentheses.
Question
What must be created before a non-static method can be invoked?
Answer
An instance (object) of the class.
Question
Non-static methods are also commonly referred to as _____ methods.
Answer
Instance
Question
Which operator is used to call a non-static method through an object reference?
Answer
The dot (.) operator
Question
What is the difference between 'Actual Parameters' and 'Formal Parameters'?
Answer
Actual parameters appear in the method call, while formal parameters appear in the method definition.
Question
In what order are values from actual parameters passed to formal parameters?
Answer
Sequentially from left to right.
Question
What is the 'lifetime' of a formal parameter?
Answer
It only exists until the method executes once completely.
Question
In 'Call by Value', what is copied to the formal parameters?
Answer
A copy of the value of the actual parameters.
Question
How does a change to a formal parameter in 'Call by Value' affect the actual parameter?
Answer
It has no effect on the actual parameter.
Question
What type of data is typically passed using the 'Call by Value' mechanism?
Answer
Primitive data types
Question
In 'Call by Reference', what information about the variable is passed to the method?
Answer
The memory location (reference) of the original variable.
Question
How does a change to a formal parameter in 'Call by Reference' affect the actual parameter?
Answer
The change is automatically reflected in the actual parameter because they share the same memory location.
Question
Which data types require the 'Call by Reference' mechanism?
Answer
Object or array variables
Question
Which keyword is used to send a value back to the calling method?
Answer
return
Question
How many values can a Java method return at a time?
Answer
Exactly one
Question
What happens to method execution immediately after a 'return' statement is encountered?
Answer
The method terminates and control returns to the caller.
Question
What defines a 'Pure Method' (Accessor Method)?
Answer
A method that returns a value based on arguments without changing the state of an object.
Question
What defines an 'Impure Method' (Mutator Method)?
Answer
A method that causes a change in the state of an object, such as modifying instance variables.
Question
What is 'Method Overloading'?
Answer
The ability to have multiple methods with the same name but different signatures within the same class.
Question
What specific part of a method must be different for overloading to be valid?
Answer
The parameter types or the number of parameters (the signature).
Question
Under which type of polymorphism is method overloading categorised?
Answer
Static polymorphism (or early binding).
Question
If two methods have the same name and parameter list but different return types, are they considered overloaded?
Answer
No, they must have different signatures.
Question
What is the purpose of using suffixes like 'L' in values like '25423L' during method calls?
Answer
To differentiate between data types (e.g., long vs int) to help the compiler select the correct overloaded method.
Question
The first line of a method that tells the compiler about the method's name, return type, and parameters is called the _____.
Answer
header
Question
Variables defined within a method or block that are only accessible within that block are _____ variables.
Answer
local
Question
Which modifier is used to define a method that belongs to the class itself rather than a specific object?
Answer
static
Question
A method used only to retrieve or calculate a value without altering any class data is a/an _____ function.
Answer
Accessor (or Pure)
Question
What is the mathematical formula for a 'Factorion' (Special Number) as described in the source material?
Answer
A natural number that equals the sum of the factorials of its digits.
Question
An 'Automorphic Number' is defined as a number whose _____ ends in the same digits as the number itself.
Answer
square
Question
A 'BUZZ number' is any number that is either divisible by 7 or ends with which digit?
Answer
7
Question
A 'Pronic number' is defined as the product of two _____ integers.
Answer
consecutive
Question
In Java, what is considered the basis of all computation?
Answer
The class
Question
Which Java library class contains pre-defined methods like 'sqrt()' and 'pow()'?
Answer
The Math class
Question
What error message is displayed if a method call does not match its prototype?
Answer
Compile-time error (or parameters are not proper).
Question
If a method is defined as 'private int findCube(int x)', what is its signature?
Answer
(int x)
Question
What is the return type of the method call 'Math.pow(2, 3)'?
Answer
double
Question
What is the next action taken by the program after a method call completes its execution?
Answer
Control returns to the statement immediately following the method call.
Question
In the method header 'public static void main(String args[])', which part is the exception list?
Answer
It is not present (it is optional).
Question
In the example 'double x = Math.sqrt(y)', what is 'y' in relation to the method call?
Answer
The argument (or actual parameter).