All Flashcards
What is the output of the following code?
java
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += i;
}
System.out.println(sum);
10
What is the output of the following code?
java
for (int i = 1; i <= 3; i++) {
System.out.print(i + " ");
}
1 2 3
What is the output of the following code?
java
int x = 5;
for (int i = 0; i < x; i++) {
System.out.print("*");
}
What is the output of the following code?
java
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
1 2 3 4 5
What is the output of the following code?
java
int count = 0;
for (int i = 0; i < 10; i += 2) {
count++;
}
System.out.println(count);
5
Identify the error in the following code:
java
for (int i = 0; i < 10; ) {
System.out.println(i);
}
Missing increment/decrement statement, leading to an infinite loop.
What is the output of the following code?
java
int product = 1;
for (int i = 1; i <= 4; i++) {
product *= i;
}
System.out.println(product);
24
What is the output of the following code?
java
String str = "Hello";
for (int i = 0; i < str.length(); i++) {
System.out.print(str.charAt(i) + " ");
}
H e l l o
What is the output of the following code?
java
int[] nums = {5, 10, 15};
for (int num : nums) {
System.out.print(num + " ");
}
5 10 15
What is the output of the following code?
java
int result = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 != 0) {
result += i;
}
}
System.out.println(result);
9
What are the key differences between for and while loops?
For loops are best for known iterations; while loops are for unknown iterations based on a condition.
Compare and contrast for loops and for-each loops.
For loops use an index; for-each loops iterate directly over elements. For-each are simpler but can't modify the original array directly.
Compare the use cases of for loops and do-while loops.
For loops are for definite iterations; do-while loops ensure at least one execution.
Compare using a for loop to iterate through an array vs. an ArrayList.
Arrays use .length and direct indexing. ArrayLists use .size() and .get(index).
What are the similarities between for and while loops?
Both are used for repeated execution of a block of code based on a condition.
Compare the readability of a for loop versus a while loop for array traversal.
For loops are generally more concise and readable for simple array traversal.
Compare the use of break statements in for loops and while loops.
Both can use break to exit the loop prematurely.
Compare the use of continue statements in for loops and while loops.
Both can use continue to skip the current iteration.
Compare the control over loop variables in for loops and while loops.
For loops provide more structured control over initialization, condition, and increment/decrement.
Compare the common mistakes made with for loops and while loops.
For loops: off-by-one errors. While loops: forgetting to update the loop variable.
How are for loops used in image processing?
To iterate through pixels in an image and apply filters or transformations.
How are for loops used in data analysis?
To process large datasets, calculate statistics, and generate reports.
How are for loops used in game development?
To update game states, render graphics, and handle user input.
Give an example of using for loops to process data from a file.
Reading each line of a file and performing operations on the data.
How are for loops used in machine learning?
To train models on large datasets by iterating through training examples.
How are for loops used in web development?
To generate dynamic content, handle user interactions, and process form data.
How can for loops be used to simulate real-world scenarios?
Modeling physical systems, simulating financial markets, or predicting weather patterns.
How are for loops used in cryptography?
To encrypt and decrypt data using iterative algorithms.
How are for loops used in robotics?
To control robot movements, process sensor data, and execute tasks.
How are for loops used in scientific computing?
To solve complex mathematical equations, simulate physical phenomena, and analyze experimental data.