KnowledgeBoat Logo

Chapter 6

Working with Methods

Class 9 - Logix Kips ICSE Computer Applications with BlueJ


Multiple Choice Questions

Question 1

A method that does not return a value has a ........... return type.

  1. boolean
  2. class
  3. int
  4. void ✓

Question 2

A method can return ...........

  1. Any number of values
  2. 2 values
  3. Only 1 value ✓
  4. 3 values

Question 3

If a method returns a value, then it must be ...........

  1. The same data type as defined in its prototype ✓
  2. void type
  3. int type
  4. boolean type

Question 4

Parameters in the method definition are called ...........

  1. Formal parameters ✓
  2. Actual 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 scope of a local variable is limited to the ...........

  1. Windows
  2. Multiple programs
  3. Class
  4. 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:

  1. 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.
  2. 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.
  3. Instance Variables (non-static variable) — The member variables that store the state of an object are known as instance variables.
  4. 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:

  1. public static double celsiusToFahrenheit(double celsius)
  2. public static double fahrenheitToCelsius(double fahrenheit)

Use the following formulas for temperature conversion:

  1. fahrenheit = (9.0 / 5) * celsius + 32
  2. 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);
    }
}
Output
BlueJ output of KboatTemperature.java

Video Explanations

PrevNext