5.3, 5.4, 5.5 + 1.3, 1.4

Ace your homework & exams now with Quizwiz!

In the code segment below, assume that the int variable n has been properly declared and initialized. The code segment is intended to print a value that is 1 more than twice the value of n. /* missing code */ System.out.print(result); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? int result = 2 * n;result = result + 1; int result = n + 1;result = result * 2; int result = (n + 1) * 2; A: I only B: II only C: III only D: I and III E: II and III

A: I only

while (k < 0) { System.out.print("*"); k++; } Which of the following ranges of initial values for k will guarantee that at least one "*" character is printed? k < 0 k = 0 k > 0 A: I only B: III only C: I and II only D: II and III only E: I, II, and III

A: I only

Consider the following code segment, which is intended to print the digits of the two-digit int number num in reverse order. For example, if num has the value 75, the code segment should print 57. Assume that num has been properly declared and initialized. /* missing code */ System.out.print(onesDigit); System.out.print(tensDigit); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? A: int onesDigit = num % 10; int tensDigit = num / 10; B: int onesDigit = num / 10; int tensDigit = num % 10; C: int onesDigit = 10 / num; int tensDigit = 10 % num; D: int onesDigit = num % 100; int tensDigit = num / 100; E: int onesDigit = num / 100; int tensDigit = num % 100;

A: int onesDigit = num % 10; int tensDigit = num / 10;

The following method is intended to return a string containing the character at position n in the string str. For example, getChar("ABCDE", 2) should return "C". /* missing precondition */ public String getChar(String str, int n) { return str.substring(n, n + 1); } Which of the following is the most appropriate precondition for the method so that it does not throw an exception? A: /* Precondition: 0 < n < str.length() - 1 */ B: /* Precondition: 0 <= n <= str.length() - 1 */ C: /* Precondition: 0 <= n <= str.length() */ D: /* Precondition: n > str.length() */ E: /* Precondition: n >= str.length() */

B: /* Precondition: 0 <= n <= str.length() - 1 */

