What are the differences between a while loop and a for loop?
While Loop: Condition checked before each iteration, more flexible. | For Loop: Initialization, condition, and increment in one line, preferred when iterations are known.
Flip to see [answer/question]
Flip to see [answer/question]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Flip
Revise later
SpaceTo flip
If confident
All Flashcards
What are the differences between a while loop and a for loop?
While Loop: Condition checked before each iteration, more flexible. | For Loop: Initialization, condition, and increment in one line, preferred when iterations are known.
What are the differences between charAt() and substring()?
charAt(): Returns a single character at a specified index. | substring(): Returns a new string that is a substring of this string.
How is iteration applied in data processing?
Iterating through lists or arrays to perform calculations or transformations on each element.
How is iteration applied in searching algorithms?
Iterating through a dataset to find a specific element that satisfies a given condition.
How is iteration used to validate user input?
Loops can be used to repeatedly prompt the user for input until it meets certain criteria.
How is iteration used in game development?
Updating game state, processing user input, and rendering graphics.
How is iteration used in web scraping?
Extracting data from multiple web pages by iterating through a list of URLs.
How is string reversal used in real-world applications?
Can be used in cryptography or checking for palindromes.
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();
}