zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

What is a nested loop?

A loop inside another loop.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What is a nested loop?

A loop inside another loop.

What is the outer loop in a nested loop?

The loop that controls the overall iterations.

What is the inner loop in a nested loop?

The loop that executes completely for each cycle of the outer loop.

What does the break statement do in a nested loop?

Exits the inner loop immediately.

What does the continue statement do in a nested loop?

Skips the current iteration of the inner loop.

What is the key difference between break and continue in nested loops?

break exits the loop entirely, while continue only skips the current iteration.

What does the following code output?

java
public static void printPyramid(int n) {
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
}
printPyramid(3);

** *

What does the following code output?

java
public static void printPyramid(int n) {
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            System.out.print(i+j);
        }
        System.out.println();
    }
}
printPyramid(3);

012 23 4

What does the following code output?

java
public static void printPyramid(int n) {
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            if (i == 1 && j == 1) {
                break;
            }
            System.out.print(i+j);
        }
        System.out.println();
    }
}
printPyramid(3);

012 2 4

What does the following code output?

java
public static void printPyramid(int n) {
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            if (i == 1 && j == 1) {
                continue;
            }
            System.out.print(i+j);
        }
        System.out.println();
    }
}
printPyramid(3);

012 23 4

Identify the potential issue in the following code:

java
for (int i = 0; i < n; i++) {
    for (int j = 0; i < m; j++) {
        System.out.println(i + " " + j);
    }
}

The inner loop condition i < m should be j < m to avoid an infinite loop or incorrect behavior.

What will be the value of primes after the following code executes with n = 3?

java
public static ArrayList<Integer> findNPrimes(int n) {
    int prime = 2;
    ArrayList<Integer> primes = new ArrayList<Integer>();
    for (int i = 0; i < n; i++) { // Outer loop: Finds n primes
        boolean notPrime = false;
        while (!notPrime) { // Middle loop: Checks if current number is prime
            for (int j = 2; j < prime; j++){ // Inner loop: Checks for divisors
                if (prime % j == 0) {
                    notPrime = true;
                    break; // Exit inner loop if not prime
                }
            }
            if (notPrime) {
                prime++;
                notPrime = false;
            } else {
                notPrime = true; // Exit middle loop if prime
            }
        }
        primes.add(prime);
        prime++;
    }
    return primes;
}
ArrayList<Integer> primes = findNPrimes(3);

[2, 3, 5]

What is the output of the following code snippet?

java
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < matrix.length; i++) {
    for (int j = 0; j < matrix[i].length; j++) {
        if (matrix[i][j] % 2 == 0) {
            System.out.print(matrix[i][j] + " ");
        }
    }
}

2 4 6 8

What is the output of this code?

java
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        System.out.print(i * j + " ");
    }
    System.out.println();
}

1 2 3 2 4 6 3 6 9

What is the output of the following code?

java
int count = 0;
for (int i = 0; i < 5; i++) {
    for (int j = i + 1; j < 5; j++) {
        count++;
    }
}
System.out.println(count);

10

What is the output of the following code snippet?

java
int x = 0;
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i == j) {
            x += i + j;
        }
    }
}
System.out.println(x);

6