This book is now obsolete Please use CSAwesome instead.

# 5.8. Easy Multiple Choice Questions¶

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

5-8-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");
```
• 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.

5-8-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");
```
• 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.

5-8-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";
```
• 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.

5-8-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");
```
• 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.
You have attempted of activities on this page