Loading...
Please wait while we prepare your content
Please wait while we prepare your content
Solutions for Computer Applications, Class 9, ICSE
What is a method? Explain the various parts of a method.
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.
How do you define and invoke a method?
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();
}
}
Explain the role of return statement in a method?
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.
What does void signify in the method prototype?
void in method prototype means that the method does not return any value.
Explain the difference between actual and formal parameters with one example of each.
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.
Explain static and non-static methods.
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.
Explain the scope of variables in Java
Scope of the variable determines in which parts of the program the variable is accessible. It is of the following types:
Write a class that contains the following two methods:
Use the following formulas for temperature conversion:
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);
}
}
A method that does not return a value has a ........... return type.
A method can return ...........
If a method returns a value, then it must be ...........
Parameters in the method definition are called ...........
The parameters that are passed to the method when it is invoked are called ...........
The scope of a local variable is limited to the ...........
A method can return more than one value.
False
Methods defined as void must return a value.
False
A method may contain any number of return statements.
True
The non-static methods need an instance to be called.
True
The static methods need an instance to be called.
False
If a method returns a value, then it must be of the same data type as defined in the method prototype.
True
Parameters in the method definition are called dummy parameters.
True
Methods reside in a class in Java.
True
The scope of a local variable is limited to the method or the block it is declared in.
True
The keyword static makes a variable a class variable.
True