AP Computer Science Loops Test

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By KretschK
K
KretschK
Community Contributor
Quizzes Created: 9 | Total Attempts: 7,328
| Attempts: 1,764 | Questions: 6
Please wait...
Question 1 / 6
0 %
0/100
Score 0/100
1. A for loop is an example of a pretest loop

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.

Submit
Please wait...
About This Quiz
AP Computer Science Loops Test - Quiz

For questions 7 and 8 you can type your answer into the box or write it on a piece of paper and hand in.

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

Explanation

not-available-via-ai

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

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.

Submit
4. 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++;      } }

Explanation

The innermost statement (x++) will execute 10,000 times. This is because the outer while loop will iterate 100 times (j++ 0). Therefore, the innermost statement will execute a total of 10,000 times.

Submit
5. 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?

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.

Submit
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; }

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.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 21, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 16, 2013
    Quiz Created by
    KretschK
Cancel
  • All
    All (6)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
A for loop is an example of a pretest loop
If x is an int where x = 0, what will x be after the following loop...
If x is an int where x = 1, what will x be after the following loop...
The following nested loop structure will execute the inner most...
Consider the following code segment: ...
Given that s is a String, what does the following loop do? ...
Alert!

Advertisement