AP Computer Science Loops Test

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By KretschK
K
KretschK
Community Contributor
Quizzes Created: 9 | Total Attempts: 6,717
Questions: 8 | Attempts: 1,647

SettingsSettingsSettings
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.


Questions and Answers
  • 1. 

    Write a loop that simulates a missile launch countdown. Assume an integer variable called start contains the starting countdown value. For example,  if start equals15, your loop will print: 15...14...13...12...11...10...9...8...7...6...5...4...3...2...1...Blast-off!! You can assume start is positive. Make sure "Blast-off!!" is followed by a new line.For extra credit, "Ignition!!" along with the 3. You do not have to wait one second between numbers.

  • 2. 

    Consider the following method public void arithmetic(int a1, int d, int an) {      // Your loop } This method prints an arithmetic sequence whose starting value is the first parameter a1, whose common difference is the second parameter d, and which stops on or before reaching the third parameter an. In other words, if I call the method as follows:
    • arithmetic(3, 5, 50), the method will print: "3 8 13 18 23 28 33 38 43 48"
    • arithmetic(-10, 7, 25) will print "-10 -3 4 11 18 25"
    Your task is to write the loop that implements the methods code. You can assume d will always be positive.

  • 3. 

    A for loop is an example of a pretest loop

    • A.

      True

    • B.

      False

    Correct Answer
    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

    Correct Answer
    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

    Correct Answer
    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

    Correct Answer
    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

    Correct Answer
    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

    Correct Answer
    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:

Quiz Review Timeline +

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
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.