Loops In Java Flashcards

1
Q

What is a loop in Java?

A

A loop is a control structure that allows code to be executed repeatedly based on a condition.

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

Name the three main types of loops in Java.

A

The three main types of loops in Java are for loop, while loop, and do-while loop.

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

True or False: A for loop is used when the number of iterations is known.

A

True

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

Fill in the blank: The syntax for a for loop is ‘for (___; ___; ___)’.

A

initialization; condition; increment/decrement

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

What is the difference between a while loop and a do-while loop?

A

A while loop checks the condition before executing the loop body, while a do-while loop executes the loop body at least once before checking the condition.

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

Which loop would you use to iterate through an array in Java?

A

You can use a for loop or an enhanced for loop (for-each loop) to iterate through an array.

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

What keyword is used to exit a loop prematurely?

A

The keyword ‘break’ is used to exit a loop prematurely.

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

What keyword can be used to skip the current iteration of a loop?

A

The keyword ‘continue’ can be used to skip the current iteration of a loop.

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

True or False: The initialization part of a for loop can be empty.

A

True

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

What is an infinite loop?

A

An infinite loop is a loop that continues to execute indefinitely because its terminating condition is never met.

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

What is the output of the following code: ‘for (int i = 0; i < 5; i++) { System.out.print(i); }’?

A

The output will be ‘01234’.

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

Fill in the blank: In a for-each loop, the syntax is ‘for (___ : ___)’.

A

dataType variable : array/collection

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

What is the purpose of the ‘break’ statement in nested loops?

A

The ‘break’ statement exits the innermost loop in which it is called.

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

What is the output of the following code: ‘int i = 0; while (i < 3) { System.out.print(i); i++; }’?

A

The output will be ‘012’.

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

True or False: You can nest loops in Java.

A

True

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

What will happen if you do not update the loop variable in a while loop?

A

If you do not update the loop variable, it may lead to an infinite loop.

17
Q

Which loop is preferred for iterating over collections in Java?

A

The enhanced for loop (for-each loop) is preferred for iterating over collections.

18
Q

What is the syntax for a while loop?

A

The syntax for a while loop is ‘while (condition) { // body }’.

19
Q

Fill in the blank: The condition of a while loop must evaluate to ___ to continue looping.

20
Q

What is the output of the following code: ‘for (int i = 5; i > 0; i–) { System.out.print(i); }’?

A

The output will be ‘54321’.

21
Q

True or False: A do-while loop will always execute at least once.

22
Q

What is an example of a scenario where a while loop is more appropriate than a for loop?

A

A while loop is more appropriate when the number of iterations is not known beforehand, such as waiting for user input.

23
Q

What is the enhanced for loop also known as?

A

The enhanced for loop is also known as the for-each loop.

24
Q

What is the purpose of the ‘continue’ statement in a loop?

A

The ‘continue’ statement skips the current iteration and proceeds to the next iteration of the loop.