Java

TOPIC 35 – TEST 4 REVIEW

 

 

DETAILS & REVIEW

 

 

DETAILS

 

The test covers Unit 4.  All notes.  All work.  All class discussion is fair game.

Your test will be fully written.  You will not have a computer.

 

Minor programming errors will not count against you.

 

The Java template will not necessarily be provided for you.  You are now expected to know the prototype for the main function.

 

 

REVIEW

 

QUESTION 1

Define the following terms.

 

            Function

            Method

            Function call

            Parameter / Argument
            Parameter list / Argument list

            Return-type

            Method prototype

            Static method

            Instance method

            Function implementation
            Void

            Void functions

            Modular programming

            Recursion
            Function overloading

 

QUESTION 2 – SHORT ANSWER QUESTIONS


Answer them.

 

a)    In Java, functions are really named __________ __________.

b)    When calling a function, what do we put in front of the function name if the function is is another class?

c)    When can we omit putting the class name in front of a function call?

d)    Give advantages of using modular programming.

e)    What does a void function return?

f)     What values can a boolean function return?

g)    How do you know if a Java function is recursive?

h)   Values that are passed to a function are called _____________ or ____________.

i)     How many values can be returned from a function?

 

QUESTION 3 – ANALYZING FUNCTION CALLS

A) Consider the following function call and answer the questions below:

 

     int x = Fun.yoyo(4, “awesome”);

 

a)    In which class is the function implementation located?

b)    What is the function’s name?

c)    What is the return type of the function?

d)    How many parameters does the function have?

e)    What is/are the parameter types?  (if any)

f)     Give the complete method prototype for this function.

 

B) Consider the following function call and answer the questions below:


Funner.yogaRocks(43.2);

 

a)    What is the name of the function?

b)    In which class is this function implemented?

c)    What is the return-type?

d)    What is/are the parameter types?  (if any)

e)    Give the complete method prototype for this function.

 

QUESTION 4 – MAKING FUNCTION CALLS

 

Make a function call using the following method prototypes.  Assume the functions are all in a class called OhYeah.  Make sure to provide correct types for the parameters.  Also, make sure to handle any value that is returned by the function by simply storing such a value in a variable.

 

a)    public static int coco()

b)    public static void clicky(int x)

c)    public static String choochoo(int a, int b, int c)

d)    public static double yoyo(String s, int c)

e)    public static void boomboom(double a)

QUESTION 5 – IMPLEMENTING FUNCTIONS

 

a)    Implement a function called quadruple that gets an integer as parameter and returns four times that amount.

b)    Implement a function called legaMark that gets a mark as parameter.  It returns true only if the mark is between 0 and 100.  Otherwise it returns false.

c)    Implement a function called outputGreeting that gets a name as parameter.  It outputs to screen “Hello name”.

 

 

QUESTION 6

 

Using the outputSpaces(…), outputStars(…) and nl() functions from the BC class that we created in the unit lessons, write the following functions:

 

public static void outputC(int size)

 

If size is 5:

 

     *****

     *

     *

     *

     *****

If size is 3:

 

     ***

     *

     ***

 

QUESTION 7 – RECURSION

 

What are the following recursive functions doing?

 

public static int fun(int a, int b)

{

   if (b < 0)

   {

      return fun(-a, -b);

   }

   else if (b == 0)

   {

      return 0;

   }

   else if (b == 1)

   {

      return a;

   }

   else

   {

      return a + fun(a, b-1);

   }

}

public static void fun(int x)

{

   if(x <= 0)

   {

      return;

   }

   else

   {

      System.out.print("*");

      fun(x-1);

   }

}

public static int fun(int a, int b)

{

   if (b == 0)

   {

      return a;

   }

   else if (b < 0)

   {

      return fun(a+1,b+1)

   }

   else

   {

      return fun(a-1,b-1)

   }

}

 


 

Good luck!  Enjoy the burn!

 

 

J