1 A
value is unchanged before it is printed the first time, so the first number output is 15. value is incremented after the call to println and the loop stops when value == 28 so the last number output is 27.
2 A
Case I: The && operation evaluates to true if and only if both operands are true. The condition in Case I is evaluated as (bonusOne && bonusTwo) && bonusThree. For the condition to evaluate to true all 3 variables must be true. This correctly implements the intended condition.
Case II: The || operation is true if at least 1 operand is true. The condition in Case II evaluates to true if at least 1 of the variables is true, which is not the intent.
Case III: The conditions are evaluated independently. It is possible for grade to be incremented by 0, 5, 10 or 15 points depending on the values of the variables. This is not the intent.
3 D
Interchanging, often called swapping, the values of at 2 positions in an array, or in an ArrayList requires the use of a variable to store the value that would otherwise be lost when one value in the array is replaced with the other.
Answers A and B fail to use a temporary variable. Answer C uses a temporary variable but stores the wrong value. Answer E stores the correct variable but does not use the temporary variable.
Answer D correctly stores one of the values in the temporary variable, overwrites the original copy then retrieves the temporary copy.
4 C
The Quick Reference contains documentation for each ArrayList method called. The list after each step is below.
[A]
[A, B]
[A, B, C]
[D, A, B, C]
[D, A, B]
[E, D, A, B]
5 A
A superclass contains fields and methods that are used by all of its subclasses. The term superclass is not intended to imply importance or complexity, but rather indicates that the superclass is above a subclass in a class hierarchy.
In the GridWorld Case Study, the Actor class is a superclass for subclasses such as Bug, Rock and Critter. Actor contains methods common to all its subclasses such as getLocation and getGrid.
Answers B and C incorrectly refer to the complexity of a superclass. Answer D suggests that data should be public, which is not only unnecessary but also violates the principle of encapsulation.
Answer E states that the superclass should provide the most specific details; however, a superclass is actually less specific than its subclasses. For example, an Actor could be a Bug, a Critter or another subclass. The subclass is can contain details specific to itself. For example, Critter contains the method getMoveLocations.
6 A
If k is initialized to 1, the outer loop will never run, so the inner loop will never run, so Hello will not be printed at all.
7 D
If k is initialized to n, the outer loop will run n – 1 times. (If p was initialized to 1, the outer loop would run n times.) Each time the outer loop runs, the inner loop will run n – 1 times.
(n – 1) runs of the outer loop multiplied by (n – 1) runs of the inner loop is (n – 1)*(n-1).
8 D
Case I uses a straightforward for loop to traverse the vals. total is incremented by vals[pos] during each run of the loop. After the loop total contains the sum of all values in vals, as intended.
Case II attempts to traverse vals backwards, which would work, except pos is initialized to vals.length. vals.length is not a valid index in vals, so the loop throws an ArrayIndexOutOfBoundsException on its first iteration.
Case III uses a while loop to traverse vals. The loop is exactly equivalent to the for loop from Case I, so it works as intended.
9 C
The loop runs 5 times. Each value of rep and the String printed are shown below.
0: ab
1: bc
2: cd
3: de
4: ef
Everything is printed on the same line, so the output is abbccddeef.
10 D
Tracing the method for 50 iterations of the loop would take too long, so it is important to determine what each variable represents. Each if statement is independent (there are no else if statements, so:
typeA is incremented for each value of k that is a multiple of both 2 and 5.
typeB is incremented for each value of k that is a multiple of 2 (regardless of whether it is also a multiple of 5).
typeC is incremented for each value of k that is a multiple of 5 (regardless of whether it is also a multiple of 2).
The loop is run for values of k from 1 to 50, inclusive. Within that range, there are 5 multiples of both 2 & 5, 25 multiples of 2 and 10 multiples of 5. The method outputs 5 25 10.
11 A
/* expression */ should evaluate to true if and only if the value in nameList at position j is equal to name.
nameList is an ArrayList so the element at position j is accessed as nameList.get(j). The contents of String objects are compared using the equals method, not ==.
The correct comparison is nameList.get(j).equals(name).
12 B
Case I returns n * 10 for all values of n that are multiples of both 3 and 4. It returns n for all other values of n.
In the table of example inputs and outputs, someProcess(3) returns 30. Case I would return 3 because 3 is not a multiple of both 3 and 4. Case I will not produce the intended results.
Case II returns n * 10 for all values of n that are multiples of either 3 or 4. It returns n for all other values of n.
This corresponds exactly with the table given. Each value of n that is a multiple of either 3 or 4 results in a return vale of n * 10. All other values of n result in a return value of n.
Case III is equivalent to Case I. Both conditions must evaluate to true for the method to return n * 10.
13 C 可画框图
x is an integer >=0. x / 10 is x without its least significant (last) digit. x % 10 is the least significant digit itself. The recursive call to mystery is prior to the call to print, so the last digit removed is the first digit printed. The last digit removed is 1. The second to last digit removed is 2 and so on. mystery is just an elaborate way of printing x.
14 D
Case I attempts to calculate the value for yrs as extraMonths % 12 and the value for mos as extraMonths / 12. There are 2 issues with this approach. The assignments are reversed. There are extraMonths / 12 additional years and extraMonths % 12 additional months. Also, Case I fails to take into account the situation in which months + mos exceeds 12.
Case II correctly converts the original age into months as years * 12 + months. It then adds extraMonths to correctly calculate the new age in months. Case II then converts the total number of months into years (totalMonths / 12) and months (totalMonths % 12) and assigns the results to years and months. Case II works as intended.
Case III first computes the total number of months, including the new months as months + extraMonths. Case III correctly determines how many years are represented by those months as totalMonths / 12 and correctly adds the result to years. The total number of months is what remains after converting all groups of 12 months to years, totalMonths % 12. Case III correctly assigns this to months. Case III works as intended.
15 B
inRangeMessage returns “Not in range” if either part of the condition is true; otherwise, it returns “In range”. In other words, inRangeMessage returns “In range” if and only if value >= 0 && value <= 100.
Case I incorrectly returns “In range” if value >= 0 without checking whether value <= 100.
Case II checks each out of range condition separately and returns “Not in range” if either condition is true. If neither condition is true, it correctly returns “In range”. Case II works as intended.
Case III has the same problem as Case I. Case III immediately returns “In range” if value >= 0 without checking the upper bound.
16 C
After the 3 assignment statements, one and three refer to the same instance of SomeClass. two refers to a different instance. The statement one.increment(200) increments the value of num in the object to which both one and three refer. The object to which two refers in unaffected.
Immediately prior to the println statement, one and three refer to the same object inside which num stores 300. two refers to a different object inside which num stores 100.
17 B
sortArray implements a variant of selection sort that sorts the array starting at the end. /* missing code */ controls the inner loop, which is responsible for finding the maximum value from 0 to j. pos starts at j, so the inner loop only needs to traverse the elements from 0 to j – 1. The order in which it does this is irrelevant, but it must cover each element in the range and ignore all elements outside the range.
Answer A incorrectly excludes the element at position 0.
Answer B correctly visits each element in the range and no other elements.
Answer C incorrectly excludes the element at position 0 and incorrectly continues until the end of the array, rather than stopping at j – 1. Although stopping at j would also be acceptable, proceeding farther means that the loop will visit elements from the already sorted part of the array while seeking the minimum.
value is unchanged before it is printed the first time, so the first number output is 15. value is incremented after the call to println and the loop stops when value == 28 so the last number output is 27.
2 A
Case I: The && operation evaluates to true if and only if both operands are true. The condition in Case I is evaluated as (bonusOne && bonusTwo) && bonusThree. For the condition to evaluate to true all 3 variables must be true. This correctly implements the intended condition.
Case II: The || operation is true if at least 1 operand is true. The condition in Case II evaluates to true if at least 1 of the variables is true, which is not the intent.
Case III: The conditions are evaluated independently. It is possible for grade to be incremented by 0, 5, 10 or 15 points depending on the values of the variables. This is not the intent.
3 D
Interchanging, often called swapping, the values of at 2 positions in an array, or in an ArrayList requires the use of a variable to store the value that would otherwise be lost when one value in the array is replaced with the other.
Answers A and B fail to use a temporary variable. Answer C uses a temporary variable but stores the wrong value. Answer E stores the correct variable but does not use the temporary variable.
Answer D correctly stores one of the values in the temporary variable, overwrites the original copy then retrieves the temporary copy.
4 C
The Quick Reference contains documentation for each ArrayList method called. The list after each step is below.
[A]
[A, B]
[A, B, C]
[D, A, B, C]
[D, A, B]
[E, D, A, B]
5 A
A superclass contains fields and methods that are used by all of its subclasses. The term superclass is not intended to imply importance or complexity, but rather indicates that the superclass is above a subclass in a class hierarchy.
In the GridWorld Case Study, the Actor class is a superclass for subclasses such as Bug, Rock and Critter. Actor contains methods common to all its subclasses such as getLocation and getGrid.
Answers B and C incorrectly refer to the complexity of a superclass. Answer D suggests that data should be public, which is not only unnecessary but also violates the principle of encapsulation.
Answer E states that the superclass should provide the most specific details; however, a superclass is actually less specific than its subclasses. For example, an Actor could be a Bug, a Critter or another subclass. The subclass is can contain details specific to itself. For example, Critter contains the method getMoveLocations.
6 A
If k is initialized to 1, the outer loop will never run, so the inner loop will never run, so Hello will not be printed at all.
7 D
If k is initialized to n, the outer loop will run n – 1 times. (If p was initialized to 1, the outer loop would run n times.) Each time the outer loop runs, the inner loop will run n – 1 times.
(n – 1) runs of the outer loop multiplied by (n – 1) runs of the inner loop is (n – 1)*(n-1).
8 D
Case I uses a straightforward for loop to traverse the vals. total is incremented by vals[pos] during each run of the loop. After the loop total contains the sum of all values in vals, as intended.
Case II attempts to traverse vals backwards, which would work, except pos is initialized to vals.length. vals.length is not a valid index in vals, so the loop throws an ArrayIndexOutOfBoundsException on its first iteration.
Case III uses a while loop to traverse vals. The loop is exactly equivalent to the for loop from Case I, so it works as intended.
9 C
The loop runs 5 times. Each value of rep and the String printed are shown below.
0: ab
1: bc
2: cd
3: de
4: ef
Everything is printed on the same line, so the output is abbccddeef.
10 D
Tracing the method for 50 iterations of the loop would take too long, so it is important to determine what each variable represents. Each if statement is independent (there are no else if statements, so:
typeA is incremented for each value of k that is a multiple of both 2 and 5.
typeB is incremented for each value of k that is a multiple of 2 (regardless of whether it is also a multiple of 5).
typeC is incremented for each value of k that is a multiple of 5 (regardless of whether it is also a multiple of 2).
The loop is run for values of k from 1 to 50, inclusive. Within that range, there are 5 multiples of both 2 & 5, 25 multiples of 2 and 10 multiples of 5. The method outputs 5 25 10.
11 A
/* expression */ should evaluate to true if and only if the value in nameList at position j is equal to name.
nameList is an ArrayList so the element at position j is accessed as nameList.get(j). The contents of String objects are compared using the equals method, not ==.
The correct comparison is nameList.get(j).equals(name).
12 B
Case I returns n * 10 for all values of n that are multiples of both 3 and 4. It returns n for all other values of n.
In the table of example inputs and outputs, someProcess(3) returns 30. Case I would return 3 because 3 is not a multiple of both 3 and 4. Case I will not produce the intended results.
Case II returns n * 10 for all values of n that are multiples of either 3 or 4. It returns n for all other values of n.
This corresponds exactly with the table given. Each value of n that is a multiple of either 3 or 4 results in a return vale of n * 10. All other values of n result in a return value of n.
Case III is equivalent to Case I. Both conditions must evaluate to true for the method to return n * 10.
13 C 可画框图
x is an integer >=0. x / 10 is x without its least significant (last) digit. x % 10 is the least significant digit itself. The recursive call to mystery is prior to the call to print, so the last digit removed is the first digit printed. The last digit removed is 1. The second to last digit removed is 2 and so on. mystery is just an elaborate way of printing x.
14 D
Case I attempts to calculate the value for yrs as extraMonths % 12 and the value for mos as extraMonths / 12. There are 2 issues with this approach. The assignments are reversed. There are extraMonths / 12 additional years and extraMonths % 12 additional months. Also, Case I fails to take into account the situation in which months + mos exceeds 12.
Case II correctly converts the original age into months as years * 12 + months. It then adds extraMonths to correctly calculate the new age in months. Case II then converts the total number of months into years (totalMonths / 12) and months (totalMonths % 12) and assigns the results to years and months. Case II works as intended.
Case III first computes the total number of months, including the new months as months + extraMonths. Case III correctly determines how many years are represented by those months as totalMonths / 12 and correctly adds the result to years. The total number of months is what remains after converting all groups of 12 months to years, totalMonths % 12. Case III correctly assigns this to months. Case III works as intended.
15 B
inRangeMessage returns “Not in range” if either part of the condition is true; otherwise, it returns “In range”. In other words, inRangeMessage returns “In range” if and only if value >= 0 && value <= 100.
Case I incorrectly returns “In range” if value >= 0 without checking whether value <= 100.
Case II checks each out of range condition separately and returns “Not in range” if either condition is true. If neither condition is true, it correctly returns “In range”. Case II works as intended.
Case III has the same problem as Case I. Case III immediately returns “In range” if value >= 0 without checking the upper bound.
16 C
After the 3 assignment statements, one and three refer to the same instance of SomeClass. two refers to a different instance. The statement one.increment(200) increments the value of num in the object to which both one and three refer. The object to which two refers in unaffected.
Immediately prior to the println statement, one and three refer to the same object inside which num stores 300. two refers to a different object inside which num stores 100.
17 B
sortArray implements a variant of selection sort that sorts the array starting at the end. /* missing code */ controls the inner loop, which is responsible for finding the maximum value from 0 to j. pos starts at j, so the inner loop only needs to traverse the elements from 0 to j – 1. The order in which it does this is irrelevant, but it must cover each element in the range and ignore all elements outside the range.
Answer A incorrectly excludes the element at position 0.
Answer B correctly visits each element in the range and no other elements.
Answer C incorrectly excludes the element at position 0 and incorrectly continues until the end of the array, rather than stopping at j – 1. Although stopping at j would also be acceptable, proceeding farther means that the loop will visit elements from the already sorted part of the array while seeking the minimum.