Java Chapter 5

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/59

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

60 Terms

1
New cards

1) Methods are commonly used to:

A) speed up the compilation of a program

B) break a problem down into small manageable pieces

C) emphasize certain parts of the logic

D) document the program

Answer: B

2
New cards

2) Which of the following is NOT a benefit derived from using methods in programming?

A) Pproblems are more easily solved.

B) simplifies programs

C) code reuse

D) All of the above are benefits.

Answer: D

3
New cards

3) This type of method performs a task and sends a value back to the code that called it.

A) value-returning

B) void

C) complex

D) local

Answer: A

4
New cards

4) 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

B) a void method

C) a complex method

D) a local variable

Answer: B

5
New cards

5) To create a method you must write its:

A) header

B) return type

C) body

D) definition

Answer: D

6
New cards

6) In the header, the method name is always followed by this:

A) parentheses

B) return type

C) data type

D) braces

Answer: A

7
New cards

7) This part of a method is a collection of statements that are performed when the method is executed.

A) method header

B) return type

C) method body

D) method modifier

Answer: C

8
New cards

8) Which of the following is NOT part of a method call?

A) method name

B) return type

C) parentheses

D) all of the above are part of a method call

Answer: B

9
New cards

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

B) Control is returned to method B.

C) Control is returned to method C.

D) The program terminates.

Answer: C

10
New cards

10) Values that are sent into a method are called:

A) variables

B) arguments

C) literals

D) types

Answer: B

11
New cards

11) 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) values may not be passed to methods

D) the method must not assign another value to the parameter that receives the argument

Answer: A

12
New cards

12) What is wrong with the following method call?

displayValue (double x);

A) There is nothing wrong with the statement.

B) displayValue will not accept a parameter.

C) Do not include the data type in the method call.

D) x should be a String.

Answer: C

13
New cards

13) Given the following method header, which of the method calls would be an error?

public void displayValues(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) They would all give an error.

Answer: C

14
New cards

14) Which of the following would be a valid method call for the following method?

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);

B) showProduct(10.0, 4);

C) showProduct(10, 4.5);

D) showProduct(33.0, 55.0);

Answer: C

15
New cards

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

Answer: A

16
New cards

16) All @param tags in a method's documentation comment must:

A) end with a */

B) appear after the general description of the method

C) appear before the method header

D) span several lines

Answer: B

17
New cards

17) A special variable that holds a value being passed into a method is called what?

A) Modifier

B) Parameter

C) Alias

D) Argument

Answer: B

18
New cards

18) When you pass an argument to a method, be sure that the argument's data type is compatible with:

A) the parameter variable's data type

B) the method's return type

C) the version of Java currently being used

D) IEEE standards

Answer: A

19
New cards

19) 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 the above

Answer: A

20
New cards

20) 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

Answer: D

21
New cards

21) Local variables:

A) are hidden from other methods

B) may have the same name as local variables in other methods

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

D) All of the above

Answer: D

22
New cards

22) Which of the following values can be passed to a method that has an int parameter variable?

A) float

B) double

C) long

D) All of the above

E) None of the above

Answer: E

23
New cards

23) The header of a value-returning method must specify this.

A) The method's local variable names

B) The name of the variable in the calling program that will receive the returned value

C) The data type of the return value

D) All of the above

Answer: C

24
New cards

24) 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 (as an integer)

C) 8

D) This is an error.

Answer: A

25
New cards

25) In a @return tag statement the description:

A) cannot be longer than one line

B) describes the return value

C) must be longer than one line

D) describes the parameter values

Answer: B

26
New cards

26) 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 type test

Answer: B

27
New cards

27) The phrase divide and conquer is sometimes used to describe:

A) the backbone of the scientific method

B) the process of dividing functions

C) the process of breaking a problem down into smaller pieces

D) the process of using division to solve a mathematical problem

Answer: C

28
New cards

28) 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 performs a specific task

Answer: D

29
New cards

29) Breaking a program down into small manageable methods:

