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
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/94
Play
Full screen (f)
Deck 5: Methods
1
What is the problem with the code snippet below?
Public static String val()
{
String result = "candy";
Return;
}
) . .
// Using method val()
System.out.println("The value is: " + val());
A) The method val does not have a return value.
B) The method val does not have any parameter variables.
C) The use of val in the System.out.println statement is illegal.
D) The String data type cannot be returned from a method.
Public static String val()
{
String result = "candy";
Return;
}
) . .
// Using method val()
System.out.println("The value is: " + val());
A) The method val does not have a return value.
B) The method val does not have any parameter variables.
C) The use of val in the System.out.println statement is illegal.
D) The String data type cannot be returned from a method.
A
2
Why is hand-tracing, or manually walking through the execution of a method, helpful?
A) It is an effective way to understand a method's subtle aspects
B) It guarantees that the method will compile without errors
C) It makes unit testing unnecessary
D) It enforces the "black-box" concept of method design
A) It is an effective way to understand a method's subtle aspects
B) It guarantees that the method will compile without errors
C) It makes unit testing unnecessary
D) It enforces the "black-box" concept of method design
A
3
What is the problem with the code snippet below?
Public class test02
{
Public static void main(String[] args)
{
System.out.println(cost(10, 4));
}
Public static void cost(int price, int reps)
{
For (int i = 0; i < reps; i++)
{
System.out.print(price);
}
Return;
}
}
A) The method cost is invoked with the wrong arguments
B) The method cost uses uninitialized variables
C) The method cost returns void and cannot be used as an expression in a print statement
D) The method cost must return an integer value
Public class test02
{
Public static void main(String[] args)
{
System.out.println(cost(10, 4));
}
Public static void cost(int price, int reps)
{
For (int i = 0; i < reps; i++)
{
System.out.print(price);
}
Return;
}
}
A) The method cost is invoked with the wrong arguments
B) The method cost uses uninitialized variables
C) The method cost returns void and cannot be used as an expression in a print statement
D) The method cost must return an integer value
C
4
What is stepwise refinement?
A) The process of unit testing
B) The process of breaking complex problems down into smaller, manageable steps
C) The design of pseudocode for black-box methods
D) The use of a temporary implementation of a method that can be improved later
A) The process of unit testing
B) The process of breaking complex problems down into smaller, manageable steps
C) The design of pseudocode for black-box methods
D) The use of a temporary implementation of a method that can be improved later
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
5
When hand-tracing methods, the values for the parameter variables
A) Are the same each time the method is invoked
B) Need not be traced because they are never returned
C) May be undetermined or missing when the method executes
D) Are determined by the arguments supplied in the code that invokes the method
A) Are the same each time the method is invoked
B) Need not be traced because they are never returned
C) May be undetermined or missing when the method executes
D) Are determined by the arguments supplied in the code that invokes the method
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
6
What is the output of the following Java program?
Public class test01
{
Public static void main(String[] args)
{
For (int i = 0; i < 4; i++)
{
System.out.print(myFun(i) + " ");
}
}
Public static int myFun(int perfect)
{
Return ((perfect - 1) * (perfect - 1));
}
}
A) -1 0 1 4
B) 1 0 1 4
C) -1 0 1 4 9
D) 1 4 9 16
Public class test01
{
Public static void main(String[] args)
{
For (int i = 0; i < 4; i++)
{
System.out.print(myFun(i) + " ");
}
}
Public static int myFun(int perfect)
{
Return ((perfect - 1) * (perfect - 1));
}
}
A) -1 0 1 4
B) 1 0 1 4
C) -1 0 1 4 9
D) 1 4 9 16
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
7
Which process helps with identifying the methods that make up a computer program?
A) Black boxing
B) Stepwise refinement
C) Parameter passing
D) Debugging
A) Black boxing
B) Stepwise refinement
C) Parameter passing
D) Debugging
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
8
Which statement about the steps for implementing a method is true?
A) Pseudocode is the first step in the process for implementing a method
B) Pseudocode is the last step in the process for implementing a method
C) Unit testing (testing in isolation) of the implemented method is an important first step
D) Unit testing (testing in isolation) of the implemented method is an important final step
A) Pseudocode is the first step in the process for implementing a method
B) Pseudocode is the last step in the process for implementing a method
C) Unit testing (testing in isolation) of the implemented method is an important first step
D) Unit testing (testing in isolation) of the implemented method is an important final step
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
9
What is the output from the following Java program?
Public class test03
{
Public static void main(String[] args)
{
For (int i = 0; i < 4; i++)
{
System.out.print(myFun(i) + " ");
}
System.out.println();
}
Public static int myFun(int perfect)
{
Perfect = 0;
Return ((perfect - 1) * (perfect - 1));
}
}
A) -1 0 1 4
B) 1 0 1 4
C) 0 0 0 0
D) 1 1 1 1
Public class test03
{
Public static void main(String[] args)
{
For (int i = 0; i < 4; i++)
{
System.out.print(myFun(i) + " ");
}
System.out.println();
}
Public static int myFun(int perfect)
{
Perfect = 0;
Return ((perfect - 1) * (perfect - 1));
}
}
A) -1 0 1 4
B) 1 0 1 4
C) 0 0 0 0
D) 1 1 1 1
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
10
If a method is declared to return void, then which statement below is true?
A) The method cannot return until reaching the end of the method body
B) The method needs a return statement that always returns the integer value zero
C) When the method terminates no value will be returned
D) The method cannot be invoked unless it is in an assignment statement
A) The method cannot return until reaching the end of the method body
B) The method needs a return statement that always returns the integer value zero
C) When the method terminates no value will be returned
D) The method cannot be invoked unless it is in an assignment statement
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
11
The term "Black Box" is used with methods because
A) Only the implementation matters; the specification is not important.
B) Only the specification matters; the implementation is not important.
C) Only the arguments matter; the return value is not important.
D) Only the return value matters; the arguments are not important.
A) Only the implementation matters; the specification is not important.
B) Only the specification matters; the implementation is not important.
C) Only the arguments matter; the return value is not important.
D) Only the return value matters; the arguments are not important.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
12
Which line of code in the Java program below is the recursive invocation of method myFun?
1 public class test03
2 {
3 public static void main(String[] args)
4 {
5 for (int i = 0; i < 4; i++)
6 {
7 System.out.print(myFun(i) + " ");
8 }
9 System.out.println();
10 }
11 public static int myFun(int perfect)
12 {
13 return ((perfect - 1) * (perfect - 1));
14 }
15 }
A) 7
B) 11
C) 13
D) There is no recursive invocation
1 public class test03
2 {
3 public static void main(String[] args)
4 {
5 for (int i = 0; i < 4; i++)
6 {
7 System.out.print(myFun(i) + " ");
8 }
9 System.out.println();
10 }
11 public static int myFun(int perfect)
12 {
13 return ((perfect - 1) * (perfect - 1));
14 }
15 }
A) 7
B) 11
C) 13
D) There is no recursive invocation
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
13
Which of the following is NOT a good practice when developing a computer program?
A) Put as many statements as possible into the main method
B) Document the purpose of each method parameter
C) Decompose a program into many small methods
D) Place code that is used multiple times into a separate method
A) Put as many statements as possible into the main method
B) Document the purpose of each method parameter
C) Decompose a program into many small methods
D) Place code that is used multiple times into a separate method
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
14
A stub method is
A) A short method
B) A method that has been unit tested
C) A method that acts as a placeholder and returns a simple value so another method can be tested
D) A method that is broken down into smaller steps through step-wise refinement
A) A short method
B) A method that has been unit tested
C) A method that acts as a placeholder and returns a simple value so another method can be tested
D) A method that is broken down into smaller steps through step-wise refinement
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
15
The variable name perfect in the method myFun in the code snippet below is used as both a parameter variable and a variable in a nested block within the method. Which statement about this situation is true?
Public static int myFun(int perfect)
{
{
Int perfect = 0;
Return ((perfect - 1) * (perfect - 1));
}
}
A) This multiple declaration of the variable perfect will not compile because the scopes overlap
B) While this is legal and will compile in Java, it is confusing
C) Because the scopes of these variables do not overlap, there is no problem
D) This situation rarely occurs and the compiler always issues a warning
Public static int myFun(int perfect)
{
{
Int perfect = 0;
Return ((perfect - 1) * (perfect - 1));
}
}
A) This multiple declaration of the variable perfect will not compile because the scopes overlap
B) While this is legal and will compile in Java, it is confusing
C) Because the scopes of these variables do not overlap, there is no problem
D) This situation rarely occurs and the compiler always issues a warning
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
16
Which option represents a legal invocation of the method square()?
Public static String square(int
A) {
Return ("Commencing");
}
A) int a = square(4);
B) String a = square("help");
C) double a = square(4.0);
D) String a = square(4);
Public static String square(int
A) {
Return ("Commencing");
}
A) int a = square(4);
B) String a = square("help");
C) double a = square(4.0);
D) String a = square(4);
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
17
Parameter variables should not be changed within the body of a method because
A) This will generate a compiler error
B) This will generate a run-time error
C) It is confusing because it mixes the concept of a parameter with that of a variable
D) It is confusing because parameter variables cannot store values
A) This will generate a compiler error
B) This will generate a run-time error
C) It is confusing because it mixes the concept of a parameter with that of a variable
D) It is confusing because parameter variables cannot store values
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
18
After the keywords "public static", what are the names (in order) of the parts of this method header?
Public static int myFun (double
A)
A) Return type, method name, parameter variable type, parameter variable name
B) Return type, method name, parameter variable name, parameter variable type
C) Method name, method type, parameter variable type, parameter variable name
D) Method name, method type, parameter variable name, parameter variable type
Public static int myFun (double
A)
A) Return type, method name, parameter variable type, parameter variable name
B) Return type, method name, parameter variable name, parameter variable type
C) Method name, method type, parameter variable type, parameter variable name
D) Method name, method type, parameter variable name, parameter variable type
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
19
The purpose of a method that returns void is
A) To satisfy compiler warnings
B) To package a repeated task as a method even though the task does not yield a value
C) To force a value to be returned in case the "return" statement is forgotten
D) To insert a temporary implementation of a method that can be refined later
A) To satisfy compiler warnings
B) To package a repeated task as a method even though the task does not yield a value
C) To force a value to be returned in case the "return" statement is forgotten
D) To insert a temporary implementation of a method that can be refined later
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
20
One advantage of designing methods as black boxes is that
A) many programmers can work on the same project without knowing the internal implementation details of methods.
B) the result that is returned from black-box methods is always the same data type.
C) the implementation of the method is open for everyone to see.
D) there are fewer parameters.
A) many programmers can work on the same project without knowing the internal implementation details of methods.
B) the result that is returned from black-box methods is always the same data type.
C) the implementation of the method is open for everyone to see.
D) there are fewer parameters.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
21
Consider a method named avg, which accepts four numbers as integers and returns their average as a double. Which of the following is a correct call to the method avg?
A) avg(2, 3.14, 3, 5);
B) double average = avg(2, 3, 4, 5);
C) avg();
D) double average = avg("2", "3", "4", "5");
A) avg(2, 3.14, 3, 5);
B) double average = avg(2, 3, 4, 5);
C) avg();
D) double average = avg("2", "3", "4", "5");
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
22
Consider a method named calc, which accepts two numbers as integers and returns their sum as an integer. Which of the following is the correct statement to invoke the method calc?
A) calc(2, 3.14);
B) int sum = calc(2, 3);
C) calc();
D) int sum = calc("2", "3");
A) calc(2, 3.14);
B) int sum = calc(2, 3);
C) calc();
D) int sum = calc("2", "3");
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
23
What is the output of the following code snippet?
Public static int recurrAverage(int num)
{
Int sum = 0;
For (int x = 1; x <= num; x++)
{
Sum = sum + x;
}
Return sum / num;
}
Public static void main(String[] args)
{
System.out.println(recurrAverage(recurrAverage(16)));
}
A) 4
B) 8
C) 12
D) 16
Public static int recurrAverage(int num)
{
Int sum = 0;
For (int x = 1; x <= num; x++)
{
Sum = sum + x;
}
Return sum / num;
}
Public static void main(String[] args)
{
System.out.println(recurrAverage(recurrAverage(16)));
}
A) 4
B) 8
C) 12
D) 16
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
24
What is the error in the following method definition?
Public static int tripler(int numPara)
{
Double result = numPara * 3;
}
A) The method does not return a value.
B) The method returns a value of type double.
C) The method does not modify its parameter variable.
D) The method should be private.
Public static int tripler(int numPara)
{
Double result = numPara * 3;
}
A) The method does not return a value.
B) The method returns a value of type double.
C) The method does not modify its parameter variable.
D) The method should be private.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
25
Consider this method comment. Which of the following options is recommended in your textbook?
/**
Computes the area of a cuboid.
@param width the width of the cuboid
@return the area of the cuboid
*/
Public static double area(double width, double height,
Double length)
{
Double result = width * height * length;
Return result;
}
A) The parameter "width" need not be described.
B) The first line of the comment should be omitted because it is obvious.
C) All of the parameters should be described.
D) The @return clause of the comment should be omitted because it is obvious.
/**
Computes the area of a cuboid.
@param width the width of the cuboid
@return the area of the cuboid
*/
Public static double area(double width, double height,
Double length)
{
Double result = width * height * length;
Return result;
}
A) The parameter "width" need not be described.
B) The first line of the comment should be omitted because it is obvious.
C) All of the parameters should be described.
D) The @return clause of the comment should be omitted because it is obvious.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
26
What is the syntax error in the following method definition?
Public static String parameter(double r)
{
Double result;
Result = 2 * 3.14 * r;
Return result;
}
A) The method does not return the value result.
B) The method does not specify the result return type.
C) The variable result is set but never used.
D) The value that is returned does not match the specified return type.
Public static String parameter(double r)
{
Double result;
Result = 2 * 3.14 * r;
Return result;
}
A) The method does not return the value result.
B) The method does not specify the result return type.
C) The variable result is set but never used.
D) The value that is returned does not match the specified return type.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
27
In a vehicle mileage application, you enter number of miles traveled by a vehicle and the amount of fuel used for 30 consecutive days. From this data the average monthly mileage of the vehicle is calculated. Which of the following should be done to improve the program design?
A) The next time the average monthly mileage is calculated, use copy and paste to avoid making coding errors.
B) Provide the same comment every time you repeat the average monthly mileage calculation.
C) Consider writing a method that returns the average monthly mileage as a double value.
D) Consider writing a method that returns void
A) The next time the average monthly mileage is calculated, use copy and paste to avoid making coding errors.
B) Provide the same comment every time you repeat the average monthly mileage calculation.
C) Consider writing a method that returns the average monthly mileage as a double value.
D) Consider writing a method that returns void
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
28
What is the error in the following method definition?
Public static void findMin(int x, int y)
{
Int min = 0;
If (x < y)
{
Min = x;
}
Else
{
Min = y;
}
}
A) The method returns the maximum instead of the minimum of the two arguments.
B) The method does not return a value.
C) The method returns 0 if the first and second arguments are equal.
D) The method does not specify a type for the second argument.
Public static void findMin(int x, int y)
{
Int min = 0;
If (x < y)
{
Min = x;
}
Else
{
Min = y;
}
}
A) The method returns the maximum instead of the minimum of the two arguments.
B) The method does not return a value.
C) The method returns 0 if the first and second arguments are equal.
D) The method does not specify a type for the second argument.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
29
The Java method Math.round can be used to round numbers. Which of the following code fragments converts a floating-point number to the nearest integer?
A) double f = 4.65;
Int n = (int) Math.round(100 *
B) double f = 4.65;
Int n = (int) Math.round(f);
C) double f = 4.65;
Int n = Math.round(f);
D) double f = 4.65;
Int n = (int) f;
F);
A) double f = 4.65;
Int n = (int) Math.round(100 *
B) double f = 4.65;
Int n = (int) Math.round(f);
C) double f = 4.65;
Int n = Math.round(f);
D) double f = 4.65;
Int n = (int) f;
F);
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
30
In an accounting application, you discover several places where the total profit, a double value, is calculated. Which of the following should be done to improve the program design?
I) The next time the total profit is calculated, use copy and paste to avoid making coding errors.
II) Provide the same comment every time you repeat the total profit calculation.
III) Consider writing a method that returns the total profit as a double value.
A) I
B) II
C) III
D) I, II, and III
I) The next time the total profit is calculated, use copy and paste to avoid making coding errors.
II) Provide the same comment every time you repeat the total profit calculation.
III) Consider writing a method that returns the total profit as a double value.
A) I
B) II
C) III
D) I, II, and III
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
31
What is the output of the following code snippet?
Public static void doubleAmount(int intvalue)
{
Intvalue = 2 * intvalue;
}
Public static void main(String[] args)
{
Int principalAmt = 2000;
System.out.println(doubleAmount(principalAmt));
}
A) 2000
B) 4000
C) 0
D) Compilation error
Public static void doubleAmount(int intvalue)
{
Intvalue = 2 * intvalue;
}
Public static void main(String[] args)
{
Int principalAmt = 2000;
System.out.println(doubleAmount(principalAmt));
}
A) 2000
B) 4000
C) 0
D) Compilation error
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
32
Suppose you need to write a method that calculates the volume of a 3D rectangular solid. Which of the following is the best choice for the declaration of this method?
A)
A) public static void volume(int
B) public static double volume(double w, double h, double l)
C) public static void volume(double w, double h, double l)
D) public static double volume(double w)
A)
A) public static void volume(int
B) public static double volume(double w, double h, double l)
C) public static void volume(double w, double h, double l)
D) public static double volume(double w)
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
33
Which of the following options represents the output of the given code snippet?
Public static int addsub(int a, boolean isSub)
{
Return (isSub ? sub(a) : a + 1);
}
Public static int sub(int
A) {
Return a - 1;
}
Public static void main(String[] args)
{
System.out.println("Sub 5 = " + addsub(5, true) +
", Add 6 = " + addsub(6, false));
}
A) Sub 5 = 6, Add 6 = 6
B) Sub 5 = 4, Add 6 = 7
C) Sub 5 = 6, Add 6 = 6
D) Sub 5 = 4, Add 6 = 6
Public static int addsub(int a, boolean isSub)
{
Return (isSub ? sub(a) : a + 1);
}
Public static int sub(int
A) {
Return a - 1;
}
Public static void main(String[] args)
{
System.out.println("Sub 5 = " + addsub(5, true) +
", Add 6 = " + addsub(6, false));
}
A) Sub 5 = 6, Add 6 = 6
B) Sub 5 = 4, Add 6 = 7
C) Sub 5 = 6, Add 6 = 6
D) Sub 5 = 4, Add 6 = 6
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
34
Which of the following is true about methods?
A) Methods can have only one argument and can return only one return value.
B) Methods can have multiple arguments and can return multiple return values.
C) Methods can have multiple arguments and can return one return value.
D) Methods can have one argument and can return multiple return values.
A) Methods can have only one argument and can return only one return value.
B) Methods can have multiple arguments and can return multiple return values.
C) Methods can have multiple arguments and can return one return value.
D) Methods can have one argument and can return multiple return values.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
35
Which of the following is not legal in a method definition?
A) Multiple parameter variables
B) Parameter variable data types
C) One return value
D) Multiple return values
A) Multiple parameter variables
B) Parameter variable data types
C) One return value
D) Multiple return values
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
36
The Math.ceil method in the Java standard library takes a single value x and returns the smallest integer that is greater than or equal to x. Which of the following is true about Math.ceil(56.75)?
A) The argument is 56.75, and the return value is 57.
B) The argument is 56.75, and the return value is 56.
C) The argument is 57, and the return value is 56.75.
D) The argument is 56, and the return value is 56.75.
A) The argument is 56.75, and the return value is 57.
B) The argument is 56.75, and the return value is 56.
C) The argument is 57, and the return value is 56.75.
D) The argument is 56, and the return value is 56.75.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
37
What are the values of x and y after executing the code snippet below?
Public static void swap(int a, int
A) x = 10 and y = 11
B) {
Int t = a;
A = b;
B = t;
}
Public static void main(String[] args)
{
Int x = 10;
Int y = 11;
Swap(x, y);
}
B) x = 11 and y = 10
C) x = 0 and y = 0
D) x = 11 and y = 11
Public static void swap(int a, int
A) x = 10 and y = 11
B) {
Int t = a;
A = b;
B = t;
}
Public static void main(String[] args)
{
Int x = 10;
Int y = 11;
Swap(x, y);
}
B) x = 11 and y = 10
C) x = 0 and y = 0
D) x = 11 and y = 11
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
38
What are the values of num1 and num2 and result after executing the code snippet below?
Public static int mystery (int firstNum, int secondNum)
{
FirstNum = firstNum * 2;
SecondNum = secondNum * 3;
Return firstNum + secondNum;
}
Public static void main(String[] args)
{
Int num1 = 10;
Int num2 = 11;
Int result = mystery(num1, num2);
}
A) num1 = 20, num2 = 33 and result is 53
B) num1 = 10, num2 = 11 and result is 53
C) num1 = 0, num2 = 0 and result is 0
D) num1 = 20, num2 = 33 and result is 0
Public static int mystery (int firstNum, int secondNum)
{
FirstNum = firstNum * 2;
SecondNum = secondNum * 3;
Return firstNum + secondNum;
}
Public static void main(String[] args)
{
Int num1 = 10;
Int num2 = 11;
Int result = mystery(num1, num2);
}
A) num1 = 20, num2 = 33 and result is 53
B) num1 = 10, num2 = 11 and result is 53
C) num1 = 0, num2 = 0 and result is 0
D) num1 = 20, num2 = 33 and result is 0
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
39
What is the output of the following code snippet?
Public class test04
{
Public static int pow(int base, int power)
{
Int result = 1;
For (int i = 0; i < power; i++)
{
Result = result * base;
}
Return result;
}
Public static void main(String[] args)
{
System.out.println(pow(pow(2, 2), 2));
}
}
A) 4
B) 8
C) 16
D) 32
Public class test04
{
Public static int pow(int base, int power)
{
Int result = 1;
For (int i = 0; i < power; i++)
{
Result = result * base;
}
Return result;
}
Public static void main(String[] args)
{
System.out.println(pow(pow(2, 2), 2));
}
}
A) 4
B) 8
C) 16
D) 32
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
40
What can be used as an argument in a method call?
I) A variable
II) An expression
III) Another method call that returns a value
IV) Another method call that has no return value
A) I only
B) I and II
C) I, II and III
D) III and IV
I) A variable
II) An expression
III) Another method call that returns a value
IV) Another method call that has no return value
A) I only
B) I and II
C) I, II and III
D) III and IV
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
41
What step should you take after implementing a method?
A) Write the pseudocode.
B) Determine the parameter variables.
C) Test the method in isolation.
D) Define the scope of the method.
A) Write the pseudocode.
B) Determine the parameter variables.
C) Test the method in isolation.
D) Define the scope of the method.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
42
Given the following code, what is the output?
Public static String magic(String s)
{
String r = "";
Boolean got = false;
For (int i = 0; i < s.length(); i++)
{
If (Character.isDigit(s.charAt(i)))
{
R = r + s.charAt(i);
Got = true;
}
Else if (got)
{
Return r;
}
}
Return r;
}
Public static void main(String[] args)
{
System.out.println(magic("ABCd45&*31"));
}
A) ABCd
B) 4531
C) 45&*
D) 45
Public static String magic(String s)
{
String r = "";
Boolean got = false;
For (int i = 0; i < s.length(); i++)
{
If (Character.isDigit(s.charAt(i)))
{
R = r + s.charAt(i);
Got = true;
}
Else if (got)
{
Return r;
}
}
Return r;
}
Public static void main(String[] args)
{
System.out.println(magic("ABCd45&*31"));
}
A) ABCd
B) 4531
C) 45&*
D) 45
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
43
What is incorrect in the following code snippet?
Public static void textInRectangle(String str)
{
System.out.println("--------");
System.out.println("|" + str + "|");
System.out.println("--------");
}
Public static void main(String[] args)
{
System.out.println(textInRectangle("Hello there");
}
A) textInRectangle is called with incorrect arguments.
B) textInRectangle should be called in an assignment statement.
C) The return value from textInRectangle is never used.
D) textInRectangle does not return a value; therefore, it cannot be used with System.out.println
Public static void textInRectangle(String str)
{
System.out.println("--------");
System.out.println("|" + str + "|");
System.out.println("--------");
}
Public static void main(String[] args)
{
System.out.println(textInRectangle("Hello there");
}
A) textInRectangle is called with incorrect arguments.
B) textInRectangle should be called in an assignment statement.
C) The return value from textInRectangle is never used.
D) textInRectangle does not return a value; therefore, it cannot be used with System.out.println
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
44
Given the following code, which argument(s) will cause the method to return true?
Public static boolean isIdeal(String s)
{
Int low = 0;
Int high = s.length() - 1;
While (low < high)
{
If (s.charAt(low) != s.charAt(high))
{
Return false;
}
Low++;
High--;
}
Return true;
}
I) isIdeal("civic")
II) isIdeal("level")
III) isIdeal("race car")
IV) isIdeal("rotor")
A) I only
B) I and II only
C) I, II, and III
D) I, II, and IV
Public static boolean isIdeal(String s)
{
Int low = 0;
Int high = s.length() - 1;
While (low < high)
{
If (s.charAt(low) != s.charAt(high))
{
Return false;
}
Low++;
High--;
}
Return true;
}
I) isIdeal("civic")
II) isIdeal("level")
III) isIdeal("race car")
IV) isIdeal("rotor")
A) I only
B) I and II only
C) I, II, and III
D) I, II, and IV
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
45
What is wrong with the following code?
Public static char grade(int score)
{
If (score >= 9)
{
Return 'A';
}
Else if (score >= 8)
{
Return 'B';
}
Else if (score >= 6)
{
Return 'C';
}
Else if (score > 4)
{
Return 'D';
}
Else if (score < 4)
{
Return 'F';
}
}
A) Illegal parameter variable name
B) Invalid parameter variable type
C) No return statement for all logic paths
D) Invalid argument in return statements
Public static char grade(int score)
{
If (score >= 9)
{
Return 'A';
}
Else if (score >= 8)
{
Return 'B';
}
Else if (score >= 6)
{
Return 'C';
}
Else if (score > 4)
{
Return 'D';
}
Else if (score < 4)
{
Return 'F';
}
}
A) Illegal parameter variable name
B) Invalid parameter variable type
C) No return statement for all logic paths
D) Invalid argument in return statements
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
46
Which of the following is the best choice for a return type from a method that prompts users to enter their credit card number exactly as it appears on the card?
A) boolean
B) int
C) String
D) long
A) boolean
B) int
C) String
D) long
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
47
What is wrong with the following code?
Public static String grade(int score)
{
If (score >= 9)
{
Return A;
}
Else if (score >= 8)
{
Return B;
}
Else if (score >= 6)
{
Return C;
}
Else if (score >= 4)
{
Return D;
}
Return F;
}
A) Illegal method name
B) Invalid parameter variable type
C) No return statement for all branches of "if" statement
D) Invalid argument in return statements
Public static String grade(int score)
{
If (score >= 9)
{
Return A;
}
Else if (score >= 8)
{
Return B;
}
Else if (score >= 6)
{
Return C;
}
Else if (score >= 4)
{
Return D;
}
Return F;
}
A) Illegal method name
B) Invalid parameter variable type
C) No return statement for all branches of "if" statement
D) Invalid argument in return statements
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
48
Which of the following is true about method return statements?
A) A method can hold multiple return statements, but only one return statement executes in one method call.
B) A method can hold only one return statement.
C) A method can hold multiple return statements, and multiple return statements can execute in one method call.
D) A method can have maximum of two return statements.
A) A method can hold multiple return statements, but only one return statement executes in one method call.
B) A method can hold only one return statement.
C) A method can hold multiple return statements, and multiple return statements can execute in one method call.
D) A method can have maximum of two return statements.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
49
You need to write a method that calculates the volume for a shape, which depends on the shape's length, width, and height. What should be the parameter variables and their data types for this method?
A) double length, double height
B) double length
C) double length, double height, String depth
D) double width, double length, double height
A) double length, double height
B) double length
C) double length, double height, String depth
D) double width, double length, double height
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
50
What is wrong with the following code?
Public static char grade(int score)
{
If (score >= 9)
{
Return 'A';
}
Else if (score >= 8)
{
Return 'B';
}
Else if (score >= 6)
{
Return 'C';
}
Else if (score >= 4)
{
Return 'D';
}
}
A) Compilation error
B) Invalid parameter variable types
C) No return statement for all possible logic paths
D) Invalid return type
Public static char grade(int score)
{
If (score >= 9)
{
Return 'A';
}
Else if (score >= 8)
{
Return 'B';
}
Else if (score >= 6)
{
Return 'C';
}
Else if (score >= 4)
{
Return 'D';
}
}
A) Compilation error
B) Invalid parameter variable types
C) No return statement for all possible logic paths
D) Invalid return type
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
51
Given the following code, which method call(s) will cause the method to return true?
Public static boolean isIdeal(int year)
{
Return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
I) isIdeal(1600)
II) isIdeal(1700)
III) isIdeal(2000)
IV) isIdeal(2008)
A) I only
B) I and II only
C) I, II, and III
D) I, III, and IV
Public static boolean isIdeal(int year)
{
Return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
I) isIdeal(1600)
II) isIdeal(1700)
III) isIdeal(2000)
IV) isIdeal(2008)
A) I only
B) I and II only
C) I, II, and III
D) I, III, and IV
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
52
A programmer notices that the following code snippet uses the same algorithm for computing cost after taxes, but with different variables, in the two places as shown below, and in several other places in the program. What could be done to improve the program?
Final double TAXRATE1 = 10;
Final double TAXRATE2 = 5.5;
Double subtotal = price * (1 + TAXRATE1) / 100;
Double total = subtotal + shipping * (1 + TAXRATE2) / 100;
A) Declare the tax rates as variables, not constants.
B) Define a method that looks up tax rates for goods and shipping charges.
C) Define a method that prompts the user for an amount and a tax rate, then returns the total amount including the tax.
D) Define a method that computes the cost after taxes from arguments for the cost before taxes and the tax rate.
Final double TAXRATE1 = 10;
Final double TAXRATE2 = 5.5;
Double subtotal = price * (1 + TAXRATE1) / 100;
Double total = subtotal + shipping * (1 + TAXRATE2) / 100;
A) Declare the tax rates as variables, not constants.
B) Define a method that looks up tax rates for goods and shipping charges.
C) Define a method that prompts the user for an amount and a tax rate, then returns the total amount including the tax.
D) Define a method that computes the cost after taxes from arguments for the cost before taxes and the tax rate.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
53
What is wrong with the following code?
Public static double div2(int n1, int n2)
{
If (n2 != 0)
{
Return (double) n1 / n2;
}
}
A) Compilation error
B) Invalid parameter variable types
C) No return statement for all possible logic paths
D) Invalid return type
Public static double div2(int n1, int n2)
{
If (n2 != 0)
{
Return (double) n1 / n2;
}
}
A) Compilation error
B) Invalid parameter variable types
C) No return statement for all possible logic paths
D) Invalid return type
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
54
Which of the following is the best choice for a return type from a method that prompts users to enter their password?
A) char
B) int
C) String
D) void
A) char
B) int
C) String
D) void
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
55
Given the following code, what is the output?
Public class MysteriousClass
{
Public static void main(String[] args)
{
Int i = 20;
Int b = m2(i);
System.out.println(b + i);
}
Public static int m1(int i)
{
Int n = 0;
While (n * n <= i)
{
N++;
}
Return n - 1;
}
Public static int m2(int
A) {
Int b = 0;
For (int n = 0; n < a; n++)
{
Int i = m1(n);
B = b + i;
}
Return b;
}
}
A) 50
B) 60
C) 70
D) 80
Public class MysteriousClass
{
Public static void main(String[] args)
{
Int i = 20;
Int b = m2(i);
System.out.println(b + i);
}
Public static int m1(int i)
{
Int n = 0;
While (n * n <= i)
{
N++;
}
Return n - 1;
}
Public static int m2(int
A) {
Int b = 0;
For (int n = 0; n < a; n++)
{
Int i = m1(n);
B = b + i;
}
Return b;
}
}
A) 50
B) 60
C) 70
D) 80
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
56
What is wrong with the following code?
Public static int count(String s)
{
For (int i = 0; i < s.length(); i++)
{
Int cnt = 0;
If (Character.isDigit(s.charAt(i)))
{
Cnt++;
}
}
Return cnt;
}
A) Illegal return type
B) Invalid parameter variable type
C) No return statement
D) Invalid scope of variable used in return statement
Public static int count(String s)
{
For (int i = 0; i < s.length(); i++)
{
Int cnt = 0;
If (Character.isDigit(s.charAt(i)))
{
Cnt++;
}
}
Return cnt;
}
A) Illegal return type
B) Invalid parameter variable type
C) No return statement
D) Invalid scope of variable used in return statement
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
57
A programmer notices that the following code snippet uses the same algorithm for computing interest earned, but with different variables, in the two places shown below and in several other places in the program. What could be done to improve the program?
Final double RATE1 = 10;
Final double RATE2 = 5.5;
Double interest = investment * RATE1 / 100;
) . .
Balance = balance + balance * RATE2 / 100;
A) Declare the rates as variables, not constants.
B) Define a method that looks up interest rates.
C) Define a method that prompts the user for an amount and a rate of interest, then returns the interest earned.
D) Define a method that computes the interest earned from an amount and a rate of interest.
Final double RATE1 = 10;
Final double RATE2 = 5.5;
Double interest = investment * RATE1 / 100;
) . .
Balance = balance + balance * RATE2 / 100;
A) Declare the rates as variables, not constants.
B) Define a method that looks up interest rates.
C) Define a method that prompts the user for an amount and a rate of interest, then returns the interest earned.
D) Define a method that computes the interest earned from an amount and a rate of interest.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
58
What is the problem with the definition of the following method that calculates and returns the tax due on a purchase amount?
Public static double taxDue(double amount, double taxRate)
{
Double taxDue = 0.0;
TaxDue = amount * taxRate;
}
A) The taxDue method should not be static.
B) The data type of the parameter variables is incorrect.
C) The taxDue calculation is incorrect.
D) The taxDue method does not return a value.
Public static double taxDue(double amount, double taxRate)
{
Double taxDue = 0.0;
TaxDue = amount * taxRate;
}
A) The taxDue method should not be static.
B) The data type of the parameter variables is incorrect.
C) The taxDue calculation is incorrect.
D) The taxDue method does not return a value.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
59
You need to write a method that calculates the shipping cost for an appliance, which depends on the item's 3D dimensions and weight. What should be the inputs and their data types for this method?
A) double size, double weight
B) double size, double weight, double price
C) double size, double weight, double shippingCost
D) double width, double height, double depth, double weight
A) double size, double weight
B) double size, double weight, double price
C) double size, double weight, double shippingCost
D) double width, double height, double depth, double weight
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
60
Which of the following code snippets can be used for defining a method that does not return a value? The method should accept a string and then display the string followed by "Just Let's learn Java!" on a separate line.
A) public static String showString(String someString)
{
System.out.println(someString);
System.out.println("Just Let's learn Java!");
}
B) public static void showString(String someString)
{
System.out.println(someString);
System.out.println("Just Let's learn Java!");
}
C) public static void showString(String someString)
{
System.out.println(someString);
System.out.println("Just Let's learn Java!");
Return someString;
}
D) public static void showString()
{
System.out.println(someString);
System.out.println("Just Let's learn Java!");
}
A) public static String showString(String someString)
{
System.out.println(someString);
System.out.println("Just Let's learn Java!");
}
B) public static void showString(String someString)
{
System.out.println(someString);
System.out.println("Just Let's learn Java!");
}
C) public static void showString(String someString)
{
System.out.println(someString);
System.out.println("Just Let's learn Java!");
Return someString;
}
D) public static void showString()
{
System.out.println(someString);
System.out.println("Just Let's learn Java!");
}
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
61
Assuming that a user enters 45 as the brightness of a lamp, which of the following hand trace tables is valid for the code below?
Public static void main(String[] args)
{
Int brightness = 0;
Scanner in = new Scanner(System.in);
System.out.print(
"Please enter your lamp brightness (in watts): ");
Brightness = in.nextInt();
System.out.println("Lamp is " + getDescription(brightness));
}
Public static String getDescription(int brightness)
{
String description = "";
If (brightness >= 120)
{
Description = "very bright";
If (brightness >= 100)
{
Description = "bright";
}
}
Else
{
Description = "pleasant";
If (brightness <= 50)
{
Description = "dim";
}
}
Return description;
}
A)
![<strong>Assuming that a user enters 45 as the brightness of a lamp, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int brightness = 0; Scanner in = new Scanner(System.in); System.out.print( Please enter your lamp brightness (in watts): ); Brightness = in.nextInt(); System.out.println(Lamp is + getDescription(brightness)); } Public static String getDescription(int brightness) { String description = ; If (brightness >= 120) { Description = very bright; If (brightness >= 100) { Description = bright; } } Else { Description = pleasant; If (brightness <= 50) { Description = dim; } } Return description; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350e_1b5e_ae0b_57d27a4830fe_TB4160_00.jpg)
B)![<strong>Assuming that a user enters 45 as the brightness of a lamp, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int brightness = 0; Scanner in = new Scanner(System.in); System.out.print( Please enter your lamp brightness (in watts): ); Brightness = in.nextInt(); System.out.println(Lamp is + getDescription(brightness)); } Public static String getDescription(int brightness) { String description = ; If (brightness >= 120) { Description = very bright; If (brightness >= 100) { Description = bright; } } Else { Description = pleasant; If (brightness <= 50) { Description = dim; } } Return description; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350e_426f_ae0b_bf0f82647d14_TB4160_00.jpg)
C)![<strong>Assuming that a user enters 45 as the brightness of a lamp, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int brightness = 0; Scanner in = new Scanner(System.in); System.out.print( Please enter your lamp brightness (in watts): ); Brightness = in.nextInt(); System.out.println(Lamp is + getDescription(brightness)); } Public static String getDescription(int brightness) { String description = ; If (brightness >= 120) { Description = very bright; If (brightness >= 100) { Description = bright; } } Else { Description = pleasant; If (brightness <= 50) { Description = dim; } } Return description; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350e_4270_ae0b_49b9dfb87564_TB4160_00.jpg)
D)![<strong>Assuming that a user enters 45 as the brightness of a lamp, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int brightness = 0; Scanner in = new Scanner(System.in); System.out.print( Please enter your lamp brightness (in watts): ); Brightness = in.nextInt(); System.out.println(Lamp is + getDescription(brightness)); } Public static String getDescription(int brightness) { String description = ; If (brightness >= 120) { Description = very bright; If (brightness >= 100) { Description = bright; } } Else { Description = pleasant; If (brightness <= 50) { Description = dim; } } Return description; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350e_6981_ae0b_672429bb4841_TB4160_00.jpg)
Public static void main(String[] args)
{
Int brightness = 0;
Scanner in = new Scanner(System.in);
System.out.print(
"Please enter your lamp brightness (in watts): ");
Brightness = in.nextInt();
System.out.println("Lamp is " + getDescription(brightness));
}
Public static String getDescription(int brightness)
{
String description = "";
If (brightness >= 120)
{
Description = "very bright";
If (brightness >= 100)
{
Description = "bright";
}
}
Else
{
Description = "pleasant";
If (brightness <= 50)
{
Description = "dim";
}
}
Return description;
}
A)
![<strong>Assuming that a user enters 45 as the brightness of a lamp, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int brightness = 0; Scanner in = new Scanner(System.in); System.out.print( Please enter your lamp brightness (in watts): ); Brightness = in.nextInt(); System.out.println(Lamp is + getDescription(brightness)); } Public static String getDescription(int brightness) { String description = ; If (brightness >= 120) { Description = very bright; If (brightness >= 100) { Description = bright; } } Else { Description = pleasant; If (brightness <= 50) { Description = dim; } } Return description; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350e_1b5e_ae0b_57d27a4830fe_TB4160_00.jpg)
B)
![<strong>Assuming that a user enters 45 as the brightness of a lamp, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int brightness = 0; Scanner in = new Scanner(System.in); System.out.print( Please enter your lamp brightness (in watts): ); Brightness = in.nextInt(); System.out.println(Lamp is + getDescription(brightness)); } Public static String getDescription(int brightness) { String description = ; If (brightness >= 120) { Description = very bright; If (brightness >= 100) { Description = bright; } } Else { Description = pleasant; If (brightness <= 50) { Description = dim; } } Return description; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350e_426f_ae0b_bf0f82647d14_TB4160_00.jpg)
C)
![<strong>Assuming that a user enters 45 as the brightness of a lamp, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int brightness = 0; Scanner in = new Scanner(System.in); System.out.print( Please enter your lamp brightness (in watts): ); Brightness = in.nextInt(); System.out.println(Lamp is + getDescription(brightness)); } Public static String getDescription(int brightness) { String description = ; If (brightness >= 120) { Description = very bright; If (brightness >= 100) { Description = bright; } } Else { Description = pleasant; If (brightness <= 50) { Description = dim; } } Return description; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350e_4270_ae0b_49b9dfb87564_TB4160_00.jpg)
D)
![<strong>Assuming that a user enters 45 as the brightness of a lamp, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int brightness = 0; Scanner in = new Scanner(System.in); System.out.print( Please enter your lamp brightness (in watts): ); Brightness = in.nextInt(); System.out.println(Lamp is + getDescription(brightness)); } Public static String getDescription(int brightness) { String description = ; If (brightness >= 120) { Description = very bright; If (brightness >= 100) { Description = bright; } } Else { Description = pleasant; If (brightness <= 50) { Description = dim; } } Return description; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350e_6981_ae0b_672429bb4841_TB4160_00.jpg)
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
62
For a program that reads city names repeatedly from the user and calculates the distance from a company's headquarters, which of the following would be a good design based on stepwise refinement?
A) Write one method that calculates distance randomly.
B) Write one method that reads city name.
C) Write one method that reads city name and another method that calculates distance.
D) Write one method that reads distance and finds city name.
A) Write one method that calculates distance randomly.
B) Write one method that reads city name.
C) Write one method that reads city name and another method that calculates distance.
D) Write one method that reads distance and finds city name.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
63
What is a good rule for deciding the number of statements in a method?
A) It is fine to put as many statements in the method as possible.
B) The method should perform multiple tasks and contain multiple statements.
C) The method should perform only one task and contain just enough statements for the task.
D) The method should contain no more than 3 statements.
A) It is fine to put as many statements in the method as possible.
B) The method should perform multiple tasks and contain multiple statements.
C) The method should perform only one task and contain just enough statements for the task.
D) The method should contain no more than 3 statements.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
64
Given the following method, what do we need to fix?
Public static String getPerformance(char grade)
{
Switch (grade)
{
Case 'A': return "Excellent"; break;
Case 'B': return "Good"; break;
Case 'C': return "Mediocre"; break;
Case 'D': return "Weak"; break;
Case 'F': return "Bad"; break;
}
}
A) Remove all the break statements
B) Add a local boolean variable definition
C) Remove all the break statements and add a return statement before the end of method
D) Remove the switch statement and use and if statement
Public static String getPerformance(char grade)
{
Switch (grade)
{
Case 'A': return "Excellent"; break;
Case 'B': return "Good"; break;
Case 'C': return "Mediocre"; break;
Case 'D': return "Weak"; break;
Case 'F': return "Bad"; break;
}
}
A) Remove all the break statements
B) Add a local boolean variable definition
C) Remove all the break statements and add a return statement before the end of method
D) Remove the switch statement and use and if statement
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
65
Which of the following code snippets can be used for defining a method that does not return a value? The method should accept a string and then display the string followed by "And that's all folks!" on a separate line.
A) public static String displayMessage(String str)
{
System.out.println(str);
System.out.println("And that's all folks!");
}
B) public static void displayMessage(String str)
{
System.out.println(str);
System.out.println("And that's all folks!");
}
C) public static void displayMessage(String str)
{
System.out.println(str);
System.out.println("And that's all folks!");
Return str;
}
D) public static void displayMessage()
{
System.out.println(str);
System.out.println("And that's all folks!");
}
A) public static String displayMessage(String str)
{
System.out.println(str);
System.out.println("And that's all folks!");
}
B) public static void displayMessage(String str)
{
System.out.println(str);
System.out.println("And that's all folks!");
}
C) public static void displayMessage(String str)
{
System.out.println(str);
System.out.println("And that's all folks!");
Return str;
}
D) public static void displayMessage()
{
System.out.println(str);
System.out.println("And that's all folks!");
}
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
66
What is the purpose of writing a stub method?
A) To test another method without writing all the implementation details of a method called by the method being tested
B) To provide a simpler implementation of a complex method
C) To run a unit test for another method
D) To call a method that is being developed
A) To test another method without writing all the implementation details of a method called by the method being tested
B) To provide a simpler implementation of a complex method
C) To run a unit test for another method
D) To call a method that is being developed
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
67
Given the following method, what is the result of getNumber(n)?
Public static double getNumber(double n)
{
Return Math.pow(Math.sqrt(n), 2) - n;
}
A) Always 0 for every argument n
B) Always 2 for every argument n
C) Close to 0, but not always 0
D) Compilation error
Public static double getNumber(double n)
{
Return Math.pow(Math.sqrt(n), 2) - n;
}
A) Always 0 for every argument n
B) Always 2 for every argument n
C) Close to 0, but not always 0
D) Compilation error
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
68
Assuming that a user enters 22 as the price of an item, which of the following hand trace tables is valid for the code below?
Public static void main(String[] args)
{
Int price = 0;
Scanner in = new Scanner(System.in);
System.out.print("Please enter object's price: ");
Price = in.nextInt();
System.out.println("Object price is " + getStatus(price));
}
Public static String getStatus(int price)
{
String status = "";
If (price >= 50)
{
Status = "reasonable";
If (price >= 75)
{
Status = "costly";
}
}
Else
{
Status = "inexpensive";
If (price <= 25)
{
Status = "reasonable";
}
}
Return status;
}
A)![<strong>Assuming that a user enters 22 as the price of an item, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int price = 0; Scanner in = new Scanner(System.in); System.out.print(Please enter object's price: ); Price = in.nextInt(); System.out.println(Object price is + getStatus(price)); } Public static String getStatus(int price) { String status = ; If (price >= 50) { Status = reasonable; If (price >= 75) { Status = costly; } } Else { Status = inexpensive; If (price <= 25) { Status = reasonable; } } Return status; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350d_cd3a_ae0b_5795f4d4af6e_TB4160_00.jpg)
B)![<strong>Assuming that a user enters 22 as the price of an item, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int price = 0; Scanner in = new Scanner(System.in); System.out.print(Please enter object's price: ); Price = in.nextInt(); System.out.println(Object price is + getStatus(price)); } Public static String getStatus(int price) { String status = ; If (price >= 50) { Status = reasonable; If (price >= 75) { Status = costly; } } Else { Status = inexpensive; If (price <= 25) { Status = reasonable; } } Return status; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350d_cd3b_ae0b_0b0cc50d85b2_TB4160_00.jpg)
C)![<strong>Assuming that a user enters 22 as the price of an item, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int price = 0; Scanner in = new Scanner(System.in); System.out.print(Please enter object's price: ); Price = in.nextInt(); System.out.println(Object price is + getStatus(price)); } Public static String getStatus(int price) { String status = ; If (price >= 50) { Status = reasonable; If (price >= 75) { Status = costly; } } Else { Status = inexpensive; If (price <= 25) { Status = reasonable; } } Return status; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350d_f44c_ae0b_b79b22e4d97f_TB4160_00.jpg)
D)![<strong>Assuming that a user enters 22 as the price of an item, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int price = 0; Scanner in = new Scanner(System.in); System.out.print(Please enter object's price: ); Price = in.nextInt(); System.out.println(Object price is + getStatus(price)); } Public static String getStatus(int price) { String status = ; If (price >= 50) { Status = reasonable; If (price >= 75) { Status = costly; } } Else { Status = inexpensive; If (price <= 25) { Status = reasonable; } } Return status; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350d_f44d_ae0b_bbefceae1e47_TB4160_00.jpg)
Public static void main(String[] args)
{
Int price = 0;
Scanner in = new Scanner(System.in);
System.out.print("Please enter object's price: ");
Price = in.nextInt();
System.out.println("Object price is " + getStatus(price));
}
Public static String getStatus(int price)
{
String status = "";
If (price >= 50)
{
Status = "reasonable";
If (price >= 75)
{
Status = "costly";
}
}
Else
{
Status = "inexpensive";
If (price <= 25)
{
Status = "reasonable";
}
}
Return status;
}
A)
![<strong>Assuming that a user enters 22 as the price of an item, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int price = 0; Scanner in = new Scanner(System.in); System.out.print(Please enter object's price: ); Price = in.nextInt(); System.out.println(Object price is + getStatus(price)); } Public static String getStatus(int price) { String status = ; If (price >= 50) { Status = reasonable; If (price >= 75) { Status = costly; } } Else { Status = inexpensive; If (price <= 25) { Status = reasonable; } } Return status; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350d_cd3a_ae0b_5795f4d4af6e_TB4160_00.jpg)
B)
![<strong>Assuming that a user enters 22 as the price of an item, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int price = 0; Scanner in = new Scanner(System.in); System.out.print(Please enter object's price: ); Price = in.nextInt(); System.out.println(Object price is + getStatus(price)); } Public static String getStatus(int price) { String status = ; If (price >= 50) { Status = reasonable; If (price >= 75) { Status = costly; } } Else { Status = inexpensive; If (price <= 25) { Status = reasonable; } } Return status; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350d_cd3b_ae0b_0b0cc50d85b2_TB4160_00.jpg)
C)
![<strong>Assuming that a user enters 22 as the price of an item, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int price = 0; Scanner in = new Scanner(System.in); System.out.print(Please enter object's price: ); Price = in.nextInt(); System.out.println(Object price is + getStatus(price)); } Public static String getStatus(int price) { String status = ; If (price >= 50) { Status = reasonable; If (price >= 75) { Status = costly; } } Else { Status = inexpensive; If (price <= 25) { Status = reasonable; } } Return status; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350d_f44c_ae0b_b79b22e4d97f_TB4160_00.jpg)
D)
![<strong>Assuming that a user enters 22 as the price of an item, which of the following hand trace tables is valid for the code below? Public static void main(String[] args) { Int price = 0; Scanner in = new Scanner(System.in); System.out.print(Please enter object's price: ); Price = in.nextInt(); System.out.println(Object price is + getStatus(price)); } Public static String getStatus(int price) { String status = ; If (price >= 50) { Status = reasonable; If (price >= 75) { Status = costly; } } Else { Status = inexpensive; If (price <= 25) { Status = reasonable; } } Return status; }</strong> A) B) C) D)](https://d2lvgg3v3hfg70.cloudfront.net/TB4160/11ea8b21_350d_f44d_ae0b_bbefceae1e47_TB4160_00.jpg)
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
69
What does the following code do?
Public static int getNumber(int number)
{
Return (int) (Math.random() * number) + 1;
}
Public static void main(String[] args)
{
For (int i = 1; i <= 10; i++)
{
System.out.println(getNumber(6) + " " + getNumber(6));
}
}
A) Generates any 10 random numbers
B) Simulates the throwing of a pair of dice 10 times
C) Generates 10 Fibonacci numbers
D) Generates the same random number 20 times
Public static int getNumber(int number)
{
Return (int) (Math.random() * number) + 1;
}
Public static void main(String[] args)
{
For (int i = 1; i <= 10; i++)
{
System.out.println(getNumber(6) + " " + getNumber(6));
}
}
A) Generates any 10 random numbers
B) Simulates the throwing of a pair of dice 10 times
C) Generates 10 Fibonacci numbers
D) Generates the same random number 20 times
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
70
Which of the following options describes the process of stepwise refinement?
I) Using arguments to pass information to a method
II) Using unit tests to test the behavior of methods
III) Decomposing complex tasks into simpler ones
A) I
B) II
C) III
D) I, II, and III
I) Using arguments to pass information to a method
II) Using unit tests to test the behavior of methods
III) Decomposing complex tasks into simpler ones
A) I
B) II
C) III
D) I, II, and III
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
71
Given the following method:
Public static boolean isMagic(int number)
{
Int j = 2;
Boolean result = false;
While (j <= number / 2)
{
If (number % j == 0)
{
Result = true;
}
J++;
}
Return result;
}
What argument(s) will cause the result of the method to be true?
I) 197
II) 224
III) 231
IV) 341
A) I and II
B) II and III
C) II, III, and IV
D) I and III
Public static boolean isMagic(int number)
{
Int j = 2;
Boolean result = false;
While (j <= number / 2)
{
If (number % j == 0)
{
Result = true;
}
J++;
}
Return result;
}
What argument(s) will cause the result of the method to be true?
I) 197
II) 224
III) 231
IV) 341
A) I and II
B) II and III
C) II, III, and IV
D) I and III
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
72
Given the following method that checks for a valid 5-digit number, what do we need to fix?
Public static boolean isValid(String s)
{
If (s != null && s.length() == 5)
{
Int i = 0;
Boolean b = Character.isDigit(s.charAt(i)) &&
Character.isDigit &&
Character.isDigit) &&
Character.isDigit) &&
Character.isLetter);
}
Else
{
Return false;
}
}
A) Change the parameter variable type to integer
B) Add another local Boolean variable definition
C) Add a return statement inside the "if" branch
D) Add a new Boolean parameter variable
Public static boolean isValid(String s)
{
If (s != null && s.length() == 5)
{
Int i = 0;
Boolean b = Character.isDigit(s.charAt(i)) &&
Character.isDigit &&
Character.isDigit) &&
Character.isDigit) &&
Character.isLetter);
}
Else
{
Return false;
}
}
A) Change the parameter variable type to integer
B) Add another local Boolean variable definition
C) Add a return statement inside the "if" branch
D) Add a new Boolean parameter variable
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
73
An effective technique for understanding the subtle aspects of a method is to:
A) Perform a manual walkthrough.
B) Write stub methods.
C) Use the Java compiler to catch compile-time errors.
D) Write large methods to eliminate the run-time overhead of calling methods.
A) Perform a manual walkthrough.
B) Write stub methods.
C) Use the Java compiler to catch compile-time errors.
D) Write large methods to eliminate the run-time overhead of calling methods.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
74
A temporary method that is used to provide a quick way to test other methods is called:
A) Stub
B) Parameter
C) Caller
D) Assessor
A) Stub
B) Parameter
C) Caller
D) Assessor
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
75
Given the following method, what do we need to fix?
Public static String getPerformance(char grade)
{
If (grade == 'A' || grade == 'B' || grade == 'C' || grade == 'D'
|| grade == 'F')
{
String desc = "";
Switch (grade)
{
Case 'A': desc = "Excellent"; break;
Case 'B': desc = "Good"; break;
Case 'C': desc = "Mediocre"; break;
Case 'D': desc = "Weak"; break;
Case 'F': desc = "Bad"; break;
}
}
Return desc;
}
A) Change the parameter variable type to String
B) Add a local Boolean variable definition to test the input
C) Add return statements inside the switch branches
D) Move the definition of the local variable desc before the if statement
Public static String getPerformance(char grade)
{
If (grade == 'A' || grade == 'B' || grade == 'C' || grade == 'D'
|| grade == 'F')
{
String desc = "";
Switch (grade)
{
Case 'A': desc = "Excellent"; break;
Case 'B': desc = "Good"; break;
Case 'C': desc = "Mediocre"; break;
Case 'D': desc = "Weak"; break;
Case 'F': desc = "Bad"; break;
}
}
Return desc;
}
A) Change the parameter variable type to String
B) Add a local Boolean variable definition to test the input
C) Add return statements inside the switch branches
D) Move the definition of the local variable desc before the if statement
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
76
In the following code snippet, what is the scope of variable b?
Public static void m1()
{
Int i = 0;
Double b = 0;
}
Public static void m2()
{
Double a = b + 1;
}
Public static void main(String[] args)
{
M1();
M2();
}
A) It can be used only in m1.
B) It can be used in user-defined methods m1 and m2.
C) It can be used anywhere in this program.
D) It can be used in many programs.
Public static void m1()
{
Int i = 0;
Double b = 0;
}
Public static void m2()
{
Double a = b + 1;
}
Public static void main(String[] args)
{
M1();
M2();
}
A) It can be used only in m1.
B) It can be used in user-defined methods m1 and m2.
C) It can be used anywhere in this program.
D) It can be used in many programs.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
77
Given the following method, what method call will return true?
Public static boolean isValid(String input)
{
Boolean valid = true;
If (input.length() != 11)
{
Valid = false;
}
Else
{
If (input.charAt(3) != '-' || input.charAt(6) != '-')
{
Valid = false;
}
Else
{
Valid =
Character.isDigit(input.charAt(0)) &&
Character.isDigit(input.charAt(1)) &&
Character.isDigit(input.charAt(2)) &&
Character.isDigit(input.charAt(4)) &&
Character.isDigit(input.charAt(5)) &&
Character.isDigit(input.charAt(7)) &&
Character.isDigit(input.charAt(8)) &&
Character.isDigit(input.charAt(9)) &&
Character.isDigit(input.charAt(10));
}
}
Return valid;
}
A) isValid("123-45-67")
B) isValid("123-456789")
C) isValid("123-45-6789")
D) isValid("ABC-45-6789")
Public static boolean isValid(String input)
{
Boolean valid = true;
If (input.length() != 11)
{
Valid = false;
}
Else
{
If (input.charAt(3) != '-' || input.charAt(6) != '-')
{
Valid = false;
}
Else
{
Valid =
Character.isDigit(input.charAt(0)) &&
Character.isDigit(input.charAt(1)) &&
Character.isDigit(input.charAt(2)) &&
Character.isDigit(input.charAt(4)) &&
Character.isDigit(input.charAt(5)) &&
Character.isDigit(input.charAt(7)) &&
Character.isDigit(input.charAt(8)) &&
Character.isDigit(input.charAt(9)) &&
Character.isDigit(input.charAt(10));
}
}
Return valid;
}
A) isValid("123-45-67")
B) isValid("123-456789")
C) isValid("123-45-6789")
D) isValid("ABC-45-6789")
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
78
Which of the following options represents the output of the given code snippet?
Public static int addsub(int a, boolean isSub)
{
If (isSub) { return sub(a); }
Else {return a + 1; }
}
Public static int sub(int
A) {
Return a - 1;
}
Public static void main(String[] args)
{
System.out.println("Sub 5 = " + addsub(5, false) +
", Add 6 = " + addsub(6, true));
}
A) Sub 5 = 6, Add 6 = 6
B) Sub 5 = 4, Add 6 = 7
C) Sub 5 = 6, Add 6 = 5
D) Sub 5 = 4, Add 6 = 6
Public static int addsub(int a, boolean isSub)
{
If (isSub) { return sub(a); }
Else {return a + 1; }
}
Public static int sub(int
A) {
Return a - 1;
}
Public static void main(String[] args)
{
System.out.println("Sub 5 = " + addsub(5, false) +
", Add 6 = " + addsub(6, true));
}
A) Sub 5 = 6, Add 6 = 6
B) Sub 5 = 4, Add 6 = 7
C) Sub 5 = 6, Add 6 = 5
D) Sub 5 = 4, Add 6 = 6
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
79
For a program that reads three letter grades and calculates an average of those grades, which of the following would be a good design based on stepwise refinement?
A) Write one method that reads three letter grades, converts each letter grade to a number, and calculates the average of the three numbers.
B) Write one method that reads three letter grades, and a second method to convert each letter to a number and calculate the average of the three numbers.
C) Write one method that reads a letter grade and returns the number equivalent, and one method that computes the average of three numbers.
D) Stepwise refinement cannot be applied to this problem.
A) Write one method that reads three letter grades, converts each letter grade to a number, and calculates the average of the three numbers.
B) Write one method that reads three letter grades, and a second method to convert each letter to a number and calculate the average of the three numbers.
C) Write one method that reads a letter grade and returns the number equivalent, and one method that computes the average of three numbers.
D) Stepwise refinement cannot be applied to this problem.
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck
80
What is incorrect in the following code snippet?
Public static void displayBox(String str)
{
System.out.println("--------");
System.out.println(str);
System.out.println("--------");
}
Public static void main(String[] args)
{
System.out.println(displayBox("Hello World"));
}
A) displayBox is called with incorrect arguments.
B) displayBox should be called in an assignment statement.
C) The return value from displayBox is never used.
D) displayBox does not return a value; therefore, it cannot be used with System.out.println
Public static void displayBox(String str)
{
System.out.println("--------");
System.out.println(str);
System.out.println("--------");
}
Public static void main(String[] args)
{
System.out.println(displayBox("Hello World"));
}
A) displayBox is called with incorrect arguments.
B) displayBox should be called in an assignment statement.
C) The return value from displayBox is never used.
D) displayBox does not return a value; therefore, it cannot be used with System.out.println
Unlock Deck
Unlock for access to all 94 flashcards in this deck.
Unlock Deck
k this deck