CS110 Java Practice Quiz #5

Methods




  Choose the best alternative:
  1. Methods eliminate the need for duplicate statements. .

  2. Math is a standard class in Java. .

  3. A program statement or expression that transfers control to a method so that the
    method will perform its particular subtask is called a .

  4. A value supplied to a tethod is known as a(n) .

  5. When the type of a method is defined as void, this indicates .

  6. What output is produced by the segment of code shown below:
    public static void main(String[] args ){
        System.out.print("W");
        p( );
        System.out.print( "Z");
    }

    static private void p( ){
        System.out.print("X");
        System.out.print( "Y");
    }

  7. What output is produced by the segment of code shown below:
    public static void main(String[] args ){
        System.out.print("BOBO");
        p3( );
        System.out.print( "MELON");
        p2( );
    }

    static private void p1( ){
        System.out.print("IS"); }

    static private void p2( ){
        System.out.print("HEAD"); }

    static private void p3( ){
        p1( );
        System.out.print( "A");
    }


  8. The output produced by the code segment below is .
    public static void main(String[] args ){
        System.out.print("B");
        p( );
        System.out.print( "C");
    }

    static private void p( ){
        System.out.print("A");
        p( );
    }

  9. What output is produced by the segment of code shown below:
    public static void main(String[] args ){
        int x;
        x = p(4);
        System.out.print( x);
    }

    static private int p(int temp){
        return temp + 3;
    }

  10. What output is produced by the segment of code shown below:
    public static void main(String[] args ){
        int z = 6;
       int x = p(z);
        System.out.print( x);
    }

    static private int p(int temp){
        System.out.print("B");
        return temp + 3;
    }

  11. In the Method heading below, a and b are called .
    double sum (int a, char b)

  12. In the method heading below, the type of the value returned by the Method is .
    static private double sum (int a, char b)

  13. What value is returned from the Method call below:
    abs(-33)

  14. What value is returned from the Method call below:
    abs(324)

  15. What value is returned from the Method call below:
    pow(3,2)

  16. What value is returned from the Method call below:
    fabs(-45.7)

  17. What value is returned from the Method call below:
    floor(67.99999)

  18. What value is returned from the Method call below:
    floor(67.0001)

  19. What value is returned from the Method call below:
    ceil(12.0000)

  20. What value is returned from the Method call below:
    ceil(12.1)

Answers

  1. True. Methods eliminate the need for duplicate statements.
  2. True.
  3. A Method call transfers control to a Method.
  4. An argument is supplied to a Method in a Method call.
  5. A Method returns nothing if it is VOID.
  6. WXYZ
  7. BOBOISAMELONHEAD
  8. This is an infinite loop because it never ends.
  9. 7 because 4 is passed to temp in Method p. The value temp + 3 is returned.
  10. B9 because 6 is passed to temp in Method p, 'B' is output and temp + 3 is returned.
  11. a and b are parameters.
  12. sum is a Method that is a member of the double class.
  13. The absolute value of the integer -33 is 33.
  14. The absolute value of a positive integer is the positive integer.
  15. 9 because pow(3,2) reads 3 to the power of 2 .
  16. fabs returns the absolute value of a floating point number. The answer is 45.7
  17. The largest integer not greater than 67.99999 is 67.
  18. The largest integer not greater than 67.0001 is 67.
  19. The smallest integer not less than 12.0000 is 12.
  20. The smallest integer not less than 12.1 is 13.