A) makes problems more easily solved

B) allows for code reuse

C) simplifies programs

D) all of the above

Answer: D

30
New cards

30) This type of method performs a task and then terminates.

A) value-returning

B) void

C) local

D) simple

Answer: B

31
New cards

31) 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

B) a void method

C) a local variable

D) a complex method

Answer: A

32
New cards

32) Which of the following is NOT a part of the method header?

A) return type

B) method name

C) parentheses

D) semicolon

Answer: D

33
New cards

33) Which of the following is included in a method call?

A) return type

B) method modifiers

C) parentheses

D) return variable

Answer: C

34
New cards

34) 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

Answer: A

35
New cards

35) 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 calling method

D) uses the declaration of the argument

Answer: B

36
New cards

36) 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

Answer: A

37
New cards

37) What will be the result of the following code?

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 or str will be changed.

Answer: C

38
New cards

38) Given the following method header, which of the method calls would be an error?

public void displayValues(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) They would all give an error.

Answer: C

39
New cards

39) Which of the following would be a valid method call for the following method?

public static void showProduct(double num1, int num2)

{

double product;

product = num1 * num2;

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

product);

}

A) showProduct("5", "40");

B) showProduct(10.0, 4.6);

C) showProduct(10, 4.5);

D) showProduct(3.3, 55);

Answer: D

40
New cards

40) When writing the documentation comments for a method, you can provide a description of each

parameter by using a:

A) @comment tag

B) @doc tag

C) @param tag

D) @return tag

Answer: C

41
New cards

41) 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

Answer: A

42
New cards

42) Local variables can be initialized with:

A) constants

B) parameter values

C) the results of an arithmetic operation

D) any of the above

Answer: D

43
New cards

43) A value-returning method must specify this as its return type in the method header.

A) an int

B) a double

C) a boolean

D) any valid data type

Answer: D

44
New cards

44) What will be returned from the following method?

public static int methodA()

{

double a = 8.5 + 9.5;

return a;

}

A) 18.0

B) 18 (as an integer)

C) 8.0

D) This is an error.

Answer: D

45
New cards

45) To document the return value of a method, use this in a documentation comment.

A) The @param tag

B) The @comment tag

C) The @return tag

D) The @returnValue tag

Answer: C

46
New cards

46) The process of breaking a problem down into smaller pieces is sometimes called:

A) divide and conquer

B) scientific method

C) top-down programming

D) whole-into-part

Answer: A

47
New cards

47) 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) both of the above

D) do nothing, the called program will take care of the throws clause

Answer: C

48
New cards

48) 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);

B) int x = 7;

displayValue(x);

C) int x = 7;

displayValue(int x);

D) int x = 7;

displayValue(x)

Answer: B

49
New cards

T/F: Methods are commonly used to break a problem into small manageable pieces.

Answer: TRUE

50
New cards

T/F: Two general categories of methods are void methods and value returning methods.

Answer: TRUE

51
New cards

T/F: In the method header, the method modifier public means that the method belongs to the class, not a

specific object.

Answer: FALSE

52
New cards

T/F: Constants, variables, and the values of expressions may be passed as arguments to a method.

Answer: TRUE

53
New cards

T/F: A parameter variable's scope is the method in which the parameter is declared.

Answer: TRUE

54
New cards

T/F: You must have a return statement in a value-returning method.

Answer: TRUE

55
New cards

T/F: 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.

Answer: TRUE

56
New cards

T/F: In the method header the static method modifier means the method is available to code outside the

class.

Answer: FALSE

57
New cards

T/F: Only constants and variables may be passed as arguments to methods.

Answer: FALSE

58
New cards

10) No statement outside the method in which a parameter variable is declared can access the parameter

by its name.

Answer: TRUE

59
New cards

T/F: The expression in a return statement can be any expression that has a value.

Answer: TRUE

60
New cards

T/F: A value-returning method can return a reference to a non-primitive type.

Answer: TRUE