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

Why is iteration important?

It allows us to execute code repeatedly, forming the backbone of complex algorithms.

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

All Flashcards

Why is iteration important?

It allows us to execute code repeatedly, forming the backbone of complex algorithms.

What is the key to avoiding infinite loops?

Ensuring the loop condition eventually becomes false.

When is a for loop preferred?

When you know the number of iterations in advance.

How can loops be used to manipulate strings?

Using string methods like .length() and .substring() within loops to process strings character by character.

What is the purpose of nested iterations?

To repeat a set of actions for each element in a collection of elements, often used with 2D arrays.

Why is tracing loops important?

It helps in debugging and understanding complex loop behavior.

What happens if a while loop's condition is initially false?

The loop body will never execute.

What is the role of initialization in a for loop?

It's executed once at the start of the loop to set up the loop variable.

What is the role of the condition in a for loop?

It's checked before each iteration to determine if the loop should continue.

What is the role of the iteration statement in a for loop?

It's executed after each iteration to modify the loop variable.

What does the following code output?

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

012

What does the following code output?

java
for (int i = 1; i <= 4; i++) {
 System.out.print(i + " ");
}

1 2 3 4

What does the following code output?

java
String str = "hello";
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
 reversed += str.charAt(i);
}
System.out.println(reversed);

olleh

What does the following code output?

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

10

What does the following code output?

java
String str = "programming";
int count = 0;
for (int i = 0; i < str.length(); i++) {
 if (str.charAt(i) == 'g') {
 count++;
 }
}
System.out.println(count);

2

Identify the error in the following code:

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

The loop will run infinitely because i is never incremented.

Identify the error in the following code:

java
for (int i = 5; i >= 0;)
 System.out.println(i);

Missing the increment/decrement statement in the for loop, and missing curly braces. The condition will always be true, resulting in infinite loop.

What does the following code output?

java
int x = 1;
while (x < 10) {
  x = x + 3;
}
System.out.println(x);

10

What does the following code output?

java
String s = "APCSA";
for (int i = 0; i < s.length(); i++) {
  System.out.print(s.substring(i, i + 1));
}

APCSA

What does the following code output?

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

**




What are the steps to reverse a string using a loop?

  1. Initialize an empty string. 2. Iterate through the original string from the last character to the first. 3. Append each character to the new string.

What are the general steps for using a while loop?

  1. Initialize a counter variable. 2. Write the while loop condition. 3. Include the code to be executed within the loop. 4. Update the counter variable inside the loop.

What are the general steps for using a for loop?

  1. Initialize the loop variable in the for loop declaration. 2. Define the loop condition. 3. Specify the increment/decrement operation. 4. Include the code to be executed within the loop.

What are the steps to trace a loop?

  1. Create a table with variables. 2. Execute each line of code. 3. Update the table with the new values. 4. Check the loop condition.

What are the steps to find a specific substring?

  1. Iterate through the string. 2. Extract a substring of the same length as the target substring. 3. Compare the extracted substring with the target substring. 4. If they are equal, return true; otherwise, continue.