Chapter 2 - Java operators Flashcards Preview

JAVA 8 OCA > Chapter 2 - Java operators > Flashcards

Flashcards in Chapter 2 - Java operators Deck (35)
Loading flashcards...

What are the three flavors of operators available in Java?

three flavors of operators are available in Java: unary, binary and ternary.


What is the order Java operators follow?

Java operators follow order of operation. If 2 operators have the same level of precedence, then Java guarantees left-to-right evaluation.


Arithmetic operators may be apply to? And except to which type?

All of the arithmetic operators may be applied to any Java primitives. EXCEPT boolean and String.


Which are the only arithmetic operators that can be applied to Strings?

Only the addition operators + and += may be applied to String values, which result in String concatenation.


In numeric promotion, if two values have different data types, what Java does?

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


In numeric promotion, if one of the values is integral and the other is floating point, what will Java do?

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.


In numeric promotion, to which type are smaller data types (byte, short and char) promoted to?

Smaller data types, namely byte, short and char, are first promoted to int any time they're used with Java binary arithmetic operator, even if neither of the operands is int. **important: unary operators are excluded from this rule. I.e: applying ++ to a short value results in a short value.


What is the resulting value after a promotion has been done and all the operants have the same data type?

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


When references are equal?

Two references are equal if and only if they point to the same object, or both point to null.


Which data types are supported by Switch statements?

Data types supported by switch statements:

byte and Byte
short and Short
char and Character
int and Integer
enum values


Are boolean and long supported by Switch statements?

No. Boolean and long and their associate wrapper classes are not supported by switch statements.


What is the variable’s coverage in the initialization of a FOR loop?

Variables declared in the initialization block of a for loop have limited scope and are only accessible within the for loop.


Does below code compile? If so, which will be the output?

for ( ; ;) {

The above will compile and run without issue. The output is an infinite loop.

Components of the for loop are each optional BUT the semicolons ; are required as for ( ) will not compile.


In a FOR loop, can a variable be redeclare in the initialization block (i.e. x below)

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

No, the above will not compile because x is initialized 2 times; outside of the for and inside the for statement.


Can different data types be used in the initialization block of a FOR loop?

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

No, the variables in the initialization block must all be of the same type.


Can loop variables be used outside the loop?

for(int y = 0, int x = 4; x < 5 && y < 10; x++, y++)


No. Using loop variables outside the loop make the code to not compile. In the above example x is a local variable to the for and cannot be outside.


What are the concatenation rules?

1. If 2 numbers are involved, use numeric addition.

2. If strings and numbers are involved, use concatenation.

3. Evaluate the expression from left to right.


Is String mutable or immutable?

String is immutable meaning it cannot be changed once it is created.

Also final is immutable.


What does the String pool contain?

Also known as "intern pool". Contains literal values that appear in your program.


String name = "Fluffy"


What does lenght() do?

lenght( )
Counts the number of characters in a string. It counts from 1 to n.


Does lenght() consider what is in an array or how many slots have been allocated?

Length does not consider what is in the array; it only considers how many slots have been allocated.


While using indexes or positions within a list Java counts starting at which position?

Starting at zero (0).


What does IndexOf( )do?

IndexOf( )
This method looks at the characters in the String and finds the first index that matches the desired value.


What is the value returned by the IndexOf() method if a match is not found?

IndexOf() does not throw an exception if a match is not found. It returns -1 instead.


What does substring() do?

Substring( )
Looks for characters in a string.
The first parameter is the index to start which is a zero-based index.

There is an optional second parameter which is the end of the index you want to stop at (it is not included in the returned string).


What does substring(3, 3) return having below String?


Having this string: animals

...substring(3, 3) returns empty string because there are no characters in the middle.


What does substring(3, 2) return having below String?


substring(3, 2) throws an exception because the end index cannot be less than the initial index.


What does substring(3,8) return having below String?


substring(3,8) throws an exception because the original string does not have position 8.


What does substring(3, 7) return having below String?


substring(3, 7) does not throw an exception because there is the "end of string" invisible position.


Is StringBuilder mutable or immutable?

The StringBuilder class is immutable.