Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

User Defined Methods

Chapter 5

User Defined Methods

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

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-modifiertypemethod-nameparameter-list and method-body.

Question 2

What is a method signature?

Answer

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

Question 3

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 4

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

What does void signify in the method prototype?

Answer

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

Question 6

Explain the difference between actual and formal parameters.

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.

Question 7

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 8

What happens when an argument is passed by reference?

Answer

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.

Question 9

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

Answer

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

Question 10

What is an ambiguous invocation? Give an example.

Answer

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));

    }

}

Question 11

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?

  1. double z = Check (6, 5);
  2. double z = Check (5.5, 7.4);

Answer

  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)

Question 12

What is the signature of the following method heading?

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

Answer

Signature of this method is:

CoolMethod(int xx, char yy, int zz)

Question 13

Which OOP principle implements function (method) overloading?

Answer

Polymorphism

Question 14

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.

Answer

public class PalindromicPrime

   {

       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++;

        }

    }

}

Output

 
SAMPLE INPUT : 
 

SAMPLE OUTPUT : 
 

Question 15

Differentiate between the following:

1. Call by value and Call by reference

Answer

Call by value Call 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.

 

2. Pure and Impure methods

Answer

Pure methods Impure 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.

 

3. Simple Method and Overloaded method

Answer

Simple Method Overloaded 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.