int x = 7;
int y = 3;
if ((x < 10) && (y < 0)) {
System.out.println("Value is: " + x * y);
} else {
System.out.println("Value is: " + x / y);
}
Value is: 2
y is > 0, so the conditional fails and the code in the else block is ran. Therefore, it will print out the value 7 / 3. However, this is integer division, and therefore the resulting output is 2.
Q12: What is returned as a result of the call mystery(“computer”)
public String mystery(String input) {
String output = "";
for (int k = 1; k < input.length(); k = k + 2) {
output += input.substring(k, k + 1);
}
return output;
}
mystery("computer");
optr
The for loop increments by two, iterating over every other element in the list and appending it to the output string.
Q19: The expression !(!(a != b) && (b > 7)) is equal to which of the following?
Simplification: !(!(a != b) && (b > 7)) !((a == b) && (b > 7)) !(a == b) || !(b > 7) (a != b) || (b <= 7)
Notes
Starting in the second half, I began using java, and it helped a lot more.
Example: Q23, best visualised with an extra print statement:
Q23: What will be the contents of animals be as a result of calling manipulate?
private List<String> animals = new ArrayList<>(Arrays.asList("bear", "zebra", "bass", "cat", "koala", "baboon"));
public void manipulate(List<String> animals) {
for (int k = animals.size() - 1; k > 0; k--) {
if (animals.get(k).substring(0, 1).equals("b")) {
animals.add(animals.size() - k, animals.remove(k));
}
System.out.println(animals);
}
}
manipulate(animals)
[bear, baboon, zebra, bass, cat, koala]
[bear, baboon, zebra, bass, cat, koala]
[bear, baboon, zebra, bass, cat, koala]
[bear, baboon, zebra, bass, cat, koala]
[bear, zebra, bass, cat, koala, baboon]
This is much easier to visualize than thinking through individually, although that is what will have to happen on the AP test.
Comments
You are seeing this because your Disqus shortname is not properly set. To configure Disqus, you should edit your
_config.yml
to include either adisqus.shortname
variable.If you do not wish to use Disqus, override the
comments.html
partial for this theme.