Chapter 2 - Java operators Flashcards Preview

JAVA 8 OCA > Chapter 2 - Java operators > Flashcards

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

What are the three flavors of operators available in Java?

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

2

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.

3

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.

4

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.

5

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.

6

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.

7

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.

8

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.

9

When references are equal?

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

10

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
String
enum values

11

Are boolean and long supported by Switch statements?

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

12

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.

13

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.

14

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.

15

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.

16

Can loop variables be used outside the loop?

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

System.out.print(x);

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.

17

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.

18

Is String mutable or immutable?

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

Also final is immutable.

19

What does the String pool contain?

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

Example:

String name = "Fluffy"

20

What does lenght() do?

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

21

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.

22

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

Starting at zero (0).

23

What does IndexOf( )do?

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

24

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.

25

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).

26

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


animals

Having this string: animals

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

27

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


animals

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

28

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

Animals

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

29

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

Animals

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

30

Is StringBuilder mutable or immutable?

The StringBuilder class is immutable.