Home  | 

 

 ONLINE SESSION    C   

Covering Gaddis chapter 5


Answer the following multiple choice questions for chapter 5

 

Multiple Choice ch 5

 

1.    This type of method does not return a value.

a.    null

b.    void

c.    empty

d.    anonymous

2.    This appears at the beginning of a method definition.

a.    semicolon

b.    parentheses

c.    body

d.    header

3.    The body of a method is enclosed in      .

a.    curly braces { }

b.    square brackets []

c.    parentheses ()

d.    quotation marks ""

4.    A method header can contain      .

a.    method modifiers

b.    the method return type

c.    the method name

d.    a list of parameter declarations

e.    all of these

f.     none of these

5.    A value that is passed into a method when it is called is known as a(n)      .

a.    parameter

b.    argument

c.    signal

d.    return value

6.    A variable that receives a value that is passed into a method is known as a(n)      .

a.    parameter

b.    argument

c.    signal

d.    return value

7.    This javadoc tag is used to document a parameter variable.

a.    @parameter

b.    @param

c.    @paramvar

d.    @arg


8.    This statement causes a method to end and sends a value back to the statement that called the method.

a.    end

b.    send

c.    exit

d.    return



DEFINE THE FOLLOWING TERMS

Classes/objects

Methods

Fields

Void

Value returning

Parameter lists

Argument lists

 

REPRESENTATIVE SAMPLE PROGRAMS FOR CHAPTER 5

SIMILAR PROGRAMS  are in the chapter examples that you downloaded

optional - not to be turned in

type in your name and RUN THEM BOTH using JGrasp


 

THE FIRST ONLY SENDS ARGUMENTS TO A METHOD

 

/**
This program demonstrates a method that
accepts two arguments.
*/

public class TwoArgs
{
public static void main(String[] args)
{
double a = 4.5;
double b = 6.9;

showSum(a, b);
}

/**
The showSum method displays the sum of
two numbers.
*/

public static void showSum(double num1, double num2)
{
double sum; // To hold the sum

sum = num1 + num2;
System.out.println("The sum is " + sum);
}
}

 

 

THE SECOND SENDS AND RETURNS

/**
This program demonstrates sending and returning a value.
*/

public class ValueReturn
{
public static void main(String[] args)
{
int total, value1 = 20, value2 = 40;

// Call the sum method, passing the contents of
// value1 and value2 as arguments. Assign the
// return value to the total variable.
total = sum(value1, value2);

// Display the contents of all these variables.
System.out.println("The sum of " + value1 +
" and " + value2 + " is " +
total);
}

/**
The sum method returns the sum of its two parameters.
@param num1 The first number to be added.
@param num2 The second number to be added.
@return The sum of num1 and num2.
*/

public static int sum(int num1, int num2)
{
int result; // result is a local variable

// Assign the value of num1 + num2 to result.
result = num1 + num2;

// Return the value in the result variable.
return result;
}
}

 

HINTS FOR CHAPTER 5

Read the following hints for correcting your JAVA programs

·         Putting a semicolon at the end of a method header. Method headers are never terminated with a semicolon.

·         Writing modifiers or return types in a method call statement. Method modifiers and return types are written in method headers, but never in method calls.

·         Forgetting to write the empty parentheses in a call to a method that accepts no arguments. You must always write the parentheses in a method call statement, even if the method doesn’t accept arguments.

·         Forgetting to pass arguments to methods that require them. If a method has parameter variables, you must provide arguments when calling the method.

·         Passing an argument of a data type that cannot be automatically converted to the data type of the parameter variable. Java will automatically perform a widening conversion if the argument’s data type is ranked lower than the parameter variable’s data type. But Java will not automatically convert an argument to a lower-ranking data type.

·         Attempting to access a parameter variable with code outside the method where the variable is declared. A parameter variable is visible only within the method it is declared in.

·         Not writing the data type of each parameter variable in a method header. Each parameter variable declaration inside the parentheses of a method header must include the variable’s data type.

·         Changing the contents of a method’s parameter variable and expecting the argument that was passed into the parameter to change as well. Method arguments are passed by value, which means that a copy of the argument is passed into a parameter variable. Changes to the parameter variable have no effect on the argument.

·         Using a variable to receive a method’s return value when the variable’s data type is incompatible with the data type of the return value. A variable that receives a method’s return value must be of a data type that is compatible with the data type of the return value.

·         Not writing a return statement in a value-returning method. If a method’s return type is anything other than void, it should return a value.

·         Not writing a required throws clause in a method that calls another method. Any method that calls a method with a throws clause in its header must either handle the potential exception or have the same throws clause. .


Chapter 5

Multiple Choice

 

1. b

2. d

3. a

4. e

5. b

6. a

7. b

8. d