Loops (While and Do-While) and logical operators (NOT, AND, OR) Flashcards
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
True
What is the output of the following code?
int counter = 2; do { counter++; System.out.println("x"); } while (counter < 6);
xxxx
What applies to a ‘do-while’ loop, but not to a ‘while’ loop?
It is guaranteed that code within the loop will run at least once.
Which of the following keywords can be used to exit the current iteration of the loop, and jump straight to the next iteration?
Continue
Which of the following shows the correct syntax for the logical OR operator in Java?
||
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?
Prior to, and outside, the loop syntax
Which of the following shows the correct syntax for the logical AND operator?
&&
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?
XZ
Which keyword is used to completely exit a loop?
Break