chapter 5 part2

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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

d) Compilation error

89) For the given code snippet, which of the following statements is true? public static double raise(double rate) { double newPayRate = rate * 1.1; return newPayRate; } public static void main(String[] args) { double rate = 40.0; double newPayRate = 0.0; newPayRate = raise(rate); System.out.println("Pay rate: " + newPayRate); } a) The code snippet executes and displays "Pay rate: 40.0" b) The code snippet executes and displays "Pay rate: 44.0" c) The code snippet executes and displays "Pay rate: 0.0" d) There is no output because the program does not compile

) The code snippet executes and displays "Pay rate: 44.0"

101) Given the code below, what is the output of the method call hashIt("#", 6)? public static void hashIt(String s, int d) { if (d <= 1) { System.out.print(d); } else { s = s + "/"; hashIt(s, d - 2); System.out.print(s + d); } } a) 0#///2#//4#/6 b) 0#/2#//4#///6 c) 0/2#//4#///6# d) #/6#//4#///20

a) 0#///2#//4#/6

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

a) It can be used only in m1.

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

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

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

64) 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!"); }

b) public static void showString(String someString) { System.out.println(someString); System.out.println("Just Let's learn Java!"); }

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

c) No return statement for all branches of "if" statement

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

c) No return statement for all possible logic paths

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

c) String

90) What is the output of the following code snippet? public static int doIt(int a, int prv1, int nxt1) { int prv = a - prv1; int nxt = a + nxt1; return prv; } public static void main(String[] args) { int a = 100; int b = 100; int c = 100; b = doIt(a, b, c); System.out.println("b = " + b + ", c = " + c); } a) b = 100, c = 101 b) b = 99, c = 101 c) b = 0, c = 100 d) b = 0, c = 101

c) b = 0, c = 100

75) 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")

c) isValid("123-45-6789")

102) Which of the following method declarations would be appropriate for a method that compares 2 numbers? The method body is shown here: { return num1 > num2; } a) public static void compare(int num1, int num2) b) public static int compare(int num1, int num2) c) public static boolean compare(int num1, int num2) d) public static double compare(int num1, int num2)

c) public static boolean compare(int num1, int num2)

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

d) 45

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

d) I, II, and IV

95) Given the method below, what is the output of the method call is div(10)? public static void div(int n) { if (n > 2) { div(n % 3); } System.out.print(n / 3 + " "); } a) 0 10 b) 3 c) 0 3 d) 10

c) 0 3

91) What is the output of the following code snippet? public static int blackBox(int a) { int val; if (a <= 0) { val = 1; } else { val = a + blackBox(a - 2); } return val; } public static void main(String[] args) { System.out.println(blackBox(4)); } a) 4 b) 1 c) 7 d) 2

c) 7

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

c) 70

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

d) I, III, and IV

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

d) Invalid scope of variable used in return statement

87) Which of the following is correct about a local variable? a) It is declared before all the methods in a program. b) It is visible to all the methods declared after it. c) It is declared exclusively in the main method. d) It is declared within the scope of any method.

d) It is declared within the scope of any method.

74) 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) Brightness Description 0 "" 45 "pleasant" "dim" b) Brightness Description 0 "very bright" 45 "bright" c) Brightness Description 0 "" 45 "bright" "pleasant" d) Brightness Description 0 "bright" 45 "pleasant" Answer: a

a) Brightness Description 0 "" 45 "pleasant" "dim"

73) 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) Price Status 0 "" 22 "inexpensive" "reasonable" b) Price Status 0 "inexpensive" 22 "reasonable" c) Price Status 0 "" 22 "reasonable" "costly" d) Price Status 0 "reasonable" 22 "costly" Answer: a

a) Price Status 0 "" 22 "inexpensive" "reasonable

94) What is the output if the method call is testMyVal(6) in the following code snippet? public static void testMyVal(int nval) { if (nval > 0) { testMyVal(nval - 2); } System.out.print(nval + " "); } a) 0 2 4 6 b) 0 0 0 0 c) 6 6 6 6 d) 6 4 2 0 Answer: a

a) 0 2 4 6

67) 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!"); }

b) public static void displayMessage(String str) { System.out.println(str); System.out.println("And that's all folks!"); }

97) Given the method below, what is the result of the method call recstr("#", 5)? public static void recstr(String s, int d) { if (d <= 1) { System.out.print (d); } else { s = s + "/"; recstr(s, d - 2); System.out.print (s + d); } } a) 1//#3/#5 b) 1#//3#/5 c) 1##/#/5 d) 0##/#/5

b) 1#//3#/5

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

b) Simulates the throwing of a pair of dice 10 times

