Chapter 2 Flashcards

1
Q

Order of operators?

A
Post unary: x++, x--
Pre unary: ++x, --x
Other unary: +,-,!
Multiplication etc: *,/,%
Addition etc: +,-
Shift operators: <>,>>>
Relational operators: ,<=,instanceof
Equal to etc: ==, !=
Logical operators: &amp;,^,|
Short circuit: &amp;&amp;, ||
Ternary: boolean?exp1:exp2
Assignment ops: =,+=,*= etc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

4 numeric promotion rules:

A

Java promotes to larger of the data types.
Integral promoted to floating point.
Byte, short and char promoted to Int first, even if none are Int.
The result has same data type as promoted operands.

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

What is data type of x * y / z?
short x = 14;
Float y = 13;
Double z = 30;

A

X promoted to Int, then to float. Result of x*y promoted to double. Result is a double.

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

–x means?

A

Operator applied first, value return is the new value.

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

X–

A

Original value is returned, then the operator is applied.

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

Int x = 3;
Int y = ++x * 5 / x– + –x;
X?
Y?

A

X is 2

Y is 7

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

short z = x * y;

Doesn’t compile, why?

A

Because short values are automatically promoted to Int when applying arithmetic operator. And short variable cannot be set to Int value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Which works?
long x = 10;
int y = 5;
A) y *= x;
B) y = y * x;
A

A works. B does not compile.

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

Is this valid:

long x = (x = 3);

A

Yes

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

X ^ Y = ?

1) t t
2) t f
3) f t
4) f f

A

1) false
2) true
3) true
4) false

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

Difference between & an &&?

A

&& short circuits and right hand side of a statement may never be evaluated.

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

Why does this not through exception:
Integer x;
If(x != null && x.getValue() < 5) {}

A

Because && short circuit prevents x.getValue() from ever being reached.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
File x = new File("s.txt");
File y = new File("s.txt");
File z = x;
A) x == y ?
B) x == z ?
A

Checks references point to same object, not for object equivalence. So…
A) false
B) true

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

Write the following as if else’s:
Int y = 10;
Int x = (y > 5) ? (2 * y) : (3 * y);

A
Int y = 10;
Final Int x;
If (y > 5) {
 X = 2 * y;
} else {
 X = 3 * t;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What target types can a switch statement have?

Seitch(targetType) {}

A

int and all primitives that can be promoted to int (byte, short, char). Also enum. Also Byte, Short, Charachter, Integer. Also String.

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

Is this legal?

for(;;){ }

A

Yep

17
Q

Is this legal?

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

A

Yep