Deck 5: Methods
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/56
Play
Full screen (f)
Deck 5: Methods
1
A __________ is a part of a method that contains a collection of statements that are performed when the method is executed.
A) method header
B) return type
C) method body
D) method modifier
A) method header
B) return type
C) method body
D) method modifier
C
2
Two general categories of methods are void methods and value-returning methods.
True
3
In the header, the method name is always followed by __________.
A) parentheses
B) a return type
C) a data type
D) braces
A) parentheses
B) a return type
C) a data type
D) braces
A
4
To create a method, you must write its __________.
A) header
B) return type
C) body
D) definition
A) header
B) return type
C) body
D) definition
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
5
Which 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
A) value-returning
B) void
C) complex
D) local
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
6
You must have a return statement in a value-returning method.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
7
Methods are commonly used to break a problem into small manageable pieces.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
8
In the method header the static method modifier means the method is available to code outside the class.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
9
Which of the following is not a part of a method call?
A) method name
B) return type
C) parentheses
D) All of these
A) method name
B) return type
C) parentheses
D) All of these
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
10
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.
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.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
11
A parameter variable's scope is the method in which the parameter is declared.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
12
Constants, variables, and the values of expressions may be passed as arguments to a method.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
13
The expression in a return statement can be any expression that has a value.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
14
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
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
15
No statement outside the method in which a parameter variable is declared can access the parameter by its name.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
16
A value-returning method can return a reference to a non-primitive type.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
17
In the method header, the method modifier public means that the method belongs to the class, not a specific object.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
18
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.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
19
Only constants and variables may be passed as arguments to methods.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
20
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
System.out.println(num);
Num = 0.0;
A) a value-returning method
B) a void method
C) a complex method
D) a local variable
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
21
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
A) cannot be longer than one line
B) describes the return value
C) must be longer than one line
D) describes the parameter values
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
22
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
A) a plan
B) a statement inside a loop
C) a comment
D) a collection of statements that perform a specific task
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
23
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.
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.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
24
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
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
25
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.
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.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
26
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.
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.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
27
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.
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.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
28
A __________ type of method performs a task and then terminates.
A) value-returning
B) void
C) local
D) simple
A) value-returning
B) void
C) local
D) simple
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
29
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.
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.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
30
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
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
31
A special variable that holds a value being passed into a method is called a(n) __________.
A) modifier
B) parameter
C) alias
D) argument
A) modifier
B) parameter
C) alias
D) argument
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
32
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
B) the method's return type
C) the version of Java currently used
D) IEEE standards
A) the parameter variable's data type
B) the method's return type
C) the version of Java currently used
D) IEEE standards
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
33
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);
B) showProduct(10.0, 4);
C) showProduct(10, 4.5);
D) showProduct(33.0, 55.0);
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);
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
34
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");
B) showProduct(10.0, 4.6);
C) showProduct(10, 4.5);
D) showProduct(3.3, 55);
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);
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
35
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
A) end with a */
B) appear after the general description of the method
C) appear before the method header
D) span several lines
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
36
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
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
37
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.
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.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
38
Values that are sent into a method are called __________.
A) variables
B) arguments
C) literals
D) types
A) variables
B) arguments
C) literals
D) types
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
39
Which of the following is not part of a method header?
A) return type
B) method name
C) parentheses
D) semicolon
A) return type
B) method name
C) parentheses
D) semicolon
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
40
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
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
41
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
B) double
C) long
D) int
A) float
B) double
C) long
D) int
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
42
Select all that apply. Which of the following are benefits of using methods in programming?
A) The program will compile faster.
B) Problems are solved more easily.
C) Code can be reused.
D) Programs are simplified.
A) The program will compile faster.
B) Problems are solved more easily.
C) Code can be reused.
D) Programs are simplified.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
43
The process of breaking a problem down into smaller pieces is sometimes called __________.
A) the divide and conquer method
B) the scientific method
C) top-down programming
D) whole-into-part programming
A) the divide and conquer method
B) the scientific method
C) top-down programming
D) whole-into-part programming
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
44
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
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
45
Local variables can be initialized with __________.
A) constants
B) parameter values
C) the result of an arithmetic operation
D) Any of these.
A) constants
B) parameter values
C) the result of an arithmetic operation
D) Any of these.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
46
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
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
47
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
public static double methodA()
{
Double a = 8.5 + 9.5;
Return a;
}
A) 18.0
B) 18
C) 8
D) 19
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
48
When writing 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
A) @comment tag
B) @doc tag
C) @param tag
D) @return tag
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
49
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
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
50
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
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
51
To document the return value of a method you can use a __________.
A) @comment tag
B) @returnValue tag
C) @param tag
D) @return tag
A) @comment tag
B) @returnValue tag
C) @param tag
D) @return tag
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
52
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.
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.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
53
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
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
54
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
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
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
55
A value-returning method must specify __________ as its return type in the method header.
A) an int
B) a double
C) a boolean
D) Any valid data type
A) an int
B) a double
C) a boolean
D) Any valid data type
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
56
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
C) 8.0
D) This is an error.
public static int methodA()
{
Double a = 8.5 + 9.5;
Return a;
}
A) 18.0
B) 18
C) 8.0
D) This is an error.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck