THINK FIRST·CODE LATER

← Java Programming
Chapter 7 · Week 7

Loops and Iteration

Answered 0/38 Correct 0
Sign in to save progress across devices
Q1

Which loop is guaranteed to execute its body at least once?

Q2

How many times does the body of for (int i = 0; i < 5; i++) execute?

Q3

How many times does for (int i = 1; i <= 5; i++) execute?

Q4

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);
Q5

In the previous program, what is the value of sum?

Q6

What is the value of balance after this code executes?

int balance = 10;
while (balance >= 1) {
    if (balance < 9) continue;
    balance = balance - 9;
}
Q7

Which statement about the for header for (A; B; C) is correct?

Q8

What is the output?

for (int i = 0; i < 3; i++);
    System.out.print(i);
Q9

What is the output?

int i = 0;
do { System.out.print(i); i++; } while (i < 0);
Q10

What is the output?

int i = 0;
while (i < 0) { System.out.print(i); i++; }
Q11

What is printed?

for (int i = 1; i <= 5; i++) {
    if (i == 3) continue;
    System.out.print(i);
}
Q12

What is printed?

for (int i = 1; i <= 5; i++) {
    if (i == 3) break;
    System.out.print(i);
}
Q13

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]);
    }
}
Q14

In a nested loop, break inside the inner loop:

Q15

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("*");
Q16

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();
}
Q17

What is the output?

int n = 1234, sum = 0;
while (n > 0) { sum += n % 10; n /= 10; }
System.out.print(sum);
Q18

What does the loop in the previous question compute?

Q19

What is the output?

int count = 0;
for (int i = 10; i > 0; i -= 3) count++;
System.out.print(count);
Q20

Which loop prints 10 8 6 4 2?

Q21

Why does this loop never end?

int i = 1;
while (i <= 10) { System.out.print(i); }
Q22

Which is the natural loop for repeatedly showing a menu until the user chooses “quit”?

Q23

What is wrong with this attempt to total five numbers?

for (int i = 0; i < 5; i++) {
    int total = 0;
    total += i;
}
Q24

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);
Q25

Which statement is true about a variable declared in a for header?

Q26

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);
Q27

Rewrite while (cond) { body } as a for loop. Which is correct?

Q28

What is the output?

int i = 5;
while (i-- > 0) System.out.print(i);
Q29

What is the output?

int product = 1;
int k = 1;
do { product *= k; k++; } while (k <= 4);
System.out.print(product + " " + k);
Q30

Which loop correctly reads numbers until the user enters −1 (sentinel), without adding the sentinel to the total?

Q31

A loop that repeats “while the user has not typed a valid value” implements:

Q32

What is the output?

String s = "Java";
for (int i = s.length() - 1; i >= 0; i--) System.out.print(s.charAt(i));
Q33

Which change would make the previous loop throw a StringIndexOutOfBoundsException?

Q34

What is printed?

int i = 1, j = 10;
while (i < j) { i += 2; j -= 2; }
System.out.print(i + " " + j);
Q35 Short answer

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++; }
Q36 Short answer

Explain the difference between break and continue, and describe a situation in which using continue inside a while loop creates an infinite loop.

Q37 Short answer

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?

Q38 Programming

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.