Docsity

AP Computer Science A Unit 4 Study Guide: Loops and Iteration, Exams of Advanced Education

Toggle sidebar
bg1
AP Computer Science A Unit 4 Study
Guide
int a = 0;
int b = 0;
while (a < 5 && b < 3)
{
System.out.println(a + " " + b);a++;b++;
}
What is output? - Answer -0 0
1 1
2 2
int n = 14;
while (!(n % 3 == 0 && n % 5 == 0))
{
System.out.println(n);
n += 2;
}
What is the last number printed by this code? - Answer -28
What is one potential problem with the following loop?
System.out.print("Enter integers. Enter -1 to exit.");
System.out.println(" Count of integers entered will be returned.");
int n = 0;
int c = 0
;while (n != -1)
{
n = scan.nextInt();
c++;
}
System.out.println(c); - Answer -The loop counts when the user enters -1 into the
keyboard, so the count will be one too many.
What is one potential problem with the following loop?
int n = 5;
while (n != -1)
pf3
pf4
pf5

Partial preview of the text

Download AP Computer Science A Unit 4 Study Guide: Loops and Iteration and more Exams Advanced Education in PDF only on Docsity!

AP Computer Science A Unit 4 Study

Guide

int a = 0; int b = 0; while (a < 5 && b < 3) { System.out.println(a + " " + b);a++;b++; } What is output? - Answer -0 0 1 1 2 2 int n = 14; while (!(n % 3 == 0 && n % 5 == 0)) { System.out.println(n); n += 2; } What is the last number printed by this code? - Answer - What is one potential problem with the following loop? System.out.print("Enter integers. Enter -1 to exit."); System.out.println(" Count of integers entered will be returned."); int n = 0; int c = 0 ;while (n != -1) { n = scan.nextInt(); c++; } System.out.println(c); - Answer -The loop counts when the user enters -1 into the keyboard, so the count will be one too many. What is one potential problem with the following loop? int n = 5; while (n != -1)

System.out.println(n); } - Answer -Since n does not change the loop will not stop. int n = 4; while (n <= 15) { n += 2; System.out.print(n + " "); } What is output? - Answer -6 8 10 12 14 16 int n = 80; while (n > 40) { System.out.print(n % 10 + " "); n -= 5; } What is output? - Answer -0 5 0 5 0 5 0 5 What is output to the screen by the following code: int n = 3; while (n < 7) { n++; System.out.print(n + " "); } - Answer -4 5 6 7 What is output to the screen by the following code: int n = 10; while (5 < n) { n--; System.out.print(n + " "); } - Answer -9 8 6 7 5 Often on the AP exam they ask you to print out the first and last number printed by a loop. What are the first and last values printed by: int n = 5; while (n < 32) { n += 5; System.out.print(n + " "); } - Answer -10 35 What are the first and last numbers printed by the following code:

int d = 2; while (d <= a) { if (a % d == 0 && b % d == 0) { f = 1; } d++; } System.out.println(f); - Answer - Consider the following code, assume a and b are integers and have been initialized. What is output when a = 66 and b = 25? int f = 0; int d = 2; while (d <= a) { if (a % d == 0 && b % d == 0) { f = 1; } d++; } System.out.println(f); - Answer - When might it be more helpful to use a for loop instead of a while loop? - Answer -When the loop is going to run a set number of times. Choose which three values should replace the blanks in the for loop so that it loops through the numbers: 2 4 6 8 10. Note that the choices will fill in the three blanks in the order which they appear. for (int i = ____; i ____; i _____) { System.out.print(i + " "); } - Answer -2, <= 10, += 2 What is output? for (int i = 1; i <= 5; i++) { System.out.print(i + " "); } - Answer -1 2 3 4 5 What is output by the following code segment? for (int i = 12; i >= 5; i -= 3) { System.out.print(i + " ");

} - Answer -12 9 6 Consider the following code segment in which str is a String with length greater than 1. for (int i = 0; i < str.length() - 1; i++) { if (str.substring(i, i + 2).equals("oh")) { System.out.println("Found it!"); } } Which of the following best describes what this code segment does? - Answer -Prints out "Found it!" for every substring which is equal to "oh". The following code segment displays the number of times the character 'a' appears in the String str. int count = 0; for (int i = 0; i < str.length(); i++) { String letter = str.substring(i, i + 1); if (letter.equals("a")) { count++; } System.out.println(count); } Which of the following modifications to the code will ensure the number it displays includes times the capital 'A' appears? - Answer -Change the if statement condition to letter.equals("a") || letter.equals("A") What is printed by the following code segment? String str = "normality"; for (int i = str.length(); i > 1; i -= 2) { System.out.print(str.substring(i - 1, i)); } - Answer -yiar What is printed by the following code? String str = "an anaconda and an ant"; int count = 0; for (int i = 0; i < str.length() - 1; i++) { if (str.substring(i, i + 2).equals("an")) { count++; }

String str = "RAM"; for (int i = str.length(); i > 0; i--) { for (int j = 1; j <= 3; j++) { System.out.print(str.substring(i - 1, i)); } } - Answer -MMMAAARRR int x = 0; while (x < 5) { int y = 5; while (x < y) { y--; x++; } System.out.print(y + " "); } - Answer -2 4 4