2 Conditional statements, loops and flow control Flashcards

1
Q

Are braces required around the block after an if statement?

A

No but only the first line will be executed if there are no braces.

//executes both lines
if(hourOfDay < 11) {
System.out.println("Good Morning");
morningGreetingCount++;
}

//only executes first line
if(hourOfDay < 11)
System.out.println(“Good Morning”);
morningGreetingCount++;

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

What is the ternary operator?

A

The conditional operator, ? :, otherwise known as the ternary operator, is the only operator
that takes three operands and is of the form:
booleanExpression ? expression1 : expression2

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

How is the ternary expression written?

A

booleanExpression ? expression1 : expression2

int y = 10;
final int x;
if(y > 5) {
x = 2 * y;
} else {
x = 3 * y;
}
Compare the previous code snippet with the following equivalent ternary operator code
snippet:
int y = 10;
int x = (y > 5) ? (2 * y) : (3 * y);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Are parenthesis required on the expressions in the ternary operator statement?

A

no

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

What is a switch statement?

A

A switch statement, as shown in Figure 2.4, is a complex decision-making structure
in which a single value is evaluated and fl ow is redirected to the fi rst matching branch,
known as a case statement. If no such case statement is found that matches the value, an
optional default statement will be called. If no such default option is available, the entire
switch statement will be skipped.

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

Which data types are allowed in switch statements?

A
■ int and Integer
■ byte and Byte
■ short and Short
■ char and Character
■ int and Integer
■ String
■ enum values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which data types are NOT supported by switch statements?

A

boolean long, float, double and

their associated wrapper classes,

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

Explain compile time constant values

A

The values in each case statement must be compile-time constant values of the same data
type as the switch value. This means you can use only literals, enum constants, or final constant variables of the same data type. By final constant, we mean that the variable must be marked with the final modifier and initialized with a literal value in the same
expression in which it is declared.

public static void main(String args[]){

    final int a=1;
    int b=2;
    int c=3;
        switch(c){
            case a: System.out.println("A"); // Ok, its final
            case b: System.out.println("B"); //will not compile;
            case 3: System.out.println("C"); // ok, literal

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

Write the logical truth tables for &, |, and ^

A

Here are some tips to help remember this table:
■ AND is only true if both operands are true.
■ Inclusive OR is only false if both operands are false.
■ Exclusive OR is only true if the operands are different.

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

In case statements, when does default get executed?

A
  • If there is no other match found.

- If the statement above gets executed and there is no break statement it will flow down into the default.

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

What are the rules for the order of case statements?

A
  • case statements can be in any order

- default statement can be in any order

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

What happens if a break statement is not added to a case code block?

A

if you leave out the break statement,

flow will continue to the next proceeding case or default block automatically.

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

When is the default block executed in a switch statement?

A

Even though the default block was before the case block, only the case block was executed.
If you recall the defi nition of the default block, it is only branched to if there is no
matching case value for the switch statement, regardless of its position within the switch
statement

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

What is the syntax for the for each loop?

A
for(datatype instance : collection) { 
//body
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a for each statement?

A

Starting with Java 5.0, Java developers have had a new type of enhanced for loop at their
disposal, one specifically designed for iterating over arrays and Collection objects. This
enhanced for loop, which for clarity we’ll refer to as a for-each loop

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

What is something to look out for on the exam about for each loops?

A

When you see a for-each loop on the exam, make sure the right-hand side is an array
or Iterable object and the left-hand side has a matching type. For example, the two
examples that follow will not compile.

■ Why will the following fail to compile?
String names = “Lisa”;
for(String name : names) { // DOES NOT COMPILE
System.out.print(name + “ “);
}

In this example, the String names is not an array, nor does it implement java.lang.
Iterable, so the compiler will throw an exception since it does not know how to iterate
over the String.

■ Why will the following fail to compile?
String[] names = new String[3];
for(int name : names) { // DOES NOT COMPILE
System.out.print(name + “ “);
}

This code will fail to compile because the left-hand side of the for-each statement does
not defi ne an instance of String. Notice that in this last example, the array is initialized
with three null pointer values. In and of itself, that will not cause the code to not
compile, as a corrected loop would just output null three times.

17
Q

What are labels and how are they used?

A

A label is an optional pointer to the head of a statement that allows the application flow to jump to it or break from it. It is a single word that is followed by a colon (:).

18
Q

What is the syntax for a label?

A

A single word followed by a colon.

int[][] myComplexArray = {{5,2,1,3},{3,9,8,9},{5,7,12,7}};
OUTER_LOOP: for(int[] mySimpleArray : myComplexArray) {
INNER_LOOP: for(int i=0; i

19
Q

What is a break statement?

A

a break statement transfers the fl ow

of control out to the enclosing statement.

20
Q

How do break statements operate in loops?

A

Without a label parameter, the break statement will terminate the nearest inner loop it is currently in the process of executing. The optional label parameter allows us to break out of a higher level outer loop.

21
Q

Which type of flow control allows optional labels?

Flow control= if, while, do while, for, switch

A

All of them allow labels

22
Q

Which type of flow control does NOT allow a break statement?

A

if statements. They are not loops, don’t get breaks.

23
Q

Which type of flow control does NOT allow a continue statement?

A

if & switch

24
Q

Does this compile?

while (false) {x=3;}

A

No, {x=3} is unreachable.