Preview

10 - Calling a Void Method

 1. An object?s behavior refers to what the object can do (or what can be done to it) and is defined by __________.

  instances

  methods

  variables

  classes

 2. A "static" object is unique; it belongs to the class rather than the instance of the class. In other words, _____________________________.

  a static variable is instantiated once the class loads and stored several times

   a static variable is only allocated to the memory once: when the class loads

  a static variable is allocated to the memory several times, before class load

  a static variable is allocated before the class loads, to the main method

 3. A bookstore is working on an on-line ordering system. Read the below and decide which option would be most suitable.
For the online ordering system, for each type of published material 
(books, movies, audio tapes) they need to track the id, title, 
author(s), date published, and price.

Which option would you recommend?
#1: Create one class PublishedMaterial that holds all the information.

#2: Create three classes Book, Movie, and AudioTape that holds the 
necessary information in each individual class.

#3: Create one class BookStore that holds all the information.

#4: Create the class PublishedMaterial and have 3 other classes Book, 
Movie, and AudioTape inherit fields such as id, title, authors(s), etc.

#5: Create separate classes PublishedMaterial, Books, Movies, AudioTape, 
Title, Price, ID, Authors, and DatePublished.

  Option 1 and 2 are similar and could both be used

  Option 3 would allow for the most complex, and therefore the best, solution

  Option 2 and 5 would be best (either of them)

  Option 4 would present the most elegant solution

 4. Procedural abstraction allows a programmer to use a method by knowing what the method does even if they do not know how the method was?written

  FALSE

  TRUE

 5. _________ methods are called through objects of the class. The dot operator is used along with the object name to call _________ methods.

  Non-static

  static

  void

  private

 6. ________ methods do not have return values and are therefore not called as part of an expression.

  private

  void

  public

  static

 7. Using a ____________ to call a method or access an instance variable causes a NullPointerException to be thrown.

  division or mod operator

  null reference

  integer = 0

  string reference

 8. A parameter is a value that is sent to a ______ when it is called.

  method

  integer

  class

  variable

 9. The parameter _________ is used by a caller to send a value to the method. This is called passing a value into the method.
public class CheckingAccount
{
  . . . .
  private int balance;

  . . . .
  public void processDeposit( int amount )
  {
    balance = balance + amount ; 
  }

}

  int

  amount

  void

  balance

 10. Refer to the previous question's code as well as the code to follow. True or False?The instance variable 'balance' of the object will hold its value permanently.
public class CheckingAccountTester
{
  public static void main( String[] args )
  {
    CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 );
    
    bobsAccount.processDeposit( 200 );

    . . . . . .
    
  }
}

  TRUE

  FALSE

 11. Referring to the above code snippets, the the parameter 'amount' will hold its value permanently.

  TRUE

  FALSE

 12. Is this toString() method correct?
public class CheckingAccount
{
  private String accountNumber;
  private String accountHolder;
  private int    balance;

  . . . .
  public void processDeposit( int amount )
  {    
    balance = balance + amount ; 
  }

  // modified toString() method
  public String toString()
  {
    return  "Account: " + accountNumber + "\tName: " + accountHolder + 
            "\tBalance: " +  amount ;
  }

}

  No. The formal parameter 'balance' cannot be given a value

  No. The formal parameter amount can only be used by the processDeposit method.

  Yes. The formal parameter amount can be used by all methods

  Yes. The formal parameter amount can be used by any method

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

  Minimum value = 6

  minFunction = 11

  Minimum value = 17

  11

 14. Which statement is TRUE of the following code snippet?
public class ExampleVoid {

   public static void main(String[] args) {
      methodRankPoints(255.7);
   }

   public static void methodRankPoints(double points) {
      if (points >= 202.5) {
         System.out.println("Rank:A1");
      }else if (points >= 122.4) {
         System.out.println("Rank:A2");
      }else {
         System.out.println("Rank:A3");
      }
   }
}

  The void method methodRankPoints is set up wrong. A function must return a value

  The void method is also the main method. It returns a single value

  The void method methodRankPoints does not return any value

  The void method is also the main method, returning no values

 15. Call to a void method must be a statement i.e. methodRankPoints(255.7); the above code will produce the following output:

  Rank:A1,A2,A3

  Rank: A3

  Rank: A2

  Rank:A1

 16. Parameters can be passed by _____ or by reference. Passing Parameters by ______ means calling a method with a parameter.
Note: Through this, the argument value is passed to the parameter.

 17. The following example shows an example of passing a parameter by reference.
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);
   }
}

  TRUE

  FALSE

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

 19. Which one of these is a valid method declaration?

  void method2()

  void method1

  void method3(void)

  method4()

 20. Consider the following data field and method. Which of the following best describes the contents of myStuff in terms of m and n after the following statement has been executed?
private int[] myStuff;

//precondition: myStuff contains
//   integers in no particular order
public int mystery(int num) {
   for (int k = myStuff.length - 1; k >= 0; k--) {
      if (myStuff[k] < num) {
          return k;
      }
   }
   return -1;
}

int m = mystery(n)

   The smallest value is at position m.

  All values in positions m+1 through myStuff.length-1 are greater than or equal to n.

  All values in position 0 through m are less than n.

  All values in position m+1 through myStuff.length-1 are less than n.

 21. Referring to the code below, what will be printed when the main method is executed?
public class AlphaSort
{

    public static void main(String[] args)
    {
        int i, j;
        String key;
        String[] letters = {"E","D","C","B","A","B"};
        for (j = 1; j < letters.length; j++)
        {
            key = letters[j];
            i = j - 1;
            while (i >= 0)
            {
                if (key.compareTo(letters[i]) > 0)
                {
                    break;
                }
                letters[i + 1] = letters[i];
                i--;
            }
            letters[i + 1] = key;
        }
        for (int t = 0; t < letters.length; t++)
        {
            System.out.print((letters[t]) + "");
        }
    }
}

  E D C B B A

  A B B C D

  E D C B B

  A B B C D E

 22. Fill in the blanks for the paragraph below.
Every method has a return type. The return type defines the 
information that the method returns to the caller after it has 
completed its work. When you don?t have anything to return to 
the caller, you set the return type as void. For example, the 
____________ has a return type of void because it doesn?t return 
anything to the caller.

  static()method

  public()method

  main() method

  void() method

 23. Using camelcase is the standard convention for creating methods and variable names, but the Java compiler doesn?t enforce this convention

  FALSE

  TRUE

 24. _________ are methods with the same name as their class. _____________ are invoked using the new keyword. Example shown below.
public class myClass{
// a constructor that takes one parameter
public myClass(int var){ 
} 
}

 25. Type the word that the following sentence is describing: A keyword used in method declarations to specify that the method does not return any value.