100) Based on the code snippet below, which of the following statements is correct? public static void recursiveMethod(int count) { recursiveMethod(count + 2); System.out.println(count); } public static void main(String[] args) { recursiveMethod(1); } a) The code snippet gives a compilation error as the recursiveMethod method cannot call itself. b) The code snippet executes and infinitely recurses but does not print anything. c) The code snippet executes and displays 1. d) The code snippet executes and does not produce any output.

b) The code snippet executes and infinitely recurses but does not print anything.

93) Based on the code snippet, which of the following statements is correct? public static void reoccur(int count) { System.out.println(count); reoccur(count + 1); } public static void main(String[] args) { reoccur(1); } a) The code snippet gives a compilation error as the reoccur method cannot call itself. b) The code snippet executes and infinitely recurses, displaying 1, 2, 3, 4, and so on. c) The code snippet executes and displays 1. d) The code snippet executes and does not produce any output

b) The code snippet executes and infinitely recurses, displaying 1, 2, 3, 4, and so on.

92) What is the output of the following code snippet? public static int fun(int x) { int returnValue = 0; if (x > 5) { returnValue = x; } else { returnValue = fun(2 * x); } return returnValue; } public static void main(String[] args) { System.out.println("fun(2) = " + fun(2)); } a) fun(2) = 4 b) fun(2) = 8 c) fun(2) = 16 d) fun(2) = 32

b) fun(2) = 8

98) Which method header is appropriate for this method body? { if (num1 < num2) { return num1; } return num2; } a) public static void comp(int num1, int num2) b) public static int comp(int num1, int num2) c) public static boolean comp(int num1, int num2) d) public static int comp(int num1, double num2)

b) public static int comp(int num1, int num2)

76) 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(s.charAt(i + 1) && Character.isDigit(s.charAt(i + 2)) && Character.isDigit(s.charAt(i + 3)) && Character.isLetter(s.charAt(i + 4)); } 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

c) Add a return statement inside the "if" branch

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

c) II, III, and IV

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

c) III

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

c) No return statement for all possible logic paths

86) What is the output of the following code snippet? public static int assignPriority(int priority) { return priority + 2; } public static void main(String[] args) { int priority = assignPriority(3); System.out.println("Priority: " + priority); } a) Priority: 2 b) Priority: 3 c) Priority: 5 d) There is no output because the program does not compile

c) Priority: 5

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

c) Remove all the break statements and add a return statement before the end of method

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

c) String

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

c) Sub 5 = 6, Add 6 = 5

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

c) Test the method in isolation.

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

c) The method should perform only one task and contain just enough statements for the task.

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

c) Write one method that reads a letter grade and returns the number equivalent, and one method that computes the average of three numbers.

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

c) Write one method that reads city name and another method that calculates distance.

99) What is the output of the following code snippet? public static int someMethod(int x) { int result = 0; if (x > 10) { result = x; } else { result = someMethod(4 * x); } return result; } public static void main(String[] args) { System.out.println("someMethod(2) = " + someMethod(2)); } a) someMethod (2) = 8 b) someMethod (2) = 16 c) someMethod (2) = 32 d) someMethod (2) = 64

c) someMethod (2) = 32

96) Which of the following code snippets returns the factorial of a given number? (Hint: Factorial of 5 = 5! = 1 * 2 * 3 * 4 * 5 = 120) a) public static int factorial(int num) { return num * factorial(num - 1); } b) public static int factorial(int num) { if (num == 1) { return 1; } return num * factorial(num); } c) public static int factorial(int num) { if (num == 1) { return 1; } System.out.println(num * factorial(num - 1)); } d) public static int factorial(int num) { if (num == 1) { return 1; } return num * factorial(num - 1); } Answer: d

d) public static int factorial(int num) { if (num == 1) { return 1; } return num * factorial(num - 1); }

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

d) Move the definition of the local variable desc before the if statement

88) What is the output of the following code snippet? public static void main(String[] args) { int gvar = 0; gvar = gvar + 10; if (gvar > 0) { int lvar = gvar + 1; } System.out.println(lvar); } a) 1 b) 10 c) 11 d) No output due to compilation error

d) No output due to compilation error

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

d) The taxDue method does not return a value.

66) 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 Answer: d

d) displayBox does not return a value; therefore, it cannot be used with System.out.println

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

d) double width, double height, double depth, double weight

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

d) textInRectangle does not return a value; therefore, it cannot be used with System.out.println


Kaugnay na mga set ng pag-aaral

Battles - Texas Revolution (Cause/Effect)

View Set

Marketing Ch. 16 - two, Marketing Ch. 14 - one, Marketing Ch. 13 - two, Marketing Ch. 13 - one, Marketing Ch. 12 - two, Marketing Ch. 12 - one

View Set

Chapter 19 19.2 The advantages and disadvantages fo partnership businesses

View Set

11/12 Themes - Sejarah Indonesia - Indonesian History

View Set