Chap 4 - Operators Flashcards

1
Q

int a = 1
int b = ++a
int c = a++

System.out.println(“a = “ + a);
System.out.println(“b = “ + b);
System.out.println(“c = “ + c);

A

a = 3
b = 2
c = 2

c sets to a before it’s incremented

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

What does % operator do?

Called modulus operator

A

Returns the remainder of a division operation

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

What is Twos Compliment?

A

Encoding for negative numbers

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

For binary code, change 42 to -42.

A

00101010 = 42
11010110 = -42

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

List steps for changing number to its negative in binary

A

1) invert (0s to 1s and visa versa)
2) add 1

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

Which bit tells you the sign of an integer?

A

highest order bit, on the left

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

Bitwise operators, explain

&
|
~

A

AND, OR, NOT

AND says if both are 1
OR says if either are 1
NOT inverts the 1<>0

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

Write the bitwise OR operator

A

|

00101010
| 00001111
—————
00101111

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

What is bitwise XOR operator and what does it do?

A

Says if exactly one is 1/TRUE

00101010
^ 00001111
—————
00100101

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

What does left shift operator &laquo_space;do?

A

shifts all bits in a value to the left a specified number of times

value &laquo_space;num

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

Bye and short values are promoted to what when an expression is evaluated?

A

int

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

Each &laquo_space;has the effect of what on the original value?

A

Doubling, except when a 1 gets shifted off the right, then its a problem

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

byte a = 64, b;
int i;

i = a &laquo_space;2;
b = (byte) (a &laquo_space;2);

// print i and b

A

i = 256
b = 0


0100 0000 is 64. Shift left 2 is
1 0000 0000

That number is 256 as an int
Buts its 0 as a byte b/c the leftmost 1 is shifted out / too many numbers for the variable size

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

Explain what happens with&raquo_space; operator, how the top bit is filled, and what the outcome is

A

Bitwise operator that shifts each bit to the right.
Top bit is filled with the previous content of the top bit
Outcome divides value by two and discards remainder

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

What does&raquo_space;> do?

A

unsigned shift-right operator

Shifts bits to the right and 0 into the top bit, unlike&raquo_space; which maintains the sign for the top bit

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

Bitwise operator compound assignments

int a = 1, b=2, c=3;

a |= 4;
b&raquo_space;= 1;
c «= 1;
a ^= c;

// print a b c

A

a = 3
b = 1
c = 6

17
Q

int a = 4, b =1;
boolean c = a < b;

// print c

A

FALSE

18
Q

What are short-circuit logical operators? And why use it?

e.g. || &&

A

For boolean values, these will stop evaluating the expression if the outcome can be determined by the left operand alone

Use it to avoid errors like, if denominator is not zero.
if (denom != 0 && num / denom > 10)

// if denom was zero, would cut out so dont get runtime error dividing by 0

19
Q

Explain how the ? operator works

A

It’s a three-way operator like the IF function in excel. If a condition is true, return first, if not return second.

int i, k;
i = 10;

k = i < 0 ? -i : i;
//prints i = 10

i = -10;
k = i < 0 ? -i : i;
//prints -i = 10

20
Q

What is a relational operator versus a logical operator?

A

Relational evaluate relationship between two objects

Logical calculate a boolean value between boolean objects

21
Q

In binary operations, order of evaluation is from ____ to ____ except for assignments which are ____ to ____

Left / Right?

A

Left to right except for assignments which go right to left

22
Q
A