Chapter.5 Test Bank Docx Methods - Java Control Structures 7e Test Bank by Tony Gaddis. DOCX document preview.

Chapter.5 Test Bank Docx Methods

Starting Out with Java: From Control Structures through Objects 7e (Gaddis)

Chapter 5 Methods

TRUE/FALSE

1. Methods are commonly used to break a problem into small manageable pieces.

2. Two general categories of methods are void methods and value-returning methods.

3. In the method header, the method modifier public means that the method belongs to the class, not a specific object.

4. Constants, variables, and the values of expressions may be passed as arguments to a method.

5. A parameter variable's scope is the method in which the parameter is declared.

6. You must have a return statement in a value-returning method.

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

8. In the method header the static method modifier means the method is available to code outside the class.

9. Only constants and variables may be passed as arguments to methods.

10. No statement outside the method in which a parameter variable is declared can access the parameter by its name.

11. The expression in a return statement can be any expression that has a value.

12. A value-returning method can return a reference to a non-primitive type.

MULTIPLE CHOICE

1. Methods are commonly used to:

a.

speed up the compilation of a program

b.

break a program down into small manageable pieces

c.

emphasize certain parts of the logic

d.

document the program

2. Which type of method performs a task and sends a value back to the code that called it?

a.

value-returning

c.

complex

b.

void

d.

local

3. In the following code, System.out.println(num) is an example of __________.

double num = 5.4;

System.out.println(num);

num = 0.0;

a.

a value-returning method

c.

a complex method

b.

a void method

d.

a local variable

4. To create a method, you must write its __________.

a.

header

c.

body

b.

return type

d.

definition

5. In the header, the method name is always followed by __________.

a.

parentheses

c.

a data type

b.

a return type

d.

braces

6. A __________ is a part of a method that contains a collection of statements that are performed when the method is executed.

a.

method header

c.

method body

b.

return type

d.

method modifier

7. Which of the following is not a part of a method call?

a.

method name

c.

parentheses

b.

return type

d.

All of these

8. If method A calls method B, and method B calls method C, and method C calls method D, when method D finishes, what happens?

a.

Control is returned to method A.

c.

Control is returned to method C.

b.

Control is returned to method B.

d.

The program terminates.

9. Values that are sent into a method are called __________.

a.

variables

c.

literals

b.

arguments

d.

types

10. When an argument is passed to a method __________.

a.

its value is copied into the method's parameter variable

b.

its value may be changed within the called method

c.

Both (a) and (b) are correct.

d.

Neither (a) nor (b) are correct.

11. Given the following method header, which of these method calls is incorrect?

public void displayValue(int x, int y);

a.

displayValue(a, b); // where a is a short and b is a byte

b.

displayValue(a, b); // where a is an int and b is a byte

c.

displayValue(a, b); // where a is a short and b is a long

d.

All of these would give an error.

12. Given the following method header, which of these method calls is incorrect?

public void displayValue(double x, int y);

a.

displayValue(a, b); // where a is a long and b is a byte

b.

displayValue(a, b); // where a is an int and b is a byte

c.

displayValue(a, b); // where a is a short and b is a long

d.

All of these would give an error.

13. Given the following method, which of these method calls is valid?

public static void showProduct (int num1, double num2)

{

int product;

product = num1 * (int)num2;

System.out.println("The product is " + product);

}

a.

showProduct(5.5, 4.0);

c.

showProduct(10, 4.5);

b.

showProduct(10.0, 4);

d.

showProduct(33.0, 55.0);

14. Given the following method, which of these method calls is valid?

public static void showProduct (double num1, int num2)

{

double product;

product = num1 * num2;

System.out.println("The product is " + product);

}

a.

showProduct("5", "40");

c.

showProduct(10, 4.5);

b.

showProduct(10.0, 4.6);

d.

showProduct(3.3, 55);

15. When an object, such as a String, is passed as an argument it is __________.

a.

actually a reference to the object that is passed

b.

passed by value like any other parameter value

c.

encrypted

d.

necessary to know exactly how long the string is when writing the program

16. All @param tags in a method's documentation must __________.

a.

end with a */

b.

appear after the general description of the method

c.

appear before the method header

d.

span several lines

17. In a @return tag statement the description __________.

a.

cannot be longer than one line

c.

must be longer than one line

b.

describes the return value

d.

describes the parameter values

18. A method

a.

may have zero or more parameters

b.

never has parameter variables

c.

must have at least two parameter variables

d.

may not have only one parameter variable

19. A special variable that holds a value being passed into a method is called a(n) __________.

a.

modifier

c.

alias

b.

parameter

d.

argument

20. When you pass an argument to a method you should be sure that the argument's type is compatible with __________.

a.

the parameter variable's data type

c.

the version of Java currently used

b.

the method's return type

d.

IEEE standards

21. In a general sense, a method is __________.

a.

a plan

b.

a statement inside a loop

