Deck 7: User-Defined 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 7: User-Defined Methods
1
When a program executes, the first statement to execute is always the first statement in the main method.
True
2
Void methods use the return statement to return the value 0.
False
3
Methods are also called modules.
True
4
A local identifier is an identifier that is declared within a method or block and that is visible only within that method or block.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
5
In order to use the method isUpperCase of the class Character, the package java.lang must be explicitly imported.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
6
Strings assigned to StringBuffer variables can be altered.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
7
Just like the nesting of loops, Java allows the nesting of methods.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
8
You can use the class String to pass strings as parameters to a method and change the actual parameter.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
9
The return statement must be the last line of the method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
10
The word static is a return type in Java.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
11
Void methods must have at least one parameter.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
12
To use a predefined method, you must know the code in the body of the method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
13
If a formal parameter is a variable of a primitive data type, then after copying the value of the actual parameter, there is no connection between the formal and actual parameter.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
14
An identifier can be declared anywhere including within a class, and outside of every method definition (and every block).
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
15
In Java, return is a reserved word.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
16
The class Math is contained in the package java.math.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
17
Reference variables as parameters allow you to return more than one value from a method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
18
Suppose that the following statement is included in a program.import static java.lang.Math.*;After this statement, a static method of the class Math can be called using just the method name.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
19
An actual parameter is a variable or expression listed in a method call.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
20
Actual and formal parameters have a one-to-one correspondence.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
21
How can a method send a primitive value back to the caller?
A) It cannot send primitive values back to the caller
B) By using the return statement
C) By assigning the value to one of its parameters
D) It can call its caller with the value
A) It cannot send primitive values back to the caller
B) By using the return statement
C) By assigning the value to one of its parameters
D) It can call its caller with the value
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
22
Which of the following is NOT true about return statements?
A) A value-returning method returns its value via the return statement.
B) return statements can be used in void methods to return values.
C) A method can have more than one return statement.
D) Whenever a return statement executes in a method, the remaining statements are skipped and the method exits.
A) A value-returning method returns its value via the return statement.
B) return statements can be used in void methods to return values.
C) A method can have more than one return statement.
D) Whenever a return statement executes in a method, the remaining statements are skipped and the method exits.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
23
public static char methodHeading(int n, double num)Which of the following statements about the method heading in the accompanying figure is NOT true?
A) The method has two parameters.
B) The method cannot be used outside the class.
C) methodHeading is an identifier giving a name to this specific method.
D) It is a value-returning method of type char.
A) The method has two parameters.
B) The method cannot be used outside the class.
C) methodHeading is an identifier giving a name to this specific method.
D) It is a value-returning method of type char.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
24
All the methods defined in a class must have different names.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
25
public static double secret(int first, double second)
{
Double temp; if (second > first)
Temp = first * second;
Else
Temp = first - second; return temp;
}What is the return type of the method in the accompanying figure?
A) public
B) int
C) void
D) double
{
Double temp; if (second > first)
Temp = first * second;
Else
Temp = first - second; return temp;
}What is the return type of the method in the accompanying figure?
A) public
B) int
C) void
D) double
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
26
The signature of a method consists of only its formal parameter list.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
27
Within a method, an identifier used to name a variable in the outer block of the method can be used to name any other variable in an inner block of the method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
28
public int mystery(int x, int y)
{
If (x >= y)
Return x - y;
Else
Return x + y;
}Based on the code in the accompanying figure, what would be the output of the following statement?System.out.println(mystery(8, mystery(2, 1));
A) 5
B) 7
C) 11
D) 13
{
If (x >= y)
Return x - y;
Else
Return x + y;
}Based on the code in the accompanying figure, what would be the output of the following statement?System.out.println(mystery(8, mystery(2, 1));
A) 5
B) 7
C) 11
D) 13
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
29
public int mystery(int x, int y)
{
If (x >= y)
Return x - y;
Else
Return x + y;
}Based on the code in the accompanying figure, what would be the output of the following statement?System.out.println(mystery(8,7));
A) 1
B) 7
C) 8
D) 15
{
If (x >= y)
Return x - y;
Else
Return x + y;
}Based on the code in the accompanying figure, what would be the output of the following statement?System.out.println(mystery(8,7));
A) 1
B) 7
C) 8
D) 15
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
30
What is an actual parameter?
A) The value passed into a method by a method call
B) The value returned by a method
C) The type of the method
D) A variable declared in the method heading
A) The value passed into a method by a method call
B) The value returned by a method
C) The type of the method
D) A variable declared in the method heading
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
31
public static double secret(int first, double second)
{
Double temp; if (second > first)
Temp = first * second;
Else
Temp = first - second; return temp;
}Which of the following is a valid call to the method in the accompanying figure?
A) secret(5, 4.8);
B) secret(int 5, double 4.8);
C) secret(int x, double y);
D) public static int secret(5, 4.8);
{
Double temp; if (second > first)
Temp = first * second;
Else
Temp = first - second; return temp;
}Which of the following is a valid call to the method in the accompanying figure?
A) secret(5, 4.8);
B) secret(int 5, double 4.8);
C) secret(int x, double y);
D) public static int secret(5, 4.8);
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
32
public static double secret(int first, double second)
{
Double temp; if (second > first)
Temp = first * second;
Else
Temp = first - second; return temp;
}Based on the code in the accompanying figure, what would be the output of the following statement?System.out.println(secret(5, 7.0));
A) 5.0
B) 7.0
C) 2.0
D) 35.0
{
Double temp; if (second > first)
Temp = first * second;
Else
Temp = first - second; return temp;
}Based on the code in the accompanying figure, what would be the output of the following statement?System.out.println(secret(5, 7.0));
A) 5.0
B) 7.0
C) 2.0
D) 35.0
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
33
public static double secret(int first, double second)
{
Double temp; if (second > first)
Temp = first * second;
Else
Temp = first - second; return temp;
}What is the name of the method in the accompanying figure?
A) first
B) second
C) secret
D) double
{
Double temp; if (second > first)
Temp = first * second;
Else
Temp = first - second; return temp;
}What is the name of the method in the accompanying figure?
A) first
B) second
C) secret
D) double
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
34
Which of the following statements is NOT true?
A) Reference variables do not directly contain the data.
B) Reference variables contain the address of the memory space where the data is stored.
C) The operator new must always be used to allocate space of a specific type, regardless of the type.
D) A String variable is actually a reference variable of the type String.
A) Reference variables do not directly contain the data.
B) Reference variables contain the address of the memory space where the data is stored.
C) The operator new must always be used to allocate space of a specific type, regardless of the type.
D) A String variable is actually a reference variable of the type String.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
35
Java allows the programmer to declare a variable in the initialization statement of the for statement.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
36
Formal parameters of primitive data types provide ____ between actual parameters and formal parameters.
A) a one-way link
B) a two-way link
C) a three-way link
D) no link
A) a one-way link
B) a two-way link
C) a three-way link
D) no link
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
37
Which modifier is used to specify that a method cannot be used outside a class?
A) public
B) abstract
C) static
D) private
A) public
B) abstract
C) static
D) private
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
38
Two methods are said to have different formal parameter lists if both methods have a different number of formal parameters, or if the data types of the formal parameters are the same.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
39
public static double secret(int first, double second)
{
Double temp; if (second > first)
Temp = first * second;
Else
Temp = first - second; return temp;
}Which of the following is NOT part of the heading of the method in the accompanying figure?
A) public
B) static
C) secret(int first, double second)
D) double temp;
{
Double temp; if (second > first)
Temp = first * second;
Else
Temp = first - second; return temp;
}Which of the following is NOT part of the heading of the method in the accompanying figure?
A) public
B) static
C) secret(int first, double second)
D) double temp;
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
40
public static char methodHeading(int n, double num)Based on the method heading in the accompanying figure, what is the return type of the value returned?
A) static
B) char
C) int
D) num
A) static
B) char
C) int
D) num
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
41
public class scopeRule //Line 1
{ //Line 2
Static double intRate = 0.055; //Line 3
Static String name; //Line 4
Static int t; //Line 5 public static int main(String[] args) //Line 6
{ //Line 7
Int first; //Line 8
Double u, t; //Line 9
String str; //Line 10
//... //Line 11
} //Line 12 public static int first(int x, int y) //Line 13
{ //Line 14
Int t; //Line 15
//... //Line 16
} public static double salary; //Line 17 public static void funcOne(int first, double x) //Line 18
{ //Line 19
Char ch; //Line 20
Int y; //Line 21
//block one //Line 22
{ //Line 23
Int u = 18; //Line 24
//... //Line 25
} //end block one //Line 26
} //Line 27
} //Line 28Which of the following is an example of a local identifier in the example seen in the accompanying figure?
A) intRate (Line 3)
B) name (Line 4)
C) salary (Line 17)
D) t (line 15)
{ //Line 2
Static double intRate = 0.055; //Line 3
Static String name; //Line 4
Static int t; //Line 5 public static int main(String[] args) //Line 6
{ //Line 7
Int first; //Line 8
Double u, t; //Line 9
String str; //Line 10
//... //Line 11
} //Line 12 public static int first(int x, int y) //Line 13
{ //Line 14
Int t; //Line 15
//... //Line 16
} public static double salary; //Line 17 public static void funcOne(int first, double x) //Line 18
{ //Line 19
Char ch; //Line 20
Int y; //Line 21
//block one //Line 22
{ //Line 23
Int u = 18; //Line 24
//... //Line 25
} //end block one //Line 26
} //Line 27
} //Line 28Which of the following is an example of a local identifier in the example seen in the accompanying figure?
A) intRate (Line 3)
B) name (Line 4)
C) salary (Line 17)
D) t (line 15)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
42
The technique to solve a problem by subdividing into smaller problems is known as divide-and- conquer and ____ design approach.
A) top-down
B) bottom-up
C) leveled
D) driver and stub
A) top-down
B) bottom-up
C) leveled
D) driver and stub
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
43
public class scopeRule //Line 1
{ //Line 2
Static double intRate = 0.055; //Line 3
Static String name; //Line 4
Static int t; //Line 5 public static int main(String[] args) //Line 6
{ //Line 7
Int first; //Line 8
Double u, t; //Line 9
String str; //Line 10
//... //Line 11
} //Line 12 public static int first(int x, int y) //Line 13
{ //Line 14
Int t; //Line 15
//... //Line 16
} public static double salary; //Line 17 public static void funcOne(int first, double x) //Line 18
{ //Line 19
Char ch; //Line 20
Int y; //Line 21
//block one //Line 22
{ //Line 23
Int u = 18; //Line 24
//... //Line 25
} //end block one //Line 26
} //Line 27
} //Line 28Which of the following identifiers seen in the accompanying figure is visible in block one?
A) name (Line 4)
B) t (Line 9)
C) local variables of main
D) x (Line 13)
{ //Line 2
Static double intRate = 0.055; //Line 3
Static String name; //Line 4
Static int t; //Line 5 public static int main(String[] args) //Line 6
{ //Line 7
Int first; //Line 8
Double u, t; //Line 9
String str; //Line 10
//... //Line 11
} //Line 12 public static int first(int x, int y) //Line 13
{ //Line 14
Int t; //Line 15
//... //Line 16
} public static double salary; //Line 17 public static void funcOne(int first, double x) //Line 18
{ //Line 19
Char ch; //Line 20
Int y; //Line 21
//block one //Line 22
{ //Line 23
Int u = 18; //Line 24
//... //Line 25
} //end block one //Line 26
} //Line 27
} //Line 28Which of the following identifiers seen in the accompanying figure is visible in block one?
A) name (Line 4)
B) t (Line 9)
C) local variables of main
D) x (Line 13)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
44
int larger(int x, int y)Given the method heading in the accompanying figure, which of the following would be an incorrect demonstration of method overloading?
A) int larger(int a, int b)
B) int larger(int x, int y, int z)
C) double larger(double x, double y)
D) char larger(char x, char y, char z)
A) int larger(int a, int b)
B) int larger(int x, int y, int z)
C) double larger(double x, double y)
D) char larger(char x, char y, char z)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
45
public class scopeRule //Line 1
{ //Line 2
Static double intRate = 0.055; //Line 3
Static String name; //Line 4
Static int t; //Line 5 public static int main(String[] args) //Line 6
{ //Line 7
Int first; //Line 8
Double u, t; //Line 9
String str; //Line 10
//... //Line 11
} //Line 12 public static int first(int x, int y) //Line 13
{ //Line 14
Int t; //Line 15
//... //Line 16
} public static double salary; //Line 17 public static void funcOne(int first, double x) //Line 18
{ //Line 19
Char ch; //Line 20
Int y; //Line 21
//block one //Line 22
{ //Line 23
Int u = 18; //Line 24
//... //Line 25
} //end block one //Line 26
} //Line 27
} //Line 28Which of the following identifiers seen in the accompanying figure is visible in method first?
A) intRate (Line 3)
B) local variables of method funcOne
C) u (Line 24)
D) first (Line 8)
{ //Line 2
Static double intRate = 0.055; //Line 3
Static String name; //Line 4
Static int t; //Line 5 public static int main(String[] args) //Line 6
{ //Line 7
Int first; //Line 8
Double u, t; //Line 9
String str; //Line 10
//... //Line 11
} //Line 12 public static int first(int x, int y) //Line 13
{ //Line 14
Int t; //Line 15
//... //Line 16
} public static double salary; //Line 17 public static void funcOne(int first, double x) //Line 18
{ //Line 19
Char ch; //Line 20
Int y; //Line 21
//block one //Line 22
{ //Line 23
Int u = 18; //Line 24
//... //Line 25
} //end block one //Line 26
} //Line 27
} //Line 28Which of the following identifiers seen in the accompanying figure is visible in method first?
A) intRate (Line 3)
B) local variables of method funcOne
C) u (Line 24)
D) first (Line 8)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
46
int larger(int x, int y)Given the method heading in the accompanying figure, which of the following does NOT demonstrate method overloading?
A) int larger(int x, int y, int z)
B) int larger(char x)
C) int max(int x, int y)
D) double larger(double x, double y)
A) int larger(int x, int y, int z)
B) int larger(char x)
C) int max(int x, int y)
D) double larger(double x, double y)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
47
Which of the following statements about strings is NOT true?
A) When you use the assignment operator to assign a string to a String variable, the computer allocates memory space large enough to store the string.
B) When a string is assigned to a String variable, the string cannot be changed.
C) The class String contains many methods that allow you to change an existing string.
D) A String variable is actually a reference variable.
A) When you use the assignment operator to assign a string to a String variable, the computer allocates memory space large enough to store the string.
B) When a string is assigned to a String variable, the string cannot be changed.
C) The class String contains many methods that allow you to change an existing string.
D) A String variable is actually a reference variable.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
48
public class scopeRule //Line 1
{ //Line 2
Static double intRate = 0.055; //Line 3
Static String name; //Line 4
Static int t; //Line 5 public static int main(String[] args) //Line 6
{ //Line 7
Int first; //Line 8
Double u, t; //Line 9
String str; //Line 10
//... //Line 11
} //Line 12 public static int first(int x, int y) //Line 13
{ //Line 14
Int t; //Line 15
//... //Line 16
} public static double salary; //Line 17 public static void funcOne(int first, double x) //Line 18
{ //Line 19
Char ch; //Line 20
Int y; //Line 21
//block one //Line 22
{ //Line 23
Int u = 18; //Line 24
//... //Line 25
} //end block one //Line 26
} //Line 27
} //Line 28Which of the following identifiers seen in the accompanying figure is NOT visible in method funcOne?
A) intRate (Line 3)
B) u (Line 24)
C) salary (Line 17)
D) first (Line 13)
{ //Line 2
Static double intRate = 0.055; //Line 3
Static String name; //Line 4
Static int t; //Line 5 public static int main(String[] args) //Line 6
{ //Line 7
Int first; //Line 8
Double u, t; //Line 9
String str; //Line 10
//... //Line 11
} //Line 12 public static int first(int x, int y) //Line 13
{ //Line 14
Int t; //Line 15
//... //Line 16
} public static double salary; //Line 17 public static void funcOne(int first, double x) //Line 18
{ //Line 19
Char ch; //Line 20
Int y; //Line 21
//block one //Line 22
{ //Line 23
Int u = 18; //Line 24
//... //Line 25
} //end block one //Line 26
} //Line 27
} //Line 28Which of the following identifiers seen in the accompanying figure is NOT visible in method funcOne?
A) intRate (Line 3)
B) u (Line 24)
C) salary (Line 17)
D) first (Line 13)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
49
The program that tests a method is called a ____ program.
A) tester
B) driver
C) stub
D) debugging
A) tester
B) driver
C) stub
D) debugging
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
50
public class scopeRule //Line 1
{ //Line 2
Static double intRate = 0.055; //Line 3
Static String name; //Line 4
Static int t; //Line 5 public static int main(String[] args) //Line 6
{ //Line 7
Int first; //Line 8
Double u, t; //Line 9
String str; //Line 10
//... //Line 11
} //Line 12 public static int first(int x, int y) //Line 13
{ //Line 14
Int t; //Line 15
//... //Line 16
} public static double salary; //Line 17 public static void funcOne(int first, double x) //Line 18
{ //Line 19
Char ch; //Line 20
Int y; //Line 21
//block one //Line 22
{ //Line 23
Int u = 18; //Line 24
//... //Line 25
} //end block one //Line 26
} //Line 27
} //Line 28Which of the following identifiers seen in the accompanying figure is visible in main?
A) t (Line 5)
B) salary (Line 17)
C) local variables of method funcOne
D) All identifiers are visible in main.
{ //Line 2
Static double intRate = 0.055; //Line 3
Static String name; //Line 4
Static int t; //Line 5 public static int main(String[] args) //Line 6
{ //Line 7
Int first; //Line 8
Double u, t; //Line 9
String str; //Line 10
//... //Line 11
} //Line 12 public static int first(int x, int y) //Line 13
{ //Line 14
Int t; //Line 15
//... //Line 16
} public static double salary; //Line 17 public static void funcOne(int first, double x) //Line 18
{ //Line 19
Char ch; //Line 20
Int y; //Line 21
//block one //Line 22
{ //Line 23
Int u = 18; //Line 24
//... //Line 25
} //end block one //Line 26
} //Line 27
} //Line 28Which of the following identifiers seen in the accompanying figure is visible in main?
A) t (Line 5)
B) salary (Line 17)
C) local variables of method funcOne
D) All identifiers are visible in main.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck