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

What does the following code output?

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

0 1 2 3 4

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

All Flashcards

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

What is the key principle behind using a while loop?

To repeatedly execute a block of code as long as a specified condition remains true.

Why is it important to avoid infinite loops?

Infinite loops can crash your program by consuming resources indefinitely.

What is the purpose of a loop control variable?

To control the number of iterations in a loop and ensure it terminates correctly.

Explain the concept of a sentinel-controlled loop.

A loop that continues until a specific input (the sentinel) is entered, signaling the end of the loop.

Explain the concept of a flag-controlled loop.

A loop that continues until a specific condition is met within the loop, indicated by a flag variable.

What are the four loopy questions used for debugging loops?

  1. Does my loop start right? 2. Does my loop end right? 3. Does my loop make progress towards completion? 4. Will the method break in the middle of the loop?

How does exception handling make code more robust?

By preventing crashes due to runtime errors and allowing the program to gracefully handle unexpected situations.

What is the purpose of the throws keyword in a method signature?

It indicates that the method might throw a specific exception, which must be handled by the calling method.

What is the importance of initializing variables before using them in a loop?

Ensures that the loop starts with a known state and avoids unexpected behavior.

Explain the difference between a skipped loop and an infinite loop.

A skipped loop never executes because the condition is initially false, while an infinite loop never terminates because the condition is always true.

What are the differences between break and continue statements?

break: Exits the loop entirely. | continue: Skips the current iteration and proceeds to the next.

What are the differences between a sentinel-controlled and a flag-controlled while loop?

Sentinel: Loop ends based on a specific input value. | Flag: Loop ends based on a condition changing a boolean variable.

What are the differences between try, catch, and finally blocks in exception handling?

try: Encloses code that might throw an exception. | catch: Handles a specific exception. | finally: Always executes, regardless of exceptions.