# Hard Multiple Choice QuestionsΒΆ

These problems are harder than most of those that you will usually see on the AP CS A exam.

- (A) s starts with two or more of the same characters
- It is not neccessary for the adjoining characters to be at the start of the string.
- (B) s contains two or more of the same characters
- The character must be adjoining in order to satisfy the s.charAt(0) == s.charAt(1) portion of the return statement.
- (C) s contains two or more of the same character in a row
- This will return true when s has at least 2 characters in it and at least 2 characters are the same in a row.
- (D) s ends with two or more of the same characters
- It is not neccessary for the adjoining characters to be at the end of the string.
- (E) s.charAt(0) == s.charAt(1)
- This returns true whenever there are at least 2 of the same character in a row in the string. It does this because of the recursive call. So, the first two characters don't have to be the ones that are the same.

5-10-1: The following method will return true if and only if:

```
public boolean check(String s) {
return s.length() >= 2 && (s.charAt(0) ==
s.charAt(1) || check(s.substring(1)));
}
```

- (A) s == (m - 5) && (2 * s + 3) == (m + 3)
- This can't be right because Susan is 5 years older than Matt, so the first part is wrong. It has Susan equal to Matt's age minus 5, which would have Matt older than Susan.
- (B) (s == m - 5) && (s - 3 == 2 * (m - 3))
- This would be true if Susan was 5 years younger than Matt and three years ago she was twice his age. But, how could she be younger than him now and twice his age three years ago?
- (C) (s == (m + 5)) && ((s + 3) == (2 * m + 3))
- This is almost right. It has Susan as 5 years older than Matt now. But the second part is wrong. Multiplication will be done before addition so (2 * m + 3) won't be correct, for in 3 years Susan will be twice as old as Matt. It should be (2 * (m + 3)) or (2 * m + 6)
- (D) s == m + 5 && s + 3 == 2 * m + 6
- Susan is 5 years older than Matt so s == m + 5 should be true and in 3 years she will be twice as old, so s + 3 = 2 * (m + 3) = 2 * m + 6
- (E) None of the above is correct.
- s == m + 5 && s + 3 == 2 * m + 6 is correct

5-10-2: Susan is 5 years older than Matt. Three years from now Susan’s age will be twice Matt’s age. What should be in place of the following condition to solve this problem?

```
for (int s = 1; s <=100; s++) {
for (int m = 1; m <= 100; m++) {
if (condition)
System.out.println("Susan is " + s + " and Matt is " + m);
}
}
```

- (A) (x > 15 && x < 18) && (x > 10)
- This can't be right as it's only checking the x variable, however the original statement can solely depend on the y variable in some cases.
- (B) (y < 20) || (x > 15 && x < 18)
- There's a third condition on x that can affect the output of the statement which is not considered in this solution.
- (C) ((x > 10) || (x > 15 && x < 18)) || (y < 20)
- The commutative property allows the terms to be switched around, while maintaining the value. In this case, the || symbol is used with the commutative property and the statement included the && must stay together to follow the laws of logic.
- (D) (x < 10 && y > 20) && (x < 15 || x > 18)
- This is the negation of the original statement, thus returning incorrect values.

5-10-3: Assuming that x and y have been declared as valid integer values, which of the following is equivalent to this statement?

```
(x > 15 && x < 18) || (x > 10 || y < 20)
```

- (A) first
- This will print, but so will something else.
- (B) first second
- Are you sure about the "second"? This only prints if y is less than 3, and while it was originally, it changes.
- (C) first second third
- Are you sure about the "second"? This only prints if y is less than 3, and while it was originally, it changes.
- (D) first third
- The first will print since x will be greater than 2 and the second won't print since y is equal to 3 and not less than it. The third will always print.
- (E) third
- This will print, but so will something else.

5-10-4: What would the following print?

```
int x = 3;
int y = 2;
if (x > 2) x++;
if (y > 1) y++;
if (x > 2) System.out.print("first ");
if (y < 3) System.out.print("second ");
System.out.print("third");
```

- (A) first
- When you do integer division you get an integer result so y / x == 0 and is not greater than 0.
- (B) second
- The first will not print because integer division will mean that y / x is 0. The second will print since it is not in the body of the if (it would be if there were curly braces around it).
- (C) first second
- Do you see any curly braces? Indention does not matter in Java.
- (D) Nothing will be printed
- This would be true if there were curly braces around the two indented statements. Indention does not matter in Java. If you don't have curly braces then only the first statement following an if is executed if the condition is true.

5-10-5: What would the following print?

```
int x = 3;
int y = 2;
if (y / x > 0)
System.out.print("first ");
System.out.print("second ");
```