Chapter 2 - Operators Flashcards

(19 cards)

1
Q

Operators precedence?

A

Post unary -> exp++, exp—
Pre unary -> ++exp, —exp
Other unary -> -, !, ~, +, (type) RL*
Cast -> (Type) reference RL*
Multiply/divide/modulus -> , /, %
Add/subtract -> +, -
Shift -> «,»,»>
Relational -> <, >, <=, >=, instanceof
Equal to/not equal -> ==, !=
Logical AND -> &
Logical exclusive OR -> ^
Logical inclusive OR -> |
Conditional AND -> &&
Conditional OR -> ||
Ternary -> exp?exp1:exp2 **
RL
Assignment -> =, +=, -=, =, /=, %=, &=, ^=, |=, «=,»_space;=,»_space;>= **RL
Arrow -> -> ***RL

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

Can we use brackets instead of parentheses in expressions?

A

No. Brackets are not recognized in expressions and will result in compile error when used

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

What is the result of below
-7%-5

A

-2
Negative sign on the divisor is always ignored when modulus is used.

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

Numeric promotion

A

1) if 2 values are different data types, Java will automatically promotes smaller variable to higher datatype

2) if integral and floating point are combined in an expression, Java will automatically promotes integral value to floating

3) if smaller data types such as byte, short and char are used in binary arithmetic expression, Java will promote them to int. Even in case that both operands are byte, short or char

4) resulting value has the promoted datatype

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

Is this valid?
long x = 10;
Int y = 2;

y = x * y;

A

No.
Y is promoted to long and result is 20 which is long. Result is being assigned to an int without casting.

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

Is this valid?
long x = 10;
Int y = 2;

y *= x;

A

Yes.
In this case y on right side is promoted to long and the result is long 20 but result is implicitly cast to int.
Special scenario in compound assignment operation

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

Is this valid? What it evaluates to?

String name=null;
name instanceof Object
null instanceof Object

A

Compiles fine.
It returns false. null is being as an object reference still

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

What this evaluates to

String title = “Venky”
title instanceof null

A

Doesn’t compile.
Compiler thinks it’s foolish to even check a reference to be instance of null (not being a class)

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

What this evaluates to?

Integer x=20;
x instanceof Date

A

Doesn’t compile.
Since compiler knows x being an integer can’t possible be a Date

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

Different categories of operators

A

1)Unary
2)Binary (arithmetic, relational, logical, conditional)
3)Ternary (compound and lambda)

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

Logical operators?

A

Logical AND - &
Logical OR - |
Logical exclusive OR - ^

They act like conditional operators when applied on boolean values.
When applied on numerical data types they act like bitwise operators.

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

What is the value of y

Int x=102
Int y=~x

What is the value of z
Int z = x | y

What is the value of z
Int z = x & y

What is the value of z
Int z = x ^ y

A

-103
-1
0
-1

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

Conditional operators

A

&&
||

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

What is the output

Int rabbit=6;
Boolean bunny= (rabbit>=6) || (++rabbit <=7);

System.out.println(bunny);
System.out.println(rabbit);

A

true
6

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

What is the output

Int count=3;
Int x=count < 5? 4 : “More”;
System.out.println(x);

A

Doesn’t compile
Because compiler knows there is a possibility that count may be greater than 5 and evaluating to string which cannot be put in integer data type

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

What data types are allowed in the blank

byte apples=5;
Short oranges=7;
________ bananas = apples + oranges;

A

int
long
float
double

17
Q

Assignment operators

long a = 10;
a += 3.2

long b = 23:
b = b * 1.0;
What is the value of a and b

A

a=13
b Doesn’t compile
When it comes to assignment operators such as this , it requires an explicit type casting.

18
Q

What is the values of a and b at the end of the code block?

Int a=2;
Int b=4;
b += 2 + a++;
a *= 3;
b += (long) 2;

19
Q

What is the value of a?

Int a = (byte)(Byte.MAX_VALUE + 1)

A

-128

The range is
-128, -127……. 0, 1, ……… 127
When you add 1 to 127 it just wraps around to -128. Imagine it as a loop.