# AP Computer Science Loops Test

Approved & Edited by ProProfs Editorial Team
At ProProfs Quizzes, our dedicated in-house team of experts takes pride in their work. With a sharp eye for detail, they meticulously review each quiz. This ensures that every quiz, taken by over 100 million users, meets our standards of accuracy, clarity, and engagement.
| Written by KretschK
K
KretschK
Community Contributor
Quizzes Created: 10 | Total Attempts: 6,102
Questions: 8 | Attempts: 1,386  Settings  For questions 7 and 8 you can type your answer into the box or write it on a piece of paper and hand in.

• 1.

• 2.

• 3.

### A for loop is an example of a pretest loop

• A.

True

• B.

False

A. True
Explanation
A for loop is an example of a pretest loop because the condition is evaluated before the loop body is executed. In a for loop, the condition is checked initially, and if it is true, the loop body is executed. If the condition is false, the loop is not executed at all. This means that the loop may not run even once if the condition is false from the beginning. Therefore, a for loop falls under the category of pretest loops.

Rate this question:

• 4.

### If x is an int where x = 1, what will x be after the following loop terminates? while (x < 100) {      x *= 2; }

• A.

2

• B.

64

• C.

100

• D.

128

• E.

It's an infinite loop

D. 128
Explanation
The given loop multiplies the value of x by 2 repeatedly until x becomes greater than or equal to 100. Since the initial value of x is 1, it will be multiplied by 2 repeatedly until it reaches 128, which is the largest power of 2 that is less than 100. Therefore, the value of x after the loop terminates will be 128.

Rate this question:

• 5.

### If x is an int where x = 0, what will x be after the following loop terminates? while (x < 100) {      x *= 2; }

• A.

2

• B.

64

• C.

100

• D.

128

• E.

It's an infinite loop

E. It's an infinite loop
• 6.

### Given that s is a String, what does the following loop do? int j = s.length( ); while (j > 0) {      System.out.print(s.charAt(j-1));     j -= 1; }

• A.

Prints s out backwards

• B.

Prints s out forwards

• C.

Prints s out backwards, skipping the last character

• D.

Prints s out backwards, skipping the first character

• E.

It's a run-time error because there is no character at s.charAt(j-1) for j = 0

A. Prints s out backwards
Explanation
The given loop iterates through the characters of the string "s" in reverse order. It starts from the last character of the string and prints each character one by one until it reaches the first character. Therefore, the loop prints the string "s" out backwards.

Rate this question:

• 7.

### The following nested loop structure will execute the inner most statement (x++) how many times? int j = 0, x = 0; while (j++ < 100) {      int k = 100;      while (k-- > 0) {            x++;      } }

• A.

100

• B.

200

• C.

10,000

• D.

20,000

• E.

100,000

C. 10,000
Explanation
The innermost statement (x++) will execute 10,000 times. This is because the outer while loop will iterate 100 times (j++ < 100), and for each iteration of the outer loop, the inner while loop will execute 100 times (k-- > 0). Therefore, the innermost statement will execute a total of 10,000 times.

Rate this question:

• 8.

### Consider the following code segment: for (int k = 0; k < 20; k = k + 2) {      if (k % 3 != 1) {            System.out.print(k + " ");     } } What is printed as a result of executing the code segment?

• A.

4 16

• B.

4 10 16

• C.

0 6 12 18

• D.

0 2 6 8 12 14 18

• E.

0 2 4 6 8 10 12 14 16 18

D. 0 2 6 8 12 14 18
Explanation
The code segment uses a for loop to iterate through values of k from 0 to 20 with a step size of 2. Inside the loop, there is an if statement that checks if k modulo 3 is not equal to 1. If this condition is true, the value of k is printed.

Starting from 0, the loop will iterate through the values: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18.

Out of these values, only 0, 2, 6, 8, 12, 14, and 18 satisfy the condition in the if statement. Therefore, these values will be printed.

Hence, the output of the code segment will be: 0 2 6 8 12 14 18.

Rate this question:

Related Topics Back to top
×

Wait!
Here's an interesting quiz for you.