c.

a comment

d.

a collection of statements that perform a specific task

22. Which of the following is not part of a method header?

a.

return type

c.

parentheses

b.

method name

d.

semicolon

23. A __________ type of method performs a task and then terminates.

a.

value-returning

c.

local

b.

void

d.

simple

24. When a method tests an argument and returns a true or false value, it should return __________.

a.

a zero for true and a one for false

b.

a boolean value

c.

a zero for false and a non-zero for true

d.

A method should not be used for this.

25. A parameter variable's scope is __________.

a.

the method in which the parameter is declared

b.

the class to which the method belongs

c.

the main method

d.

All of these are true.

26. The lifetime of a method's local variable is __________.

a.

the duration of the program

b.

the duration of the class to which the method belongs

c.

the duration of the method that called the local variable's method

d.

only while the method is executing

27. The header of a value-returning method must specify __________.

a.

the method's local variable names

b.

the name of the variable in the calling method that will receive the returned value

c.

the data type of the return value

d.

All of these must be specified.

28. You should always document a method by writing comments that appear __________.

a.

just before the method's definition

b.

just after the method's definition

c.

at the end of the file

d.

only if the method is more than five lines long

29. When an argument value is passed to a method, the receiving parameter variable is __________.

a.

declared within the body of the method

b.

declared in the method header inside the parentheses

c.

declared in the call

d.

used in the declaration of the argument

30. If you attempt to use a local variable before it has been given a value, __________.

a.

a compiler error will occur

b.

the local variable will always contain the value 0

c.

the results will be unpredictable

d.

the local variable will be ignored

31. Values stored in local variables __________.

a.

are lost between calls to the method in which they are declared

b.

retain their values from the last call to the method in which they are declared

c.

may be referenced by the calling method

d.

may be referenced by any other method if the method in which they are declared is a public method

32. Local variables can be initialized with __________.

a.

constants

c.

the result of an arithmetic operation

b.

parameter values

d.

Any of these.

33. The process of breaking a problem down into smaller pieces is sometimes called __________.

a.

the divide and conquer method

c.

top-down programming

b.

the scientific method

d.

whole-into-part programming

34. A value-returning method must specify __________ as its return type in the method header.

a.

an int

c.

a boolean

b.

a double

d.

Any valid data type

35. What will be returned from the following method?

public static double methodA()

{

double a = 8.5 + 9.5;

return a;

}

a.

18.0

b.

18

c.

8

d.

19

36. What will be returned from the following method?

public static int methodA()

{

double a = 8.5 + 9.5;

return a;

}

a.

18.0

c.

8.0

b.

18

d.

This is an error.

37. In the following code, Integer.parseInt(str) is an example of __________.

int num;

string str = "555";

num = Integer.parseInt(str) + 5;

a.

a value-returning method

c.

a local variable

b.

a void method

d.

a complex method

38. What will be the result after the following code executes?

int num;

string str = "555";

num = Integer.parseInt(string str) + 5;

a.

num will be set to 560

b.

str will have a value of "560"

c.

The last line of code will cause an error.

d.

Neither num nor str will be changed.

39. When writing documentation comments for a method, you can provide a description of each parameter by using a __________.

a.

@comment tag

c.

@param tag

b.

@doc tag

d.

@return tag

40. To document the return value of a method you can use a __________.

a.

@comment tag

c.

@param tag

b.

@returnValue tag

d.

@return tag

41. Assume that the following method header is for a method in class A.

public void displayValue(int value)

Assume that the following code segments appear in another method, also in class A. Which contains a legal call to the displayValue method?

a.

int x = 7;

void displayValue(x);

c.

int x = 7;

displayValue(int x);

b.

int x = 7;

displayValue(x);

d.

int x = 7;

displayValue(x)

MULTIPLE RESPONSE

1. Select all that apply. Which of the following are benefits of using methods in programming?

a.

The program will compile faster.

c.

Code can be reused.

b.

Problems are solved more easily.

d.

Programs are simplified.

2. Select all that apply. Local variables __________.

a.

are hidden from other methods

b.

cannot be changed once they are given a value in the method where they exist

c.

may have the same name as local variables in other methods

d.

lose the values stored in them between calls to the method in which the variable is declared

3. Select all that apply. Any method that calls a method with a throws clause in its header must __________.

a.

handle the potential exception

b.

have the same throws clause

c.

do nothing since the called program will take care of the throws clause

d.

contain only string variables

4. Select all that apply. Which of the following types of values can be passed to a method that has an int parameter variable?

a.

float

c.

long

b.

double

d.

int

Document Information

Document Type:
DOCX
Chapter Number:
5
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 5 Methods
Author:
Tony Gaddis

Connected Book

Java Control Structures 7e Test Bank

By Tony Gaddis

Test Bank General
View Product →

$24.99

100% satisfaction guarantee

Buy Full Test Bank

Benefits

Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party