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
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/50
Play
Full screen (f)
Deck 5: Methods
1
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
B
2
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.
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.
C
3
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
{
Double a = 8.5 + 9.5;
Return a;
}
A) 18.0
B) 18 (as an integer)
C) 8
D) This is an error
A
4
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
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
5
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);
{
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 50 flashcards in this deck.
Unlock Deck
k this deck
6
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
A) method name
B) return type
C) parentheses
D) all of the above are part of a method call
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
7
Which of the following is not a benefit derived from using methods in programming?
A) problems are more easily solved
B) simplifies programs
C) code reuse
D) all of the above are benefits
A) problems are more easily solved
B) simplifies programs
C) code reuse
D) all of the above are benefits
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
8
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
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
9
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
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 50 flashcards in this deck.
Unlock Deck
k this deck
10
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
A) method header
B) return type
C) method body
D) method modifier
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
11
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 50 flashcards in this deck.
Unlock Deck
k this deck
12
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
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
13
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 50 flashcards in this deck.
Unlock Deck
k this deck
14
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
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
15
Functional decomposition is
A) the backbone of the scientific method
B) the process of decomposing functions
C) the process of breaking a problem down into smaller pieces
D) the process of dead plants decomposing and turning back into soil
A) the backbone of the scientific method
B) the process of decomposing functions
C) the process of breaking a problem down into smaller pieces
D) the process of dead plants decomposing and turning back into soil
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
16
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 50 flashcards in this deck.
Unlock Deck
k this deck
17
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 50 flashcards in this deck.
Unlock Deck
k this deck
18
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
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
19
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
A) value-returning
B) void
C) complex
D) local
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
20
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
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
21
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 50 flashcards in this deck.
Unlock Deck
k this deck
22
Which of the following is not a part of the 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 50 flashcards in this deck.
Unlock Deck
k this deck
23
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
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
24
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
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
25
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
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
26
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
A) @comment tag
B) @doc tag
C) @param tag
D) @return tag
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
27
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
A) an int
B) a double
C) a boolean
D) any valid data type
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
28
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 50 flashcards in this deck.
Unlock Deck
k this deck
29
Local variables can be initialized with
A) constants
B) parameter values
C) the results of an arithmetic operation
D) any of the above
A) constants
B) parameter values
C) the results of an arithmetic operation
D) any of the above
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
30
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);
{
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 50 flashcards in this deck.
Unlock Deck
k this deck
31
Which of the following is included in a method call?
A) return type
B) method modifiers
C) parentheses
D) return variable
A) return type
B) method modifiers
C) parentheses
D) return variable
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
32
The process of breaking a problem down into smaller pieces is called
A) functional decomposition
B) scientific method
C) top-down programming
D) whole-into-part
A) functional decomposition
B) scientific method
C) top-down programming
D) whole-into-part
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
33
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
{
Double a = 8.5 + 9.5;
Return a;
}
A) 18.0
B) 18 (as an integer)
C) 8.0
D) This is an error
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
34
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
A) a plan
B) a statement inside a loop
C) a comment
D) a collection of statements that performs a specific task
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
35
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)
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)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
36
This 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 50 flashcards in this deck.
Unlock Deck
k this deck
37
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
A) The @param tag
B) The @comment tag
C) The @return tag
D) The @returnValue tag
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
38
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
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
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
39
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
A) makes problems more easily solved
B) allows for code reuse
C) simplifies programs
D) all of the above
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
40
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 50 flashcards in this deck.
Unlock Deck
k this deck
41
The expression in a return statement can be any expression that has a value.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
42
A parameter variable's scope is the method in which the parameter is declared.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
43
Constants,variables,and the values of expressions may be passed as arguments to a method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
44
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 50 flashcards in this deck.
Unlock Deck
k this deck
45
You must have a return statement in a value-returning method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
46
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 50 flashcards in this deck.
Unlock Deck
k this deck
47
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 50 flashcards in this deck.
Unlock Deck
k this deck
48
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 50 flashcards in this deck.
Unlock Deck
k this deck
49
A value-returning method can return a reference to a non-primitive type.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
50
Only constants and variables may be passed as arguments to methods.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck