For Loops Flashcards
In which order do the statements in a ‘for’ loop appear?
Initialization statement, Continue condition, Increment statement
How many times would the loop below iterate?
for (int n = 32; n < 55; n += 2) { //some logic here }
12
How many times would the below loop iterate?
for (int p = 20; p >= 5; p -= 3) { //logic goes here }
6
True or False?
In the code below i is accessible outside the loop
for (int i = 0; i < 10; i++) { //some logic here }
False
What would the value of x be at the end of the final iteration, but before the loop exits?
for (int i = 0; i < 10; i++) {
int x = 6;
x += 2;
}
8
True or False?
In the code below x would not be accessible outside the loop
for (int i = 0; i < 10; i++) {
int x = x + i;
}
True
Determine the value of x output to the console given the code below?
int x = 5;
for (int i = 0; i < 2; i++) {
for (int j = 1; j < 4; j++) { x *= j; } }
180
What is the value of x printed to the console given the below code?
int x = 1; for (int i = 2; i < 7; i++) { x = x + i; } System.out.println(x);
21
What is the value of x printed to the console, given the code below?
int x = 2; for (int i = 0; i < 5; i++) { x = i; } System.out.println(x);
4