public class Toy { private String name; private double price; public Toy(String n, double p) { name = n; price = p; } public void raisePrice(double surcharge) // Line 12 { return price + surcharge; // Line 14 } Which of the following changes should be made so that the class definition compiles without error and the method raisePrice works as intended? A: Replace line 14 with surcharge += price;. B: Replace line 14 with price += surcharge;. C: Replace line 14 with return price += surcharge;. D: Replace line 12 with public raisePrice (double surcharge). E: Replace line 12 with public double raisePrice (double surcharge).

B: Replace line 14 with price += surcharge;.

In the code segment below, assume that the int variables a and b have been properly declared and initialized. int c = a; int d = b; c += 3; d--; double num = c; num /= d; Which of the following best describes the behavior of the code segment? A: The code segment stores the value of (a + 3) / b in the variable num. B: The code segment stores the value of (a + 3) / (b - 1) in the variable num. C: The code segment stores the value of (a + 3) / (b - 2) in the variable num. D: The code segment stores the value of (a + 3) / (1 - b) in the variable num. E: The code segment causes a runtime error in the last line of code because num is type double and d is type int.

B: The code segment stores the value of (a + 3) / (b - 1) in the variable num.

public class ItemInventory { private int numItems; public ItemInventory(int num) { numItems = num; } public updateItems(int newNum) { numItems = newNum; } } Which of the following best identifies the reason the class does not compile? A: The constructor header is missing a return type. B: The updateItems method is missing a return type. C: The constructor should not have a parameter. D: The updateItems method should not have a parameter. E: The instance variable numItems should be public instead of private.

B: The updateItems method is missing a return type.

int temp = x; /* missing code */ Which of the following can be used to replace /* missing code */ so that the code segment works as intended? A: x = y; x = temp; B: x = y; y = temp; C: y = x; x = temp; D: y = x; temp = y; E: y = x; temp = x;

B: x = y; y = temp;

int a = 4; int b = 5; a++; b++; int c = a + b; a -= 1; System.out.println(a + c); What is printed when the code segment is executed? A: 9 B: 10 C: 14 D: 15 E: 25

D: 15

int a = 5; int b = 8; int c = 3; System.out.println(a + b / c * 2); What is printed as a result of executing this code? A: 2 B: 6 C: 8 D: 9 E: 14

D: 9

int a = 5; int b = 4; int c = 2; a *= 3; b += a; b /= c; System.out.print(b); What is printed when the code segment is executed? A: 2 B: 4 C: 9 D: 9.5 E: 19

C: 9

Which of the following expressions evaluate to 7 ? 9 + 10 % 12 (9 + 10) % 12 9 - 2 % 12 A: I only B: II only C: I and III D: II and III E: I, II, and III

D: II and III

public class Player { private double score; public getScore() { return score; } // Constructor not shown } The accessor method getScore is intended to return the score of a Player object. Which of the following best explains why the class does not compile? A: The getScore method should be declared as private. B: The getScore method requires a parameter. C: The return type of the getScore method needs to be defined as double. D: The return type of the getScore method needs to be defined as String. E: The return type of the getScore method needs to be defined as void.

C: The return type of the getScore method needs to be defined as double.

public int threeTimesDiff (int num1, int num2) { return 3 * (num1 - num2); } Which, if any, precondition is required so that the method works as intended for all values of the parameters that satisfy the precondition? A: num1 > 0, num2 > 0 B: num1 >= 0, num2 >= 0 C: num1 >= num2 D: num2 >= num1 E: No precondition is required.

C: num1 >= num2

k++; k++; count++; k--; count++; k--; Which of the following best describes the behavior of the code segment? A: The code segment leaves both k and count unchanged. B: The code segment increases both k and count by 2. C: The code segment increases k by 4 and count by 2. D: The code segment leaves k unchanged and increases count by 2. E: The code segment increases k by 2 and leaves count unchanged.

D: The code segment leaves k unchanged and increases count by 2.

num += num; num *= num; Assume that num has been previously declared and initialized to contain an integer value. Which of the following best describes the behavior of the code segment? A: The value of num is two times its original value. B: The value of num is the square its original value. C: The value of num is two times the square of its original value. D: The value of num is the square of twice its original value. E: It cannot be determined without knowing the initial value of num.

D: The value of num is the square of twice its original value.

Consider the following code segment. int x = 5; x += 6 * 2; x -= 3 / 2; What value is stored in x after the code segment executes? A: -1.5 B: 1 C: 9 D: 15.5 E: 16

E: 16

public class Bugs { private int population; public Bugs(int p) { population = p; } public int getPopulation() { return p; } } Which of the following best explains why the getPopulation method does NOT work as intended? A: The getPopulation method should be declared as private. B: The return type of the getPopulation method should be void. C: The getPopulation method should have at least one parameter. D: The variable population is not declared inside the getPopulation method. E: The instance variable population should be returned instead of p, which is local to the constructor.

E: The instance variable population should be returned instead of p, which is local to the constructor.

public class Password { private String password; public Password (String pwd) { password = pwd; } public void reset(String new_pwd) { password = new_pwd; } } Consider the following code segment, which appears in a method in a class other than Password. The code segment does not compile. Password p = new Password("password"); System.out.println("The new password is " + p.reset("password")); Which of the following best identifies the reason the code segment does not compile? A: The code segment attempts to access the private variable password from outside the Password class. B: The new password cannot be the same as the old password. C: The Password class constructor is invoked incorrectly. D: The reset method cannot be called from outside the Password class. E: The reset method does not return a value that can be printed.

E: The reset method does not return a value that can be printed.

public class FishTank { private double numGallons; private boolean saltWater; public FishTank(double gals, boolean sw) { numGallons = gals; saltWater = sw; } public double getNumGallons() { return numGallons; } public boolean isSaltWater() { if (saltWater) { return "Salt Water"; } else { return "Fresh Water"; } } } Which of the following best explains the reason why the class will not compile? A: The variable numGallons is not declared in the getNumGallons method. B: The variable saltWater is not declared in the isSaltWater method. C: The isSaltWater method does not return the value of an instance variable. D: The value returned by the getNumGallons method is not compatible with the return type of the method. E: The value returned by the isSaltWater method is not compatible with the return type of the method.

E: The value returned by the isSaltWater method is not compatible with the return type of the method.

Which of the following statements stores the value 3 in x ? A: int x = 4 / 7; B: int x = 7 / 3; C: int x = 7 / 4; D: int x = 5 % 8; E: int x = 8 % 5;

E: int x = 8 % 5;


Related study sets

chapter 4 neuroscience practice questions

View Set

chapter 11, 12-13 Cognitive Psych.

View Set

AP Psych Unit 4- Sensation and Perception

View Set

4.1.7.1 the Distribution of Income and Wealth

View Set