Shared Flashcard Set

Details

Java Cert - OCAJ-SE8 - Chapter 2 - Operators and Statements
Flashcards for If-Else, Switch, While Loop
37
Software
Professional
12/27/2017

Additional Software Flashcards

 


 

Cards

Term
What is the order of operator precedence?
Definition

1. Post-unary (int++, int--)

2. Pre-unary (++int, --int)

3. Other unary (~, -, !)

4.Multiplication/Division/Modulus (*, /, %)

5.Addition/Subtraction (+, -)

6.Shift (<<, >>, >>>)

7.Relational (<, >, =, !=, <=, >=, instanceof)

8.Equal to, not equal to (==, !=)

9.Logical (&, ^, |)

10.Short-Circuit (&&, ||)

11.Ternary ( (x<1) ? 1:0)

12.Assignment (=, +=, -=, *=, /=, %=) 

 

in other words, remember

1. UNARY - Post, pre, other

2. PEMDAS - (Parens, Multiplication, Division, Addition, Subtraction)

3. (Sh)RELSTA - (Relational, Equality, Logical, Short-Circuit, Ternary, Assignment)

 

 

 

 

 

 

 

Term
What are the rules for numeric promotion?
Definition

1. If two values have different data types, Java will automatically promote one of the values to the type of the larger one.

Int x = 1;

Long y = 33;

X is promoted to long, and the result is long

 

2. If one of the values is integral and the other is floating-point, Java will automatically promote the integral value to the floating-point value's data type.

int i1 = 1;

double d2 = 2;

Int is promoted to double and the result is double.

 

3.  Smaller data types, namely byte, short, char, are first promoted to int any time they are used with a Java Binary arithmetic operator, even if neither of the operands is int.

short s1 = 1;

short s2 = 3;

both of these are promoted to int and the result is int.

 

4. After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as the as its promoted operands.

see above

Term
What is one of the advantages of compound assignment operators?
Definition

Explicit casting

so if you have;

int x = 1;

long y = 10;

x *= y;

This will promote the int var x to long, it will calculate the result as a long and automatically downcast it to an int.

Term

What will be printed in the following statements?

 

if(hourOfDay <= 11)

     System.out.println("Print 1");

     System.out.println("Print 2");

Definition

If hourOfDay is > 11, Only "Print 2" will be printed.

This is because there are no block braces surrounding the println statements, the second println will always print, but the first println is the one affected by the if statement .

Term

True or false - all the following statements are legal in Java;

 

Int x = 1;

1. If(x){

}

2. If(x=6){

}

Definition
False - these s=if statements will not compile, if statements require a Boolean expression, 1. Is evaluating an int, and 2. Is assigning a variable value to x.
Term
True or false - in a switch statement, if no valid/matching case statement is found, and there is no default statement, the switch block is skipped.
Definition
True
Term
What are valid data types in switch statements?
Definition
byte, char, short, int, enum, String, Byte, Character, Short, Integer.
Term
What are the valid data types for a case statement?
Definition

The only data types you can have in a case statement are compile time constants - literals,  enum constants, or final constant variables of the same data type.

Final constant means that this variable must have been marked final and initialized with the same data type as the switch in-line with the declaration. 

Term
True or false - switch statement default statements are required to be the last of the case statements.
Definition
False, they can be anywhere in the switch block.
Term
True or false - if you leave out the break statement in a case, it will still exit out of the block after it executes the code for the matching case.
Definition
False - if you leave out breaks, once it finds the matching case statement, it will execute that case's code and then all the code for all the cases after, if they do not have breaks as well.
Term

In a switch case statement, if one of the input params coming into a method that has a switch/case block is marked as final, this value will compile as a valid case compile time constant.

 

public void getVal(final String inputStr){

     String swatch = "D";

     String Val = "";

     switch(swatch){

          case(inputStr){

              Val = inputStr;

              break;

          }

 

     }

     Return Val;

}

Definition
False, because it came into the method as a parameter marked final, it is not a constant, and will not compile.
Term

True or false - the following while loop will compile.

 

while x < 2{

}

Definition
False, the expression in the while needs to have parentheses.
Term

True or false - the following code for the do while will compile.

Boolean b = true;

do

     System.out.println("yellow");

while(b)

b= false;

Definition
True.  You are allowed to remove the braces for do while statements that only have one line of code.  However there is no control statement for this do while and it will run forever.
Term
What are the elements of a for loop?
Definition

1. For

2. Parens

3. Initialization of counter/control

4. Boolean expression (usually of counter)

5. Update counter/control

6. Code block

Term
True or false - In a for loop, the Boolean expression is evaluated, the update expression is performed and then the code block is run.
Definition
False, the Boolean expression is evaluated, the code block is run, and finally the counter is updated.
Term

True or false - the following for loop code will compile;

 

