# Multiple Choice Exercises¶

## Easier Multiple Choice Questions¶

- 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");
```

- 5 6 7 8 9
- What is i set to in the initialization area?
- 4 5 6 7 8 9 10 11 12
- What is i set to in the initialization area?
- 3 5 7 9 11
- This loop changes i by 1 each time in the change area.
- 3 4 5 6 7 8 9 10 11 12
- The value of i starts at 3 and this loop will execute until i equals 12. The last time through the loop the value of i is 12 at the begininng and then it will be incremented to 13 which stops the loop since 13 is not less than or equal to 12.

3-11-5: What does the following code print?

```
for (int i = 3; i <= 12; i++)
{
System.out.print(i + " ");
}
```

- 9
- This would be true if i started at 0.
- 7
- Note that it stops when i is 9.
- 6
- Since i starts at 3 and the last time through the loop it is 8 the loop executes 8 - 3 + 1 times = 6 times.
- 10
- This would be true if i started at 0 and ended when i was 10. Does it?

3-11-6: How many times does the following method print a `*`

?

```
for (int i = 3; i < 9; i++)
{
System.out.print("*");
}
```

- 5 4 3 2 1
- x is initialized (set) to -5 to start.
- -5 -4 -3 -2 -1
- x is incremented (x++) before the print statement executes.
- -4 -3 -2 -1 0
- x is set to -5 to start but then incremented by 1 so it first prints -4.

3-11-7: What does the following code print?

```
int x = -5;
while (x < 0)
{
x++;
System.out.print(x + " ");
}
```

- 7
- This would be true if it stopped when i was 12, but it loops when i is 12.
- 8
- Note that it stops when i is 13 so 13 - 5 is 8.
- 12
- This would be true if i started at 1.
- 13
- This would be true if i started at 0.

3-11-8: How many times does the following method print a `*`

?

```
for (int i = 5; i <= 12; i++)
{
System.out.print("*");
}
```

- 4
- The loop starts with i = 1 and loops as long as it is less than 5 so i is 1, 2, 3, 4.
- 5
- This would be true if the condition was i <= 5.
- 6
- This would be true if i started at 0 and ended when it reached 6 (i <= 5).

3-11-9: How many times does the following method print a `*`

?

```
for (int i = 1; i < 5; i++)
{
System.out.print("*");
}
```

- 7
- This would be true if i started at 1 and ended when it reached 8.
- 8
- This would be true if the loop ended when i reached 8.
- 9
- This loop starts with i = 0 and continues till it reaches 9 so (9 - 0 = 9).

3-11-10: How many times does the following method print a `*`

?

```
for (int i = 0; i <= 8; i++)
{
System.out.print("*");
}
```

- 4
- This would be true if x started at 1 instead of 0.
- 5
- The loop starts with x = 0 and ends when it reaches 5 so 5 - 0 = 5.
- 6
- This would be true if the condition was x <= 5 instead of x = 5.

3-11-11: How many times does the following method print a `*`

?

```
for (int x = 0; x < 5; x++)
{
System.out.print("*");
}
```

- 6
- This loop starts with x = 2 and continues while it is less than 8 so 8 - 2 = 6.
- 7
- This would be true if the loop ended when x was 9 instead of 8 (x <= 8).
- 8
- This would be true if the loop started with x = 0.

3-11-12: How many times does the following method print a `*`

?

```
for (int x = 2; x < 8; x++)
{
System.out.print("*");
}
```

- 1 2 3 4
- This would be true if x started at 1 and ended when x was 5.
- 1 2 3 4 5
- This would be true if x started at 1.
- 0 1 2 3 4
- This would be true if the loop ended when x was 5.
- 0 1 2 3 4 5
- This loop starts with x = 0 and ends when it reaches 6.

3-11-13: What does the following code print?

```
int x = 0;
while (x <= 5)
{
System.out.print(x + " ");
x++;
}
```

- 3 4 5 6 7 8
- Notice that x isn't changed in the loop.
- 3 4 5 6 7 8 9
- Notice that x isn't changed in the loop.
- 0 1 2 3 4 5 6 7 8
- Notice that x isn't changed in the loop.
- 0 1 2 3 4 5 6 7 8 9
- Notice that x isn't changed in the loop.
- It is an infinite loop
- Since x is never changed in the loop this is an infinite loop.

3-11-14: What does the following code print?

```
int x = 3;
while (x < 9)
{
System.out.print(x + " ");
}
```

## Medium Multiple Choice Questions¶

- (!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-15: 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-16: 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-17: Which of the following is equivalent to the code segment below?

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

- I
- This will loop with i changing from 1 to 5 and then for each i, j will loop from i to 0 printing the value of i and then a new line.
- II
- This will loop i from 0 to 4 and j from 0 to i, neglecting to ouput 5.
- III
- This will loop with i changing from 1 to 4 and j from i to 0.
- IV
- This will loop with i changing from 1 to 5 and j from 0 to i but it will print each value on a different line.
- V
- This will loop with i changing from 0 to 4 and j from 0 to i.

3-11-20: Which of the following code segments will produce the displayed output?

```
1
22
333
4444
55555
I. for (int i = 1; i <= 5; i++) {
for (int j = i; j > 0; j--) {
System.out.print(i);
}
System.out.println();
}
II. for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(i);
}
System.out.println();
}
III. for (int i = 1; i < 5; i++) {
for (int j = i; j > 0; j--) {
System.out.print(i);
}
System.out.println();
}
IV. for (int i = 1; i < 6; i++) {
for (int j = 0; j < i; j++) {
System.out.println(i);
}
}
V. for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(i+1);
}
System.out.println();
}
```

- 0 2 4 6 8 10 12 14 16 18
- This would be correct if we were printing out all of the values of k, not just the ones that have a remainder of 1 when divided by 3.
- 4 16
- This is missing the value 10 (10 divided by 3 does have a remainder of 1).
- 0 6 12 18
- None of these answers have a remainder of 1 when divided by 3.
- 1 4 7 10 13 16 19
- This answer would be correct if k was incremented by 1 instead of 2. K will be 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 in this loop.
- 4 10 16
- This will loop with k having a value of 0 to 18 (it will stop when k = 20). It will print out the value of k followed by a space when the remainder of dividing k by 3 is 1.

3-11-21: What is printed as a result of the following code segment?

```
for (int k = 0; k < 20; k+=2) {
if (k % 3 == 1)
System.out.print(k + " ");
}
```

- I
- This will loop with j from 1 to 5 and k from 5 to j and print out the value of j and a space. So the first time through the loop it will print 1 five times and the next time it will print out 2 four times and so on.
- II
- This will print out each value from 1 to 5 five times.
- III
- This will loop with j from 1 to 5 and k from 1 times.
- IV
- This will loop j from 1 to 5 and k from 1 to 5, printing each number 5 times.
- V
- This loops with j from 1 to 5 and k from j to 5 and prints out the value of k, printing 1 through 5 on the first line, 2 through 5 on the next, and so on.

3-11-22: Which of the following code segments will produce the displayed output?

```
11111
2222
333
44
5
I. for (int j = 1; j <= 5; j++) {
for (int k = 5; k >= j; k--) {
System.out.print(j + " ");
}
System.out.println();
}
II. for (int j = 1; j <= 5; j++) {
for (int k = 5; k >= 1; k--) {
System.out.print(j + " ");
}
System.out.println();
}
III. for (int j = 1; j <= 5; j++) {
for (int k = 1; k <= j; k++) {
System.out.print(j + " ");
}
System.out.println();
}
IV. for (int j = 1; j <= 5; j++) {
for (int k = 1; k <= 5; k++) {
System.out.println(j + " ");
}
}
V. for (int j = 1; j <= 5; j++) {
for (int k = j; k <= 5; k++) {
System.out.print(k + " ");
}
System.out.println();
}
```

- var1 = 0, var2 = 2
- This would be true if the body of the while loop never executed. This would have happened if the while check was if var1 != 0 instead of var2 != 0
- var1 = 1, var2 = 1
- This would be true if the body of the while loop only execued one time, but it executes twice.
- var1 = 3, var2 = -1
- This would be true if the body of the while loop executed 3 times, but it executes twice.
- var1 = 2, var2 = 0
- The loop starts with var1=0 and var2=2. The while checks that var2 isn't 0 and that var1/var2 is greater than or equal to zero (0/2=0) so this is equal to zero and the body of the while loop will execute. The variable var1 has 1 added to it for a new value of 1. The variable var2 has 1 subtracted from it for a value of 1. At this point var1=1 and var2=1. The while condition is checked again. Since var2 isn't 0 and var1/var2 (1/1=1) is >=0 so the body of the loop will execute a second time. The variable var1 has 1 added to it for a new value of 2. The variable var2 has 1 subtracted from it for a value of 0. At this point var1=2 and var2=0. The while condition is checked again. Since var2 is zero the while loop stops and the value of var1 is 2 and var2 is 0.
- The loop won't finish executing because of a division by zero.
- 0/2 won't cause a division by zero. The result is just zero.

3-11-23: What are the values of var1 and var2 after the following code segment is executed and the while loop finishes?

```
int var1 = 0;
int var2 = 2;
while ((var2 != 0) && ((var1 / var2) >= 0)) {
var1 = var1 + 1;
var2 = var2 - 1;
}
```

## Hard Multiple Choice Questions¶

- (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-24: 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-25: 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-26: What would the following print?

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

## More Practice¶

For more practice with loops and strings see http://codingbat.com/java/Warmup-2. For practice with loops and arrays see http://codingbat.com/java/Array-2.

Here are some recommended problems