Working with Methods

ICSE Class 9 Computer Applications · Solutions for Class 9 ICSE Logix Kips Computer Applications with BlueJ Java · 23 solved questions

Exercise
Marks
11 Mark · Multiple Choice Questions

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

Show SolutionHide Solution↓
  • void ✓
  • 21 Mark · Multiple Choice Questions

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

    Show SolutionHide Solution↓
  • Only 1 value ✓
  • 31 Mark · Multiple Choice Questions

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

    Show SolutionHide Solution↓
  • The same data type as defined in its prototype ✓
  • 41 Mark · Multiple Choice Questions

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

    Show SolutionHide Solution↓
  • Formal parameters ✓
  • 51 Mark · Multiple Choice Questions

    The parameters that are passed to the method when it is invoked are called ...........

    Show SolutionHide Solution↓
  • Actual parameters ✓
  • 61 Mark · Multiple Choice Questions

    The scope of a local variable is limited to the ...........

    Show SolutionHide Solution↓
  • Method or block it is declared in ✓
  • 1State whether the given statements are True or…

    A method can return more than one value.
    False

    Solution not available in the source paper.

    2State whether the given statements are True or…

    Methods defined as void must return a value.
    False

    Solution not available in the source paper.

    3State whether the given statements are True or…

    A method may contain any number of return statements.
    True

    Solution not available in the source paper.

    4State whether the given statements are True or…

    The non-static methods need an instance to be called.
    True

    Solution not available in the source paper.

    5State whether the given statements are True or…

    The static methods need an instance to be called.
    False

    Solution not available in the source paper.

    6State whether the given statements are True or…

    If a method returns a value, then it must be of the same data type as defined in the method prototype.
    True

    Solution not available in the source paper.

    7State whether the given statements are True or…

    Parameters in the method definition are called dummy parameters.
    True

    Solution not available in the source paper.

    8State whether the given statements are True or…

    Methods reside in a class in Java.
    True

    Solution not available in the source paper.

    9State whether the given statements are True or…

    The scope of a local variable is limited to the method or the block it is declared in.
    True

    Solution not available in the source paper.

    10State whether the given statements are True or…

    The keyword static makes a variable a class variable.
    True

    Solution not available in the source paper.

    1Assignment Questions

    What is a method? Explain the various parts of a method.

    Show SolutionHide Solution↓

    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.

    2Assignment Questions

    How do you define and invoke a method?

    Show SolutionHide Solution↓

    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();
        }
    }
    3Assignment Questions

    Explain the role of return statement in a method?

    Show SolutionHide Solution↓

    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.

    4Assignment Questions

    What does void signify in the method prototype?

    Show SolutionHide Solution↓

    void in method prototype means that the method does not return any value.

    5Assignment Questions

    Explain the difference between actual and formal parameters with one example of each.

    Show SolutionHide Solution↓

    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.

    6Assignment Questions

    Explain static and non-static methods.

    Show SolutionHide Solution↓

    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.

    7Assignment Questions

    Explain the scope of variables in Java

    Show SolutionHide Solution↓

    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.

    Press ↓ to reveal a solution and move to the next question. Back to Solutions for Class 9 ICSE Logix Kips Computer Applications with BlueJ Java.