# Medium Multiple Choice Questions¶

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

6-7-1: How many stars are output when the following code is executed?

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
System.out.println("*");
}


6-7-2: 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();
}


6-7-3: 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.println(k + " ");
}


6-7-4: 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();
}


6-7-5: Assume that temp is an int variable intialized to be greater than zero and that a is an array of ints. What scenario will cause an infinite loop in the following code?

for (int k = 0; k < a.length; k++) {
while (a[k] < temp) {
a[k] *= 2;
}
}


6-7-6: 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;
}