Preview

12 - Calling a non void method

 1. _________ 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;
  }
}

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

 3. The items that must be written in a method header are "returnType", "functionName" and "parameterName", because "visibility" has a default value

  FALSE

  TRUE

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

  TRUE

  FALSE

 5. Which one of these method declarations is incorrect?

  private void exampleMethod()

  static int exampleMethod()

  public int exampleMethod (int paramOne, int paramTwo)

  private void (String paramOne)

 6. Consider the following class definition. The method setValue assigns the value of i to the instance field value. What could you write for the implementation of setValue?
public class MyClass
{
private int value;
public void setValue(int i){ / code / }
// Other methods?
}

  Both options (A) and (B)

  (Option A) value = i;

  (Option C) value == i;

  (Option B) this.value = i;

 7. The java run time system automatically calls this method during garbage collection.

  finalize()

  finalised()

  finaliser()

  finally()

 8. What is the output of the following code?
class eq
{
public static void main(String args[])
{
String s1 = ?Hello?;
String s2 = new String(s1);
System.out.println(s1==s2);
}
}

  Hello

  FALSE

  Error

  TRUE

 9. What will the output of the following program be?
class prob1{
int puzzel(int n){

int result;

if (n==1)
return 1;
result = puzzel(n-1) * n;
return result;
}
}

class prob2{

public static void main(String args[])

{

prob1 f = new prob1();

System.out.println(? puzzel of 6 is = ? + f.puzzel(6));

}
}

  720

  6

  30

  120

 10. What is the output of the following code?
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; 
   }
}

  Nothing will be executed as the method is void

  Error

  Minimum Value = 6

  min