chapter 5 part1

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

a) 4

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.

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

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

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

a) This multiple declaration of the variable perfect will not compile because the scopes overlap

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

b) 1 0 1 4

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

b) Only the specification matters; the implementation is not important

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

b) Sub 5 = 4, Add 6 = 7

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

b) To package a repeated task as a method even though the task does not yield a value

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

b) double average = avg(2, 3, 4, 5);

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

b) int sum = calc(2, 3)

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

c) All of the parameters should be described.

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

c) When the method terminates no value will be returned

29) Which of the following is the correct header for a method definition named calcSum that accepts four int arguments and returns a double? a) public static double calcSum() b) public static calcSum(int a, int b, int c, int d) c) public static double calcSum(int a, int b, int c, int d) d) public static int calcSum(double a, double b, double c, double d)

c) public static double calcSum(int a, int b, int c, int d)

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

d) 1 1 1 1

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

d) Multiple return values

45) What is the output of the given code snippet? public static int addsub(int a, int sub, int add) { sub = a - 1; return sub; add = a + 1; return add; } public static void main(String[] args) { int a = 5; int b = 0; int c = 0; addsub(a, b, c); System.out.println("Subtract = " + b + ", Add = " + c); } a) Subtract = 6, Add = 6 b) Subtract = 4, Add = 5 c) Subtract = 4, Add = 0 d) No output, compilation error

d) No output, compilation error

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

a) Return type, method name, parameter variable type, parameter variable name

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

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

a) The method does not return a value.

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

a) The method val does not have a return value.

21. Which of the following statements about variables is true? a) The same variable name can be used in two different methods. b) The same name can be used for two different variables in a single method. c) You should use global variables whenever possible. d) A variable is visible from the point at which it is defined until the end of the program.

a) The same variable name can be used in two different methods.

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

41) What are the values of x and y after executing the code snippet below? public static void swap(int a, int b) { int t = a; a = b; b = t; } public static void main(String[] args) { int x = 10; int y = 11; swap(x, y); } a) x = 10 and y = 11 b) x = 11 and y = 10 c) x = 0 and y = 0 d) x = 11 and y = 11

a) x = 10 and y = 11

27) 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 * f); 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;

b) double f = 4.65; int n = (int) Math.round(f);

1) Which process helps with identifying the methods that make up a computer program? a) Black boxing b) Stepwise refinement c) Parameter passing d) Debugging

b) Stepwise refinement

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

b) The method does not return a value.

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

b) The process of breaking complex problems down into smaller, manageable steps

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

b) num1 = 10, num2 = 11 and result is 53

38) Which of the following is the correct header for a greaterThan method definition that takes two arguments of type double and returns true if the first value is greater than the second value? a) public static int greaterThan(double a, double b) b) public static boolean greaterThan(double a, double b) c) public static double greaterThan(boolean a, boolean b) d) public static boolean greaterThan(double a, b)

b) public static boolean greaterThan(double a, double b)

39) 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) public static void volume(int a) 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)

b) public static double volume(double w, double h, double l)

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

c) 16

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

c) A method that acts as a placeholder and returns a simple value so another method can be tested

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

c) Consider writing a method that returns the average monthly mileage as a double value.

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

c) I, II and III

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

c) III

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

c) It is confusing because it mixes the concept of a parameter with that of a variable

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

c) Methods can have multiple arguments and can return one return value.

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'; } 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

c) No return statement for all logic paths

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

c) The method cost returns void and cannot be used as an expression in a print statement

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

d) Are determined by the arguments supplied in the code that invokes the method

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

d) Compilation error

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

d) Define a method that computes the cost after taxes from arguments for the cost before taxes and the tax rate

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

d) Define a method that computes the interest earned from an amount and a rate of interest.

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

d) String a = square(4);

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

d) The value that is returned does not match the specified return type

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

d) There is no recursive invocation

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

d) Unit testing (testing in isolation) of the implemented method is an important final step

42) Which of the following options represents the output of the code snippet below? public static void doIt(int a, int prv, int nxt) { prv = a - 1; nxt = a + 1; } public static void main(String[] args) { int a = 100; int b = 100; int c = 100; doIt(a, b, c); System.out.println("b = " + b + ", c = " + c); } a) b = 100, c = 101 b) b = 99, c = 100 c) b = 99, c = 101 d) b = 100, c = 100

d) b = 100, c = 100

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

d) double width, double length, double height


Ensembles d'études connexes

ATI 70, 72 osteoporosis and osteoarthritis

View Set

Corporate Finance Non-Math Questions

View Set

Missed Questions MKTG 409 Exam 1

View Set

A&P Lab E: Muscles Operating on the Scapula and Shoulder

View Set

Chapter 2: Family-Centered Community-Based Care (Prep U)

View Set