Edhesive Unit 4
What is the output after executing the following: int x = 0; while (x < 5) { System.out.print(x); x++; }
01234
What is x equal to after the following code is executed? int x = 0; x++;
1
Consider the following code, assume a and b are integers and have been initialized. What is output when a = 55 and b = 45? int f = 0; int d = 2; while (d <= a) { if (a % d == 0 && b % d == 0) { f = 1; } d++; } System.out.println(f);
1
What is output by the following code? for (int i = 1; i < 4; i++) { for (int j = 0; j < i; j++) { System.out.print(i); } System.out.println(); }
1 22 333
Consider the following code: for (int i = 12; i > 2; i -= 3) { System.out.print(i + " "); }
12 9 6 3
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 + " "); }
2, <= 10, += 2
Consider the following code: int n = 14; while (!(n % 3 == 0 && n % 5 == 0)) { System.out.println(n); n += 2; }
28
What is output to the screen by the following code: int n = 3; while (n < 7) { n++; System.out.print(n + " "); }
4 5 6 7
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++; } } System.out.println(count);
5
Consider the following two algorithms which both are meant to print all multiples of 11 from 1 up to a user input positive integer value - upper. Which statement correctly compares the efficiency of these two algorithms? Algorithm 1: for (int i = 1; i <= upper; i++) { if (i % 11 == 0) { System.out.println(i + " "); } } Algorithm 2: for (int i = 1; i <= upper / 11; i++) { System.out.println(i * 11 + " "); }
Algorithm 2 is more efficient for all possible values of upper.
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?
Change the if statement condition to letter.equals("a") || letter.equals("A").
What is output by the following code? for (int i = 0; i < 4; i++) { for (int j = i; j < 5; j++) { System.out.print(j + " "); } System.out.println(); }
0 1 2 3 4 1 2 3 4 2 3 4 3 4
Consider the following code: int n = 80; while (n > 40) { System.out.print(n % 10 + " "); n -= 5; }
0 5 0 5 0 5 0 5
What is output? for (int i = 1; i <= 5; i++) { System.out.print(i + " "); }
1 2 3 4 5
How many times will line 6 be executed when the following code is run? 1 for (int i = 0; i <= 15; i += 5) 2 { 3 int s = 0; 4 for (int j = i; j < i + 3; j++) 5 { 6 s += j; 7 } 8 System.out.print(s + " "); 9 }
12
What is output by the following code? int a = 5; int s = 0; while (a % 4 != 0) { s += a; a++; } System.out.println(s);
18
What are the first and last numbers printed by the following code: int x = 20; while (x <= 25) { x++; System.out.print(x + " "); }
21 26
How many times will line 7 be executed when the following code is run? 1 String str = "rebellion"; 2 int i = 0; 3 int j = str.length() - 1; 4 String result = ""; 5 while (i < j) 6 { 7 result = str.substring(i, i + 1) + result + str.substring(j, j + 1); 8 i++; 9 j--; 10 } 11 System.out.println(result);
4
Consider the following code: int x = 5; while (x < 9) { System.out.print(x + " "); x += 2; } What is output?
5 7
Consider the following code: int n = 4; while (n <= 15) { n += 2; System.out.print(n + " "); }
6 8 10 12 14 16
Consider the following code: int n = 75; while (n > 0) { System.out.print(n + " "); n /= 10; } What is output?
75 7
What is output by the following loop: int n = 10; while (n > 5) { n--; System.out.print(n); }
98765
What is the difference between the two loops? I. int num = 0; int c = 0; while (num != 100) { num = scan.nextInt(); c++; } II. int num = 0; int c = 0; while (c != 100) { num = scan.nextInt(); c++; }
I stops by using user input, II stops by using a count variable.
Consider the following code: int a = 7; while (a < 15) { a += a % 4; System.out.print(a + " "); }
Infinite Loop
What is output by the following code? 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)); } }
MMMAAARRR
Which of the following best describes what the following algorithm does? You may assume the variable s is initialized as a non-empty String. boolean r = true; for (int i = 0; i < s.length() - 1; i++) { if (s.substring(i, i + 1).equals(s.substring(i + 1, i + 2))) { r = false; } } if (r) { System.out.println("pass"); }
The algorithm prints "pass" if there are no pairs of consecutive letters in s which are the same.
Adding a tracking variable to count the number of times a loop executes can be an effective way to measure efficiency.
True
If A is true, B is true and C is true, is the following compound statement true or false? (A && B) && (B || C)
True
When might it be more helpful to use a for loop instead of a while loop?
When the loop is going to run a set number of times.
A while loop is an example of ______
iteration
What is printed by the following code? String str = "internet"; for (int i = 1; i < str.length(); i += 2) { System.out.print(str.substring(i, i + 1)); }
nent
Suppose the String variable str is initialized with a value of "abcde". Which of the following is equal to the string "bcd"?
str.substring(1, 4)
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)); }
yiar
Consider the following code: int a = scan.nextInt(); int b = scan.nextInt(); while (a < b) { a += b % a; System.out.println(a + " " + b); }
4 10 6 10 10 10
What is output to the screen by the following code: int n = 10; while (5 < n) { n--; System.out.print(n + " "); }
9 8 7 6 5
What is output by the following code segment? for (int i = 12; i >= 5; i -= 3) { System.out.print(i + " "); }
12 9 6
What is output by the following code? int x = 0; while (x < 5) { int y = 5; while (x < y) { y--; x++; } System.out.print(y + " "); }
2 4 4
What is output by the following code? int a = 0; for (int i = 1; i < 5; i++) { for (int j = 1; j < 4; j++) { a++; } } System.out.println(a);
12
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);
0
What is x equal to after the following code is executed? int x = 1; x--;
0
Consider the following code: int a = 0; int b = 0; while (a < 5 && b < 3) { System.out.println(a + " " + b); a++; b++; } What is output?
0 0 1 1 2 2
What is the result of 28 % 3?
1
What is output by the following code? for (int i = 5; i >= 2; i--) { for (int j = 2; j <= 4; j++) { System.out.print(i * j + " "); } System.out.println(); }
10 15 20 8 12 16 6 9 12 4 6 8
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 + " "); }
10 35
Consider the following algorithm acting on a String str. 1 int count = 0; 2 for (int i = 0; i < str.length(); i++) 3 { 4 for (int j = i; j >= 0; j--) 5 { 6 String a = str.substring(j, j + 1); 7 if (a.equals(str.substring(i, i + 1))) 8 { 9 System.out.print(a); 10 } 11 } 12 } Suppose we wish to use the variable count to measure the number of times two characters in the string are compared. What line of code should be added and where?
Add the line count++; between lines 5 and 6.
When is the increment statement (shown in bold below) run in a for loop? for(int i = 0; i < 10; i++)
At the end of every run of the loop, before the boolean expression is checked for the next iteration of the loop.
When is the initialization statement (shown in bold below) run in a for loop? for(int i = 0; i < 10; i++)
Before the first run of the loop only, before the boolean condition is checked.
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?
Prints out "Found it!" for every substring which is equal to "oh".
What is one potential problem with the following loop? int n = 5; while (n != -1) { System.out.println(n); }
Since n does not change the loop will not stop.
What does the following loop do: int num = scan.nextInt(); int sum = 0; while (num > 0) { sum += num % 10; num /= 10; } System.out.print(sum);
Sums each digit of the number the user enters.
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);
The loop counts when the user enters -1 into the keyboard, so the count will be one too many.
Which of the following code segments will produce the same output as the segment below? for (int i = 5; i < 14; i += 3) { System.out.print(i); }
int i = 5; while (i < 14) { System.out.print(i); i += 3; }
Consider the following code: String w = "onomatopoeia"; for (int i = 0; i < w.length(); i++) { System.out.print(w.substring(i, i + 1) + " "); if (i % 3 == 2) { System.out.println(); } } What is output?
o n o m a t o p o e i a
What is output by the following code? String str = "processor"; System.out.print(str.substring(str.length() - 1)); System.out.print(str.substring(1, 2);
rr