For Loops Flashcards

1
Q

In which order do the statements in a ‘for’ loop appear?

A

Initialization statement, Continue condition, Increment statement

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

How many times would the loop below iterate?

for (int n = 32; n < 55; n += 2) { 
    //some logic here
}
A

12

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

How many times would the below loop iterate?

for (int p = 20; p >= 5; p -= 3) { 
    //logic goes here
}
A

6

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

True or False?
In the code below i is accessible outside the loop

for (int i = 0; i < 10; i++) { 
    //some logic here
}
A

False

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

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;
}

A

8

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

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;
}

A

True

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

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;
} }
A

180

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

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);
A

21

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

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);
A

4

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