Loops (While and Do-While) and logical operators (NOT, AND, OR) Flashcards

1
Q

True or False?
Variables declared outside a loop can be accessed inside the loop
Variables declared inside a loop cannot be accessed outside the loop

A

True

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

What is the output of the following code?

int counter = 2;
do {
    counter++;
    System.out.println("x");
}
while (counter < 6);
A

xxxx

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

What applies to a ‘do-while’ loop, but not to a ‘while’ loop?

A

It is guaranteed that code within the loop will run at least once.

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

Which of the following keywords can be used to exit the current iteration of the loop, and jump straight to the next iteration?

A

Continue

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

Which of the following shows the correct syntax for the logical OR operator in Java?

A

||

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

If a ‘while’ loop is used with a variable to keep a count of the number of times it has iterated, it must be declared when?

A

Prior to, and outside, the loop syntax

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

Which of the following shows the correct syntax for the logical AND operator?

A

&&

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

Consider the following code:

boolean a = true;
boolean b = false;
boolean c = true;

if (!b || !c) {
    System.out.print("x");
}
else{
    System.out.print("y");
}
if (c){
    System.out.print("z");
}

What would be output if the code was executed?

A

XZ

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

Which keyword is used to completely exit a loop?

A

Break

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