Chapter 2 - Java operators Flashcards

1
Q

What are the three flavors of operators available in Java?

A

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

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

What is the order Java operators follow?

A

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

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

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

A

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

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

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

A

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

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

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

A

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

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

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

A

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.

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

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

A

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.

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

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

A

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.

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

When references are equal?

A

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

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

Which data types are supported by Switch statements?

A

Data types supported by switch statements:

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
11
Q

Are boolean and long supported by Switch statements?

A

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

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

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

A

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

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

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

for ( ; ;) {
}

A

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.

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

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++){
}

A

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

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

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++){
}

A

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

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

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

A

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
Q

What are the concatenation rules?

A
  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
Q

Is String mutable or immutable?

A

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

Also final is immutable.

19
Q

What does the String pool contain?

A

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

Example:

String name = “Fluffy”

20
Q

What does lenght() do?

A

lenght( )

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

21
Q

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

A

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

22
Q

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

A

Starting at zero (0).

23
Q

What does IndexOf( )do?

A

IndexOf( )

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

24
Q

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

A

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

25
Q

What does substring() do?

A

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
Q

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

animals

A

Having this string: animals

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

27
Q

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

animals

A

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

28
Q

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

Animals

A

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

29
Q

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

Animals

A

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

30
Q

Is StringBuilder mutable or immutable?

A

The StringBuilder class is immutable.

31
Q

Does == check object equality or the values inside the String?

A

== checks object equality.

32
Q

What does equals check?

A

The equals method checks the values inside the String instead of the object reference.

33
Q

When are Strings evaluated?

A

Strings are evaluated at compile-time, for that reason below Strings are different even though at run time evaluate to the same String:

String x = “Hello World”;
String y = “ Hello World.trim();

34
Q

Does StringBuilder include the equals method?

A

StringBuilder DOES NOT include the equals method.

35
Q

Can a StringBuilder be compared (==) with a String?

A

Java does not allow to compare a String with StringBuilder using ==. The code does not compile.