Preview

11 - Calling a void method with parameters

 1. The following code shows the syntax of a method. What is 'public static'?
Syntax

public static int methodName(int a, int b) {
   // body
}

  return type

  modifier

  formal parameters

  list of parameters

 2. In the code above, 'int' is the:

  list of parameters

  formal parameters

  return type

  modifier

 3. In the code in question 1, 'a,b' are:

  return type

  formal parameters

  modifier

  list of parameters

 4. In the code in question 1, 'int a, int b' are:

  modifier

  formal parameters

  list of parameters

  return type

 5. What is the output of the following code? Minimum Value =
public class ExampleMinNumber {
   
   public static void main(String[] args) {
      int a = 11;
      int b = 6;
      int c = minFunction(a, b);
      System.out.println("Minimum Value = " + c);
   }

   /** returns the minimum of two numbers */
   public static int minFunction(int n1, int n2) {
      int min;
      if (n1 > n2)
         min = n2;
      else
         min = n1;

      return min; 
   }
}

  3

  6

  9

  11

 6. A parameter is a _____ that you can pass to a method in Java.

  object

  reference

  string object

  value

 7. Passing Parameters by _____ means calling a method with a parameter. Through this, the argument value is passed to the parameter.

  integer

  call

  reference

  value

 8. The following example shows an example of passing parameter by ______.
public class swappingExample {

   public static void main(String[] args) {
      int a = 30;
      int b = 45;
      System.out.println("Before swapping, a = " + a + " and b = " + b);

      // Invoke the swap method
      swapFunction(a, b);
      System.out.println("\n**Now, Before and After swapping values will be same here**:");
      System.out.println("After swapping, a = " + a + " and b is " + b);
   }

   public static void swapFunction(int a, int b) {
      System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
      
      // Swap n1 with n2
      int c = a;
      a = b;
      b = c;
      System.out.println("After swapping(Inside), a = " + a + " b = " + b);
   }
}

  call

  reference

  integer

  value

 9. When a class has two or more methods by the same name but different parameters, it is known as method ___________.

 10. In ____________, a method has the same method name, type, number of parameters, etc

 11. _________ are constructed out of statements which are placed between brackets like these "{ }" as shown below:
package exlcode;

public class MethodExample {

  public static int exampleVariableOne = 5;
  public static int exampleVariableTwo = 10;

  public static void main(String[] args) {
    // this prints the sum of exampleVariableOne and exampleVariableTwo
    System.out.println(add(exampleVariableOne, exampleVariableTwo));
  }

  // this method takes in two parameters and
  // returns the sum of the two parameters
  public static int add(int parameterOne, int parameterTwo) {
    return parameterOne + parameterTwo;
  }
}

 12. In the above example, the return type _____ signifies that the method does not return anything, which is why we do not see a statement that says return.

 13. Analyse the following code. Is the following statement true or false? Changing the value of parameterOne does affect the value of exampleVariableOne.
package exlcode;

public class MethodExample {

  public static int exampleVariableOne = 5;
  public static int exampleVariableTwo = 10;

  public static void main(String[] args) {
    // this prints the sum of exampleVariableOne and exampleVariableTwo
    System.out.println(add(exampleVariableOne, exampleVariableTwo));
  }

  // this method takes in two parameters and
  // returns the sum of the two parameters
  public static int add(int parameterOne, int parameterTwo) {
    return parameterOne + parameterTwo;
  }
}

  FALSE

  TRUE

 14. When dealing with methods, when we need more than one parameter, they are written one after the other, separated by a semi colon.

  FALSE

  TRUE

 15. All parameters are just copies of the original value or address. Regardless of what we do to the parameter inside the method, the original value ?....

  will be an integer

  will not change

  will change

  will be transformed into an object

 16. _____' is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor.

 17. A method that accepts parameters must list the parameters in the method declaration. The parameters are placed in a parameter list inside the parentheses that follow the method name.

  FALSE

  TRUE

 18. For each parameter used by the method, you list the parameter _____ followed by the parameter name.

  object

  type

  value

  reference

 19. The names you use for parameters can be the same as the names you use for the variables you pass to the method when you call it, but they don?t have to be.

  FALSE

  TRUE

 20. When Java passes a variable to a method via a parameter, the method itself receives a copy of the variable?s value, not the variable itself.

  FALSE

  TRUE

 21. Analyse the following code. Which of the following statements is correct?
public class ChangeParameters
{
 public static void main(String[] args)
 {
  int number = 1;
  tryToChangeNumber(number);
  System.out.println(number);
 }
 public static void tryToChangeNumber(int i)
 {
  i = 2;
 }
}

  Here a variable named number is set to 1 and then passed to the method named tryToChangeNumber

  All of the statements here are correct.

  This method receives the variable as a parameter named i and then sets the value of i to 2

  In the main method, println is used to print the value of number after the tryToChangeNumber method returns

 22. Refer to the code above. Even though the tryToChangeNumber method changes the value of its parameter, that change has no effect on the original variable that was passed to the method.

  FALSE

  TRUE

 23. Bill has a class which defines two overloaded methods. The first function is a function of a single argument (which must be provided) and can only validly ________________________.
public void handle(Void e) 

protected void handle() 

  take a single parameter

  take an object as parameter

  take a string object

  take the value NULL

 24. Referring to the code in the previous question, the second function doesn't take an argument and _______________________.

  is therefore not an object

  is therefore referring to an invalid class

  passing NULL to it would not compile

  passing any paramters to it would not compile

 25. The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.

  TRUE

  FALSE

 26. Because the Void class can not be instantiated, the only value you can pass to a method with a Void type parameter, such as handle(Void e), is ____.

  value

  null

  object

  string

 27. If you are declaring a method, you state parameters with classes, such as:

  this.likes(fish);

  public void goFishingIn(Pond pond)

  this.likes(Fish fish);

  Fish fish = pond.catchAFish();

 28. What is the output of the following code?
    class overload 
    {
        int x;
  int y;
        void add(int a) 
        {
            x =  a + 1;
        }
        void add(int a, int b)
        {
            x =  a + 2;
        }        
    }    
    class Overload_methods 
    {
        public static void main(String args[])
        {
            overload obj = new overload();   
            int a = 0;
            obj.add(6);
            System.out.println(obj.x);     
        }
   }

  7

  5

  8

  6

 29. What is the process of defining two or more methods within same class that have same name but different parameters declaration?

  method overriding

  method overloading

  none of the mentioned

  method hiding

 30. What is the output of the following code?
class Compscibits
{
 public void m1 (int i,float f)
 {
  System.out.println(" int float method");
 }
 
 public void m1(float f,int i);
  {
  System.out.println("float int method");
  }
 
  public static void main(String[]args)
  {
    Compscibits c=new Compscibits();
        c.m1(20,20);
  }
}

  float int method

  run time error

  int float method

  compile time error