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
}

  modifier

  formal parameters

  list of parameters

  return type

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

  return type

  modifier

  list of parameters

  formal parameters

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

  list of parameters

  return type

  formal parameters

  modifier

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

  formal parameters

  modifier

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

  9

  6

  11

  3

 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.

  reference

  value

  call

  integer

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

  integer

  call

  reference

  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 be transformed into an object

  will change

  will not change

 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.

  TRUE

  FALSE

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

  object

  value

  reference

  type

 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.

  TRUE

  FALSE

 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.

  TRUE

  FALSE

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

  All of the statements here are correct.

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

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

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

 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 an object as parameter

  take the value NULL

  take a single parameter

  take a string object

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

  passing NULL to it would not compile

  is therefore not an object

  passing any paramters to it would not compile

  is therefore referring to an invalid class

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

  FALSE

  TRUE

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

  string

  value

  object

  null

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

  public void goFishingIn(Pond pond)

  this.likes(Fish fish);

  this.likes(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);     
        }
   }

  6

  7

  5

  8

 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 hiding

  none of the mentioned

  method overloading

 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

  compile time error

  run time error

  int float method