# 3.11. Multiple Choice Exercises¶

## 3.11.1. Easy Multiple Choice Questions¶

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

- x is negative
- This will only print if x has been set to a number less than zero. Has it?
- x is zero
- This will only print if x has been set to 0. Has it?
- x is positive
- The first condition is false and x is not equal to zero so the else will execute.

3-11-1: What does the following code print when x has been set to 187?

```
if (x < 0) System.out.println("x is negative");
else if (x == 0) System.out.println("x is zero");
else System.out.println("x is positive");
```

- first case
- This will print if x is greater than or equal 3 and y is less than or equal 2. In this case x is greater than 3 so the first condition is true, but the second condition is false.
- second case
- This will print if x is less than 3 or y is greater than 2.

3-11-2: What is printed when the following code executes and x equals 4 and y equals 3?

```
if (!(x < 3 || y > 2)) System.out.println("first case");
else System.out.println("second case");
```

- A
- Notice that each of the first 4 statements start with an if. What will actually be printed? Try it.
- B
- Each of the first 4 if statements will execute.
- C
- Check this in DrJava.
- D
- Each of the if statements will be executed. So grade will be set to B then C and finally D.
- E
- This will only be true when score is less than 60.

3-11-3: What is the value of grade when the following code executes and score is 80?

```
if (score >= 90) grade = "A";
if (score >= 80) grade = "B";
if (score >= 70) grade = "C";
if (score >= 60) grade = "D";
else grade = "E";
```

- first case
- This will print if either of the two conditions are true. The first isn't true but the second will cause an error.
- second case
- This will print if both of the conditions are false. But, an error will occur when testing the second condition.
- You will get a error because you can't divide by zero.
- The first condition will be false so the second one will be executed and lead to an error since you can't divide by zero.

3-11-4: What is printed when the following code executes and x has been set to zero and y is set to 3?

```
if (x > 0 || (y / x) == 3) System.out.println("first case");
else System.out.println("second case");
```

## 3.11.2. Medium Multiple Choice Questions¶

These problems are similar to those you will see on the AP CS A exam.

- (!c) && (!d)
- NOTing (negating) an OR expression is the same as the AND of the individual values NOTed (negated). See DeMorgans laws.
- (c || d)
- NOTing an OR expression does not result in the same values ORed.
- (c && d)
- You do negate the OR to AND, but you also need to negate the values of c and d.
- !(c && d)
- This would be equivalent to (!c || !d)
- (!c) || (!d)
- This would be equivalent to !(c && d)

3-11-5: Which of the following expressions is equivalent to !(c || d) ?

- x = 0;
- If x was set to 1 then it would still equal 1.
- if (x > 2) x *= 2;
- What happens in the original when x is greater than 2?
- if (x > 2) x = 0;
- If x is greater than 2 it will be set to 0.
- if (x > 2) x = 0; else x *= 2;
- In the original what happens if x is less than 2? Does this give the same result?

3-11-6: Which of the following is equivalent to the code segment below?

```
if (x > 2) x = x * 2;
if (x > 4) x = 0;
```

- x = 0;
- No matter what x is set to originally, the code will reset it to 0.
- if (x > 0) x = 0;
- Even if x is < 0, the above code will set it to 0.
- if (x < 0) x = 0;
- Even if x is > than 0 originally, it will be set to 0 after the code executes.
- if (x > 0) x = -x; else x = 0;
- The first if statement will always cause the second to be executed unless x already equals 0, such that x will never equal -x.
- if (x < 0) x = 0; else x = -1;
- The first if statement will always cause the second to be executed unless x already equals 0, such that x will never equal -x.

3-11-7: Which of the following is equivalent to the code segment below?

```
if (x > 0) x = -x;
if (x < 0) x = 0;
```

- I and III only
- Choice I uses multiple if's with logical ands in the conditions to check that the numbers are in range. Choice II won't work since if you had a score of 94, it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". Choice III uses ifs with else if to make sure that only one conditional is executed.
- II only
- Choice II won't work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". This could have been fixed by using else if instead of just if.
- III only
- III is one of the correct answers. However, choice I is also correct. Choice I uses multiple if's with logical ands in the conditions to check that the numbers are in range. Choice III uses ifs with else if to make sure that the only one conditional is executed.
- I and II only
- Choice II won't work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". This could have been fixed by using else if instead of just if.
- I, II, and III
- Choice II won't work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". This could have been fixed by using else if instead of just if.

3-11-8: At a certain high school students receive letter grades based on the following scale: 93 or above is an A, 84 to 92 is a B, 75 to 83 is a C, and below 75 is an F. Which of the following code segments will assign the correct string to grade for a given integer score?

```
I. if (score >= 93)
grade = "A";
if (score >= 84 && score <=92)
grade = "B";
if (score >=75 && score <= 83)
grade = "C";
if (score < 75)
grade = "F";
II. if (score >= 93)
grade = "A";
if (score >= 84)
grade = "B";
if (score >=75)
grade = "C";
if (score < 75)
grade = "F";
III. if (score >= 93)
grade = "A";
else if (score >= 84)
grade = "B";
else if (score >=75)
grade = "C";
else
grade = "F";
```

## 3.11.3. Hard Multiple Choice Questions¶

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

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

3-11-9: 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)));
}
```

- 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.
- (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?
- (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)
- 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
- None of the above is correct.
- s == m + 5 && s + 3 == 2 * m + 6 is correct

3-11-10: 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);
}
}
```

- (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.
- (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.
- ((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.
- (x < 10 && y > 20) && (x < 15 || x > 18)
- This is the negation of the original statement, thus returning incorrect values.

3-11-11: 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)
```

- first
- This will print, but so will something else.
- first second
- Are you sure about the "second"? This only prints if y is less than 3, and while it was originally, it changes.
- 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.
- 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.
- third
- This will print, but so will something else.

3-11-12: 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");
```

- first
- When you do integer division you get an integer result so y / x == 0 and is not greater than 0.
- 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).
- first second
- Do you see any curly braces? Indention does not matter in Java.
- 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.

3-11-13: What would the following print?

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