Java loops Flashcards

while, do-while and for loop

1
Q

What is the syntax of a basic for loop in Java?

A

for (initialization; condition; increment/decrement) {
System.out.println(i);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the following for loop do?

for (int i = 10; i >= 1; i–) {
System.out.print(i + “ “);
}

A

Counts backward from 10 to 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you print all even numbers from 1 to 20 using a for loop?

A

for (int i = 2; i <= 20; i += 2) {
System.out.println(i);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the initialization section of a for loop work? Can you initialize multiple variables?

A

initialization can be done only once.
Yes, multiple variables can be initialized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can a for loop be used without an initialization statement?

A

initialization can be done before the for loop and syntax for it can be left empty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What will happen if the condition in a for loop is omitted?

A

If the condition in a for loop is omitted, the loop will continue indefinitely, creating an infinite loop. it will keep iterating until it’s manually terminated (e.g., with a break statement or external interruption).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you break out of a for loop prematurely?

A

You can break out of a for loop prematurely using the ‘break’ statement. This causes the loop to terminate immediately, regardless of the loop condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between break and continue in a for loop?

A

break: Immediately terminates the loop, exiting the loop entirely.

continue: Skips the current iteration of the loop and moves to the next iteration, without terminating the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you nest two for loops to create a multiplication table (from 1 to 5)?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How would you sum all numbers from 1 to 100 using a for loop?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can a for loop iterate over an array in Java?

A

int[] arr = {10, 20, 30, 40};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you implement an enhanced for loop to iterate over an array?

A

int[] numbers = {1, 2, 3, 4};
for (int num : numbers) {
System.out.println(num);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does a for loop work with a String to print each character separately?

A

String word = “Java”;
for (int i = 0; i < word.length(); i++) {
System.out.println(word.charAt(i));
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How would you reverse a string using a for loop in Java?

A

String word = “Java”;
for (int i = word.length() - 1; i >= 0; i–) {
System.out.print(word.charAt(i));
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can you create an infinite loop using the for loop?

A

for(;;){
System.out.println(“infinite loop”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between a for loop and a while loop?

A

for loop: Typically used when the number of iterations is known beforehand.
while loop: Used when the number of iterations is not known ahead of time, and the loop continues until a specific condition becomes false.

17
Q

How does a do-while loop work, and how is it different from a while loop?

A

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

the code inside the ‘do’ block runs at least once even if the condition is false. In the while loop, if the condition is alse, the loop is exited instantly.

18
Q

What is the syntax of a basic while loop in Java?

A

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

19
Q

How do you write a while loop that runs until the user inputs a negative number?

A

Scanner sc = new Scanner(System.in);
int number;
while ((number = sc.nextInt()) >= 0) {
System.out.println(“You entered: “ + number);
}

20
Q

How can you create an infinite loop using a while loop?

A

while (true) {
System.out.println(“Infinite Loop”);
}

21
Q

How can you modify a for loop to iterate through only odd numbers between 1 and 20?

A

for (int i = 1; i <= 20; i += 2) {
System.out.println(i);
}

22
Q

How can you use a for loop to find the factorial of a number (e.g., 5!)?

A

int factorial = 1;
for (int i = 1; i <= 5; i++) {
factorial = factorial * i;
}
System.out.println(factorial);

23
Q

How would you use a for loop to reverse an array in Java?

A

int[] arr = {1, 2, 3, 4, 5};
for (int i = arr.length - 1; i >= 0; i–) {
System.out.print(arr[i] + “ “);
}

24
Q

How can you use a for loop to check if a number is prime?

A

A prime number is only divisible by 1 and itself. You can check this by dividing the number by all numbers from 2 to half of the number.

int number = 29;
boolean isPrime = true;
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
System.out.println(isPrime ? “Prime” : “Not Prime”);

25
Q

What will the following for loop output, and why?

for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.print(i + “ “);
}

A

The continue statement skips the current iteration when i is even. Therefore, only odd numbers will be printed.