User-Defined Methods

ICSE Class 10 Computer Applications · Logix Computer Applications · 39 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↓
  • of 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 method that changes the state of an object is known as ...........

    Show SolutionHide Solution↓
  • impure method ✓
  • 71 Mark · Multiple Choice Questions

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

    Show SolutionHide Solution↓
  • Method or block it is declared in ✓
  • 81 Mark · Multiple Choice Questions

    The technique in which the change in the formal parameter gets reflected in the actual parameter is known as ...........

    Show SolutionHide Solution↓
  • call by reference ✓
  • 91 Mark · Multiple Choice Questions

    In which technique are the values of actual parameters copied to the formal parameters?

    Show SolutionHide Solution↓
  • call by value ✓
  • 101 Mark · Multiple Choice Questions

    A method with many definitions is known as ...........

    Show SolutionHide Solution↓
  • overloaded method ✓
  • 1State whether the given statements are True or…

    A method may contain any number of return statements.
    True

    Solution not available in the source paper.

    2State 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.

    3State whether the given statements are True or…

    A method can return more than one value.
    False

    Solution not available in the source paper.

    4State whether the given statements are True or…

    Methods defined as void must return a value.
    False

    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…

    In Java, all primitive types are passed by value and all reference types are passed by reference.
    True

    Solution not available in the source paper.

    7State whether the given statements are True or…

    You can place the return statement in a void method without any expression.
    True

    Solution not available in the source paper.

    8State 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.

    9State whether the given statements are True or…

    Parameters in the method definition are called dummy parameters.
    True

    Solution not available in the source paper.

    10State whether the given statements are True or…

    Methods reside in a class in Java.
    True

    Solution not available in the source paper.

    11State whether the given statements are True or…

    Method overloading is one of the ways by which Java implements polymorphism.
    True

    Solution not available in the source paper.

    12State 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.

    13State whether the given statements are True or…

    The keyword static makes a method a class method.
    True

    Solution not available in the source paper.

    14State whether the given statements are True or…

    An impure method always returns the same value when the same arguments are given.
    False

    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

    What is a method signature?

    Show SolutionHide Solution↓

    The method name along with the list of parameters used in the method prototype is known as method signature.

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

    What does the return statement do 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.

    5Assignment 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.

    6Assignment Questions

    Explain the difference between actual and formal parameters.

    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.

    7Assignment 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.

    8Assignment Questions

    What happens when an argument is passed by reference?

    Show SolutionHide Solution↓

    In pass by reference, the reference of the actual parameter is passed to the formal parameter. Both actual parameter and formal parameter represent the same memory location so any changes made to the formal parameters get reflected in the actual parameters.

    9Assignment Questions

    When a method has been declared more than once in a class, how does Java determine the overloading?

    Show SolutionHide Solution↓

    Overloading is determined based on the number and type of parameters of the method.

    10Assignment Questions

    What is an ambiguous invocation? Give an example.

    Show SolutionHide Solution↓

    Sometimes there are two or more possible matches in the invocation of an overloaded method. In such cases, the compiler cannot determine the most specific match. This is referred to as ambiguous invocation. It causes a compile time error. For example, the below program will cause a compile time error due to ambiguous invocation:

    public class AmbiguousDemo {
    
        public static double max(double num1, int num2) {
            if (num1 > num2)
                return num1;
            else
                return num2;
        }
    
        public static double max(int num1, double num2) {
            if (num1 > num2)
                return num1;
            else
                return num2;
        }
    
        public static void main(String args[]) {
            /*
             * This line will cause a compile time error
             * as reference to max is ambiguous
             */
            System.out.println(max(19, 20));
        }
    }
    11Assignment Questions

    Given below are the two method definitions:

    1. public static double Check(double x, double y)
    2. public static double Check(int x, double y)

    Which of the two methods is invoked for the following?

    Show SolutionHide Solution↓
    1. double z = Check (6, 5); invokes public static double Check(int x, double y)
    2. double z = Check (5.5, 7.4); invokes public static double Check(double x, double y)
    12Assignment Questions

    What is the signature of the following method heading?

    public void CoolMethod(int xx, char yy, int zz)

    Show SolutionHide Solution↓

    Signature of this method is:

    CoolMethod(int xx, char yy, int zz)

    13Assignment Questions

    Which OOP principle implements function (method) overloading?

    Show SolutionHide Solution↓

    Polymorphism

    14Assignment Questions

    A palindromic prime is a prime number and also palindromic. For example, 131, 313, and 757 are prime numbers and also palindromic prime numbers. Write a program that displays the first 100 palindromic prime numbers.

    Show SolutionHide Solution↓
    public class KboatPalindromicPrime
    {
        public static boolean isPrime(int n) {
            int c = 0;
            for (int i = 1; i <= n; i++) {
                if (n % i == 0) {
                    c++;
                }
            }
            
            boolean prime = c == 2;
            return prime;
        }
    
        public static boolean isPalindrome(int n) {
            int num = n, revNum = 0;
            while (num != 0) {
                int digit = num % 10;
                num /= 10;
                revNum = revNum * 10 + digit;
            }
    
            boolean palin = revNum == n;
            return palin;
        }
    
        public static void main(String args[]) {
            int x = 1, count = 0;
            while (count < 100) {
                if (isPrime(x) && isPalindrome(x)) {
                    count++;
                    System.out.println(x);
                }
                x++;
            }
        }
    }
    Figure unavailable in the source: BlueJ output of KboatPalindromicPrime.java
    15Assignment Questions

    Differentiate between the following:

    1. Call by value and Call by reference
    Show SolutionHide Solution↓
    Call by valueCall by reference
    Values of actual parameters are copied to formal parameters.Reference of actual parameters is passed to formal parameters.
    Changes made to formal parameters are not reflected back to actual parameters.Changes made to formal parameters are reflected back to actual parameters.
    1. Pure and Impure methods
    Pure methodsImpure methods
    Pure methods take objects and/or primitive data types as arguments but does not modify the objects.Impure methods change the state of received objects.
    Pure methods doesn't have side effects.Impure methods have side effects.
    1. Simple Method and Overloaded method
    Simple MethodOverloaded method
    Simple Methods have unique names.In case of Overloaded methods, there are two or more methods with the same name.
    We can identify the method being invoked by looking at its name.We need to examine the method's number and type of parameters to determine which method will be invoked.

    Press ↓ to reveal a solution and move to the next question. Back to Logix Computer Applications.

    Free sample bank