Preview

07 - Writing Methods Part 2

 1. Methods are useful because they help us avoid having to repeat the same sequence of commands over and over.

  TRUE

  FALSE

 2. Analyse the following code. ___________ is a method that looks for a letter in a particular string.
public class StringFind
{
   public boolean findLetter(String letter, String text)
   {
      boolean flag = false;
      for(int i=0; i < text.length(); i++) {
          if (text.substring(i, i+1).equalsIgnoreCase(letter))
              flag = true;
      }
      return flag;
   }
  public static void main(String args[])
  {
    StringFind test = new StringFind();
    String message = "TestandTrack";
    String letter = "p";
    System.out.println("Does " + message +  " contain a " + letter + "?");
    System.out.println( test.findLetter(letter, message) );
  }
}

  findLetter

  Find test

  StringFind

  String message

 3. The code above produces two outputs when it is compiled. What is the second output?

  TestandTrack

  p

  TRUE

  FALSE

 4. The isBoiling method is intended to return __________ if increasing the currentTemp by the parameter amount is greater than or equal to the boilingPoint.
public class Liquid
{
    private int currentTemp;
    private int boilingPoint;

    public Liquid(int ct, int bp)
    {
        currentTemp = ct;
        boilingPoint = bp;
    }

    public boolean isBoiling(int amount)
    {
         if (currentTemp + amount < boilingPoint)
     {
         return false;
     }
     else
     {
         return true;
     }
    }
}

  TRUE

  ct

  currentTemp

  FALSE

 5. Java uses Call ________when it passes arguments to methods. This means that a copy of the value in the argument is saved in the parameter variable

  by value

  by reference

  by parameter passing

  by void method

 6. If the parameter variable changes its value inside the method, the original value outside the method is ___________.

  also changed

  void

  immutable

  not changed

 7. Methods can even access the private data and methods of a parameter that is a reference to an object if the parameter is _________ the method?s enclosing class.

  the same type as

  identical in operator value to

  identical in value to

  different to

 8. Note that Strings are immutable objects, so they cannot be changed by the method; only a ___________________.

  mutable method can change this.

  identical value + 1 is stored in the next available memory location.

  referenced address location Is used.

  new changed copy of them can be made.

 9. When an actual parameter is a reference to an object, the method or constructor could use this reference to alter the state of the original object.

  TRUE

  FALSE

 10. It is good programming practice to always modify mutable objects that are passed as parameters even if this is not required in the specification.

  FALSE

  TRUE