AP Computer Science Loops Test

8 Questions | Total Attempts: 833

SettingsSettingsSettings
Please wait...
AP Computer Science Loops Test

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

  • 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

  • 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

  • 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

  • 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

  • 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

Back to Top Back to top