Chapter 2: Operators Flashcards

1
Q

Which of the following can be used with boolean variables?

==
+

!
%
~
cast with (boolean)

A

==
!
(boolean)

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

that types will allow the following to compile

byte apples = 5;
short oranges = 10;
_____ bananas = apples + oranges;

A

int
long
double

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

what changes, applied independently, will allow the following code snippets to compile?

3: long ear = 10;
4: int hearing = 2 * ear;

A

Cast ear on line 4 to int
change data type of ear on line 3 to short
cast 2 * ear on line 4 to int
change data type of hearing to long

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

what is the output?

3: boolean canine = true, wolf = true;
4: int teeth = 20;
5: canine = (teeth != 10) ^ (wolf = false);
6: System.out.println(canine + “, “ + teeth + “, “+ wolf);

A

true, 20, false

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

which of the following operators are ranked in increasing or the same order of precedence?

+, *, %, –
=, ==, !

A

all of them

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

What is the output?

1: public class MyCalculator {
2: static long addX( double d, float f) {
3: return (int) d + f;
4: }
5: main method printing a bunch of that static methods, with chained casts as inputs

system.out.print( addX( 1.4, 2.4f ) + “,”);
system.out.print(addX( 1.9, (float) 4 ) + “,”);
system.out.print(addX( (long)(int)(short) 2, (float) 4 )) ; } }

A

doesn’t compile because of line 3

the cast (int) is applied to d, not the expression d + f
since the cast operator is higher precedence than +, it is immediately applied to d

however, f is a float, so the expression as a whole is promoted to a float

while int could have been promoted to a long, float can’t be without a cast.

If there were parentheses around d + f, output would be 3, 5, 6

as 1.4 + 2.4 = 3.8 (int) 3.8 -> 3
1.9 + 4 = 5.9 (int) 5.9 -> 5
then (long)(int)(short) 2 is just 2
(float) 4 is 4.0. 2+ 4.0, with (int) thrown in = 6
3, 5, 6

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

What is the output of printing the following code snippet?

int ph = 7, vis = 2;
boolean clear = vis > 1 & ( vis < 9 || ph < 2)
boolean safe = (vis > 2) && (ph++ > 1);
boolean tasty = 7 <= –ph;

A

true
false
true

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

int pig = (short)4;
pig = pig++;
long goat = (int)2;
goat -= 1.0

print pig, goat

A

4, 1

The casting isn’t really necessary - 4 could fit in an int, but they are casting it to a short (before it promptly gets promoted to an int)

pig = pig++;
A common mistake here = since it is a post-increment, the original value of pig is assigned back to pig

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

int a = 2, b =4, c =2;
print ( a > 2 ? –c : b++); //1
print ( b = (a!=c ? a : b) ); //2
print ( a > b ? b < c ? b : 2 : 1); //3

A

the outputs are 4, 5, and 1

//1 ternary operator checks if a < 2, false so
b is printed, ‘4’, then post-incremented to 5

//2 Since a and c are both 2, the condition is false, and b is assigned to itself -> 5

//3 nested ternary operator.
The outer operation checks if a is greater than b. It’s not.
so we just skip past that inner ternary operation [ b<c ? b : 2] and print ‘1’

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

short height = 1, weight = 3;
short zebra = (byte) weight * (byte) height;

A

compilation error
* promotes the casted variables to int
which won’t fit in the short on the LHS

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

do * and % have the same precedence?

A

yes -
unless there’s parenthesis, modulus and multiply will read LTR

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

the ______ operator increases a value and returns an original value
while the ______ operator decreases a value and returns a new value

A

post-increment
pre-decrement

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

boolean x = true, y = false, z = true;
boolean a = x & y ^ z; // 1
boolean b = x && !y;
boolean c = !(a && b);

print( a, b, c)

A

true true true

//1
true & false -> false
false ^ (XOR) true -> true

the rest are self-explanatory

The && and || operators “short-circuit”, meaning they don’t evaluate the right-hand side if it isn’t necessary.

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

Select the right statement(s):

//1 the return value of an assignment operation expression is the value of the assigned variable

//2 The logical complement operator (!) cannot be used to flip numeric values

//3 the ternary operator is the only one to take 3 operands ‘ ? : ‘

A

//1 and //2 and //3
all of them are correct

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

how many lines of the following code contains compiler errors?

int x = 1 * 2 * (long) 3;
short y = (byte)(double) (x *= 2);
double z = y;
float a1 = (float) ((z == 1_000f) ? z * 2L : z);

A

the first line

When multiplied by (long)3, the whole expression is promoted to long, can’t fit into int in the LHS

The other lines store smaller values in larger or same-size data types

the last line is getting cast to float whether it’s z*2L or just z

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

int start = 7;
int end = 4;
end += ++start;
start = (byte)(Byte.MAX_VALUE + 1);

A

start is -128
end is 12
(pre-increment returns the incremented value)

17
Q

Select correct statements about unary operators?

Unary operators are always executed before any surrounding numeric binary or ternary operators

The post-decrement operator (–) returns the value of the variable before the decrement is applied

The ! operator cannot be used on numeric values

A

unary operators need only one operand to perform any operation like increment, decrement, negation, etc.

they’re all correct statements

18
Q

int r = 8;

int a = ~r;
int b = -r;

var c = a == b ? 5 : 10;
print a, b, –c;

A

-9, -8, -9

The ~ operator is a bitwise NOT operator. It inverts the bits of its operand. The binary representation of 8 is 1000. Inverting these bits gives 0111, which is the binary representation of 7. However, because integers in Java are stored using two’s complement notation, the actual value of a will be -9.

  • unneeded but cool * two’s complement =
    the most common method of representing signed integers in computers. In this method, the leftmost bit is the sign bit. If the sign bit is 0, the number is positive, and if it is 1, the number is negative. Positive numbers are represented in the regular binary form, while negative numbers are represented in the two’s complement form which is obtained by inverting all bits of the positive form and then adding 1 to the result.