7.7. Easy Multiple Choice Questions¶

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

6-7-1: What does the following code print?

```for (int i = 3; i <= 12; i++)
{
System.out.print(i + " ");
}
```
• 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.

6-7-2: How many times does the following method print a `*`?

```for (int i = 3; i < 9; i++)
{
System.out.print("*");
}
```
• 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?

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

```int x = -5;
while (x < 0)
{
x++;
System.out.print(x + " ");
}
```
• 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.

6-7-4: How many times does the following method print a `*`?

```for (int i = 5; i <= 12; i++)
{
System.out.print("*");
}
```
• 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.

6-7-5: How many times does the following method print a `*`?

```for (int i = 1; i < 5; 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).

6-7-6: How many times does the following method print a `*`?

```for (int i = 0; i <= 8; 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).

6-7-7: How many times does the following method print a `*`?

```for (int x = 0; x < 5; x++)
{
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.

6-7-8: How many times does the following method print a `*`?

```for (int x = 2; x < 8; 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.

6-7-9: What does the following code print?

```int x = 0;
while (x <= 5)
{
System.out.print(x + " ");
x++;
}
```
• 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.

6-7-10: What does the following code print?

```int x = 3;
while (x < 9)
{
System.out.print(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.
Next Section - 7.8. Medium Multiple Choice Questions