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.

  FALSE

  TRUE

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

  StringFind

  Find test

  String message

  findLetter

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

  TRUE

  FALSE

  TestandTrack

  p

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

  ct

  FALSE

  currentTemp

  TRUE

 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 void method

  by parameter passing

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

  void

  not changed

  immutable

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

  different to

  the same type as

  identical in operator value to

  identical in value to

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

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

  referenced address location Is used.

  new changed copy of them can be made.

  mutable method can change this.

 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.

  TRUE

  FALSE