for( ; ; ){

 

Definition
True - this is an infinite loop, and the only required elements in the parentheses are the semi-colons.
Term

True or false - the following for loop will not compile.

 

int x = 0;

 

for( long y = 0, z = 4; x < 5 && y < 10; x + +, y + +) {

     System.out.print( y + " ");

}

Definition

False - this will compile!

1. You can use variables declared before the for loop in the Boolean expression.

2. Different data types can be used in the Boolean expression, remember the rule of automatic promotion

3. Multiple Boolean expressions can be evaluated as long as valid relational operators are used for the evaluations

4. Multiple variables of different data types can be evaluated in the update component

Term

True or false - the following for loop will compile;

 

int x = 0;

for(long y = 0, x = 1; ; ){

 

}

Definition

False, you cannot redeclare a variable in the for loop declaration section.

 

The code can be fixed by;

int x = 0;

long y = 0;

for(y =1, x = 2; ; ){

 

}

Term

True or false - in the for loop initialization block, you can initialize and declare different data types.

 

for(long y = 0, int x = 1; ; )

Definition
False - the initialization statement has to be a single data type when declaring.
Term

True or false - the following for loop code will compile;

 

for(int x =0; ; ){

 

}

System.out.println(x);

Definition
False - you cannot use a variable declared in the initialization section outside of the loop code block.
Term

True or false - the following two enhanced for loop blocks will compile;

 

1. String names = "Charlie";

for(String name : names){

 

}

 

2. String [] names = new String[3];

names[0] = "Charlie";

for(int name : names){

}

Definition

False - for the first one, String does not implement the iterable interface.

 

for the second one, the wrong data type is used to define an instance of the collection, its an int instead of a String.

Term

Please list the control flow statements that allow the following;

1. Allows optional labels

2. Allows unlabeled breaks

3. Allows continues

Definition

1. Optional labels - if-then, do while, while, for, switch-case

2. Unlabeled breaks - Do while, while, for, switch-case

3. Continue - do while, while, for

Term
True or false - in an enhanced for loop, the reference on the left-side only needs to be a superclass of the data types on the right hand-side, not an exact match.
Definition
True
Term
True or false - In an enhanced for loop, it allows access to the built-in counter of the current loop position.
Definition
False, there is no such counter or pointer.
Term
True or false - in an enhanced for loop, the terms must be compile-time constants.
Definition
False - this only applies to the case value in a case statement.
Term

True or false - in a do while loop, a Boolean express in the while will not go back into the do, if it evaluates to false.

Boolean x = false;

do System.out.println();

while(!(x = true))

Definition
True, if the while expression evaluates to false, the do code block will not run again.
Term

What does the following expression evaluate to?

 

String tiger = "Tiger";

String lion = "Lion";

1) final String s = 250 > 378 ? lion : tiger = "is bigger";

Definition

In this case TERNARY operation takes precedence.

(250 > 378 : lion ? Tiger) is done first to get not the variable tiger but the VALUE of tiger which is "Tiger". Then the value of "Tiger" is assigned to "is bigger". But this will not work because a value cannot be assigned a value.

 

final String s = "Tiger" = "is bigger";

is not valid.

 

if it were final String s = 250 > 378 : tiger ? (Lion = "is bigger"); // this would be fine.

 

Term

True or False - In a switch case statement, you can use casting and expressions to calculate compile time constant values for the cases.

i.e. 

case (int)'a':

case 1*1:

Definition
True
Term
True or false - in a switch case statement, if you repeat a case value via some final constant value declaration and also a literal, both being legit compile time constant values, both of these lines will be marked as not able to compile. However if two values are repeated and one is not a compile-time constant, the line with the value that is not a compile-time constant will not compile.
Definition
True.
Term
True of false - 1) in an enhanced for loop, you are allowed to iterate over a Map. 2) Also using a enhanced for loop does not prevent an infinite loop. 3) Also You are able to find out the number of the current iteration while iterating.
Definition
False - for all 3. 1) You cannot iterate over a Map in an enhanced for loop. 2) Enhanced for loops do prevent an infinite loop because there is no explicit condition check, and 3) you cannot find out the current loop iteration while iterating.
Term
True or false - in a for loop, when/if the Boolean expression fails or is false, the code for loop code block does not get run (again or at all) and the counter is updated.
Definition
False - when the for loop Boolean expression is false, the code block does not get run, AND the counter IS NOT updated.
Term
True or false - if you break out of a for loops code block, it will do one final update on the counter variable prior to ending the loop.
Definition
False - it will end the for loop and not update the counter variable.
Term
True or false - the keyword "for" can be used as a label in a for, do, while loop.
Definition
FALSE - "for" cannot be used as a label, but any other identifier can, i.e. "String".
Term
True or false - in a switch case, a byte can be used as the switch variable and a char literal compile-time constant can be used as a case label.
Definition
True - this will compile and run.
Term
True or false - with string concatenation using "+", this is a string adding/concatenation a new String.
Definition
False - with concatenation, StringBuilder is actually be used under the covers to append and then sending back a string.
Term

True or false - in a for loop, it is NOT legal to have a System.out.println(++i) as the counter.

i.e.

 

for(int i = 0; i<3;System.out.println(++1));

Definition
False - this is a legal for loop.
Term
True or false - if a switch case has a byte as the switch variable, which is assigned a char value of 'a' or integral match 97, when it is in the block, it will convert any char literals like 'a' to their integral (97) and then do the compare.
Definition
True
Supporting users have an ad free experience!