Which loop is guaranteed to execute its body at least once?
How many times does the body of for (int i = 0; i < 5; i++) execute?
How many times does for (int i = 1; i <= 5; i++) execute?
What is the value of i when System.out.print(i) is executed?
int sum = 0;
int i = 0;
while (i < 5) { sum = sum + i; i++; }
System.out.print(i);
System.out.print(" ");
System.out.print(sum);
In the previous program, what is the value of sum?
What is the value of balance after this code executes?
int balance = 10;
while (balance >= 1) {
if (balance < 9) continue;
balance = balance - 9;
}
Which statement about the for header for (A; B; C) is correct?
What is the output?
for (int i = 0; i < 3; i++);
System.out.print(i);
What is the output?
int i = 0;
do { System.out.print(i); i++; } while (i < 0);
What is the output?
int i = 0;
while (i < 0) { System.out.print(i); i++; }
What is printed?
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
System.out.print(i);
}
What is printed?
for (int i = 1; i <= 5; i++) {
if (i == 3) break;
System.out.print(i);
}
How many lines does the following code output?
public class Question7 {
public static void main(String[] args) {
String[] days = { "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };
for (int i = 0; i < days.length - 1; i++)
System.out.println(days[i]);
}
}
In a nested loop, break inside the inner loop:
How many times is System.out.print executed?
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 4; j++)
System.out.print("*");
How many stars are printed?
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) System.out.print("*");
System.out.println();
}
What is the output?
int n = 1234, sum = 0;
while (n > 0) { sum += n % 10; n /= 10; }
System.out.print(sum);
What does the loop in the previous question compute?
What is the output?
int count = 0;
for (int i = 10; i > 0; i -= 3) count++;
System.out.print(count);
Which loop prints 10 8 6 4 2?
Why does this loop never end?
int i = 1;
while (i <= 10) { System.out.print(i); }
Which is the natural loop for repeatedly showing a menu until the user chooses “quit”?
What is wrong with this attempt to total five numbers?
for (int i = 0; i < 5; i++) {
int total = 0;
total += i;
}
What is the output?
int x = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (i == j) x++;
System.out.print(x);
Which statement is true about a variable declared in a for header?
What is the output?
int total = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) continue;
total += i;
}
System.out.print(total);
Rewrite while (cond) { body } as a for loop. Which is correct?
What is the output?
int i = 5;
while (i-- > 0) System.out.print(i);
What is the output?
int product = 1;
int k = 1;
do { product *= k; k++; } while (k <= 4);
System.out.print(product + " " + k);
Which loop correctly reads numbers until the user enters −1 (sentinel), without adding the sentinel to the total?
A loop that repeats “while the user has not typed a valid value” implements:
What is the output?
String s = "Java";
for (int i = s.length() - 1; i >= 0; i--) System.out.print(s.charAt(i));
Which change would make the previous loop throw a
StringIndexOutOfBoundsException?
What is printed?
int i = 1, j = 10;
while (i < j) { i += 2; j -= 2; }
System.out.print(i + " " + j);
Convert the following while loop into an equivalent for
loop and explain why the for version is preferable here.
int i = 1;
while (i <= 12) { System.out.println(i + " x 7 = " + i * 7); i++; }
Explain the difference between break and continue, and
describe a situation in which using continue inside a while loop creates an infinite
loop.
A program must read exactly ten marks and report the average, the highest and the lowest. Which loop would you choose, and which variables must be initialised before the loop starts, and to what?
Write a program Multiplication that prints the multiplication
table from 1 to 9 as a neat grid, with rows and columns aligned, using nested loops.