Multiple Choice Questions
Question 1
A method that does not return a value has a ........... return type.
- boolean
- class
- int
- void ✓
Question 2
A method can return ...........
- Any number of values
- 2 values
- Only 1 value ✓
- 3 values
Question 3
If a method returns a value, then it must be ...........
- The same data type as defined in its prototype ✓
- void type
- int type
- boolean type
Question 4
Parameters in the method definition are called ...........
- Formal parameters ✓
- Actual parameters
- Informal parameters
- void parameters
Question 5
The parameters that are passed to the method when it is invoked are called ...........
- Formal parameters
- Actual parameters ✓
- Informal parameters
- void parameters
Question 6
The scope of a local variable is limited to the ...........
- Windows
- Multiple programs
- Class
- Method or block it is declared in ✓
State whether the given statements are True or False
Question 1
A method can return more than one value.
False
Question 2
Methods defined as void must return a value.
False
Question 3
A method may contain any number of return statements.
True
Question 4
The non-static methods need an instance to be called.
True
Question 5
The static methods need an instance to be called.
False
Question 6
If a method returns a value, then it must be of the same data type as defined in the method prototype.
True
Question 7
Parameters in the method definition are called dummy parameters.
True
Question 8
Methods reside in a class in Java.
True
Question 9
The scope of a local variable is limited to the method or the block it is declared in.
True
Question 10
The keyword static makes a variable a class variable.
True
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
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 3
Explain the role of return statement 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 4
What does void signify in the method prototype?
Answer
void in method prototype means that the method does not return any value.
Question 5
Explain the difference between actual and formal parameters with one example of each.
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. Example:
public class FindSquareDemo {
public int FindSquare(int x) {
int result;
result = x * x;
return result;
}
public void MyFindSquare() {
int num = 4;
int myResult;
myResult = FindSquare(num);
System.out.println("Square of " + num + " is " + myResult);
}
}
Here, variable x in the definition of FindSquare is formal parameter and variable num in the method call of FindSquare is actual parameter.
Question 6
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.
Question 7
Explain the scope of variables in Java
Answer
Scope of the variable determines in which parts of the program the variable is accessible. It is of the following types:
- Local Variables — The variables declared inside a method or block are called local variables. The scope of local variables is limited to the method or the block they are declared in.
- Parameter Variables — The variables used as arguments in the method prototype are called parameter variables. Their scope is limited to the method where they are being used.
- Instance Variables (non-static variable) — The member variables that store the state of an object are known as instance variables.
- Class Variables (static variable) — A variable declared in a class with the static modifier is called a class variable.
Question 8
Write a class that contains the following two methods:
- public static double celsiusToFahrenheit(double celsius)
- public static double fahrenheitToCelsius(double fahrenheit)
Use the following formulas for temperature conversion:
- fahrenheit = (9.0 / 5) * celsius + 32
- celsius = (5.0 / 9) * (fahrenheit - 32)
Finally, invoke these methods in BlueJ to convert some test values.
public class KboatTemperature
{
public static double celsiusToFahrenheit(double celsius) {
double f = (9.0 / 5) * celsius + 32;
return f;
}
public static double fahrenheitToCelsius(double fahrenheit) {
double c = (5.0 / 9) * (fahrenheit - 32);
return c;
}
public static void main(String args[]) {
double r = celsiusToFahrenheit(100.5);
System.out.println("100.5 degree celsius in fahrenheit = " + r);
r = fahrenheitToCelsius(98.6);
System.out.println("98.6 degree fahrenheit in celsius = " + r);
}
}