Chapter 2: Operators and Statements Flashcards
What is the structure of a while statement?
while ( booleanExpression ) {
// do something
}
What is a termination condition in a while statement?
This is the boolean expression inside a while loop that will continue executing as long as the expression evaluates to true.
In a while loop, when does a loop terminate
The boolean expression is evaluated before each iteration of the loop and when the expression becomes false, the loop terminates
What is the structure of a do while loop?
do {
statement(s)
} while (booleanExpression);
How many times will a do while loop execute and why?
It will be executed at least once cos Java will execute the statement and then check the condition after
Are the curly braces a requirement in both while and do while loops?
No, they are not required for single statements, only for a block of multiple statements.
What is the structure of a for loop?
for ( 1) initialization block ; 2) booleanExpression ; 4) updateStatement) { 3) //body }
What are the 5 steps involved in the execution of a for loop?
1) initialization of variable(s) occurs 2) if boolean expression is true, keep going else exit the loop 3) Execute statement(s) in the body 4) Execute the update statements 5) go back to step 2 and rinse and repeat until the expression is false
Is this a valid statement? for( ; ; ) { System.out.println(“hello”) }
Yes, perfectly legal. This will create an infinite loop. The parentheses and semicolons are mandatory whereas the curly braces are only required for multiple statements.
True or False, in a for statement, the initialisation and updation section can only contain one statement?
False, they can contain multiple statements separated by commas.
What is the output of this code? for ( int i = 0; i<10; i++) { System.out.println( i + “ “); }
It will print numbers 0 - 9 and once it reaches the 10th iteration, the expression will evaluate to false and the loop will exit
What is the output of this code? int x = 0; for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) { System.out.println( x + “ “); }
It won’t compile because the variable x is being redeclared in the for loop after already being initialised before. The compiler will recognise that is a duplicate
What is the output of this code? for (long y = 0, int x = 4; x < 5 && y < 10; x++, y++) { System.out.println( x + “ “); }
It won’t compile as long and int and int are not of the same type which is required in the initialisation block
What is the output of this code? for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) { System.out.println( y + “ “); } System.out.println( x);
This will not compile as the variable x is out of scope. As it was defined inside the loop, it can only be accessed there
Is the following code legal in Java? int x = 0; for (long y = 0, z = 4; x <5 && y < 10; x++, y ++) { System.out.println( y + “ “); }
Yes, you can have multiple statements within the initialisation and updation section of a for loop. Additionally you can define random variables like z which is never used
What is the structure of an enhanced for loop? which features are required?
for (datatype name : collection) { // Body } The for keyword, parentheses, colon are required and the curly braces only if there are multiple statements.
What is on the RHS of an enhanced loop
An already defined Array or lterable object like ArrayList and List
What is on the LHS of an enhanced loop
The LHS must have the matching type for the object on the right. e.g the type of an int []array will be an int the type for a 2D String [][]array is a String array[] the type of a list with type Integer will be an int
Will this code compile? String [] names = new String[3] for(String name : names) { System.out.println( names + “ “); }
Yes even though the values inside names are null, the code will compile fine and output null 3 times.
what is the output of this code? Explain your logic
int x = 20;
while (x>0) {
do { x - = 2;
}
while (x>5);
x–;
System.out.println(x);
}
// output is 3, 0 As x is 20, the statement inside the outer loop evaluates to true as a result, we enter the inner loop which tells us to keep repeating & decrementing the value of x by 2 until the condition (x>5) is false. when x is 4, the inner loop exits and x goes into the outer-loop where it is decremented by 1. Output is now 3. x>0 is still true thus another iteration begins even though x is not greater than 5 cos a do while loop will always execute at least once. x is decremented again by 1 and is 0. The loop finally terminated .
What is a label?
A label is a marker that points to the head of a statement.
Labels allow the application flow to jump to the statement or break from it.
What is the structure of a label?
It is a single word with a colon after it. e.g OUTER_LOOP:
Is a label a requirement?
No they are optional
What is the convention for labels?
They follow the same rules for identifiers however the convention is that they are written in uppercase separated by underscore similar to constants
What does the break statement do?
Exits out of a statement/loop and returns control to the enclosing statement
What does continue do?
Essentially skips the iteration of a loop if a certain condition is met and carries on with the next iteration of the loop. e.g for (int k = 0; k<100; k++) { if (k % 7 == 0) { continue; } System.out.println(k) //
Which data types are supported in a switch statement?
Strings, Enums & Integral values: byte, char, short, int and their wrapper classes.
What is an unreachable statement?
This is when the JVM throws a compile error when it knows that a certain code/ statement will never be executed. For instance code written after an infinite loop
When can you not use break?
you can’t use break in an if statement UNLESS the if statement is placed within a switch statement or a loop
When can you not use continue?
you can’t use break in an “if” or “switch” statement UNLESS in a while, do, or for loop
Can the String class name be used as a label or a variable?
yes, String is still a valid identifier however it is not advised
Another definition of continue
continue means we shouldn’t bother executing any of the statements for this current iteration. Basically skip this iteration and continue with the rest.
What are labels used for?
Optional label allows us to break out of a higher level outer loop
Name the logical operators and what they are used for
& - AND - only true if both operands are true | - INCLUSIVE OR - only false if both operands are false ^ - EXCLUSIVE OR - only true if the operands are different
What is the resulting data type of the equation x * y where: int x = 1; long y = 33;
long // the result is automatically promoted to the larger data type
What will this code print? double x = 39.21; float y = 2.1f; float z = x + y; System.out.println(z);
line : float z = x + y; will not compile when you add a float and a double together, the value returns a double due to numeric promotion. Therefore to fit into a float variable you need an explicit cast. e.g float z = (float) x+y;