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

What is the definition of Iteration?

The process of repeating a block of code until a condition is met.

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

All Flashcards

What is the definition of Iteration?

The process of repeating a block of code until a condition is met.

What is a while loop?

A loop that executes a block of code repeatedly as long as a specified condition remains true.

What is an infinite loop?

A loop where the condition always evaluates to true, causing the loop to run indefinitely.

What is a sentinel value in a loop?

A specific input that signals the end of the loop.

What is a flag variable in a loop?

A boolean variable used to control the execution of a loop, typically set to true initially and false to exit.

What does the break statement do in a loop?

Immediately exits the loop, regardless of the loop condition.

What does the continue statement do in a loop?

Skips the rest of the current iteration and jumps to the next iteration.

What is an Exception?

An error that occurs during program execution.

What is a try block?

A block of code that might throw an exception.

What is a catch block?

A block of code that handles specific exceptions thrown in the try block.

What is a finally block?

A block of code that always executes, regardless of exceptions.

What are the steps to determine if a number is divisible by another number without using the modulo operator?

  1. Check for invalid input (negative number or non-positive divisor). 2. If the number is 0, return true. 3. While the number is greater than 0, subtract the divisor from the number. 4. Return true if the number is 0 after the loop, false otherwise.

What are the steps to extract digits from an integer?

  1. Check for invalid input (negative number). 2. While the number is not 0, print the last digit (number % 10). 3. Remove the last digit (number /= 10).

What are the steps to find the minimum value from a series of user inputs using a while loop?

  1. Initialize min with the first input number. 2. While the input is not the sentinel value, compare the input with min. 3. If the input is less than min, update min. 4. Get the next input.

What are the steps to find the maximum value from a series of user inputs using a while loop?

  1. Initialize max with the first input number. 2. While the input is not the sentinel value, compare the input with max. 3. If the input is greater than max, update max. 4. Get the next input.

What are the steps to compute the sum of a series of user inputs using a while loop?

  1. Initialize sum to 0. 2. While the input is not the sentinel value, add the input to sum. 3. Get the next input.

What are the steps to compute the average of a series of user inputs using a while loop?

  1. Initialize sum to 0 and counter to 0. 2. While the input is not the sentinel value, add the input to sum and increment counter. 3. Get the next input. 4. Calculate the average (sum / counter).

What does the following code output?

java
int i = 0;
while (i < 5) {
  System.out.println(i);
  i++;
}

0 1 2 3 4

Identify the error in the following code:

java
int i = 0;
while (i < 5) {
  System.out.println(i);
}

The loop will run infinitely because the value of i is never updated.

What does the following code output?

java
int i = 5;
while (i > 0) {
  System.out.println(i);
  i--;
}

5 4 3 2 1

Identify the error in the following code:

java
int i = 10;
while (i > 0);
{
  System.out.println(i);
  i--;
}

The semicolon after the while condition creates an empty loop, and the subsequent block is executed only once.

What does the following code output?

java
int i = 0;
while (i < 10) {
  if (i % 2 == 0) {
    System.out.println(i + " is even");
  }
  i++;
}

0 is even 2 is even 4 is even 6 is even 8 is even

Identify the error in the following code:

java
int i = 0;
while (i < 5) {
  System.out.println(i);
  i++;
  if (i == 3) {
    break;
  }
}

No error. This code will print 0, 1, and 2, then exit the loop.

What does the following code output?

java
int i = 0;
while (i < 5) {
  i++;
  if (i == 3) {
    continue;
  }
  System.out.println(i);
}

1 2 4 5

Identify the error in the following code:

java
int number = -5;
while (number > 0) {
  System.out.println(number);
  number--;
}

The loop condition is initially false, so the loop body will never execute.

What does the following code output?

java
int i = 0;
while (true) {
  System.out.println(i);
  i++;
  if (i > 5) {
    break;
  }
}

0 1 2 3 4 5

Identify the error in the following code:

java
int i = 0;
while (i < 5) {
  System.out.println(i++);
}

No error. This code will print 0, 1, 2, 3, and 4.

What does the following code output?

java
int x = 10;
try {
  int result = x / 0;
  System.out.println("Result: " + result);
} catch (ArithmeticException e) {
  System.out.println("Error: Division by zero");
}

Error: Division by zero