Chapter 2: Operators Flashcards
Which of the following can be used with boolean variables?
==
+
–
!
%
~
cast with (boolean)
==
!
(boolean)
that types will allow the following to compile
byte apples = 5;
short oranges = 10;
_____ bananas = apples + oranges;
int
long
double
what changes, applied independently, will allow the following code snippets to compile?
3: long ear = 10;
4: int hearing = 2 * ear;
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
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);
true, 20, false
which of the following operators are ranked in increasing or the same order of precedence?
+, *, %, –
=, ==, !
all of them
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 )) ; } }
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
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;
true
false
true
int pig = (short)4;
pig = pig++;
long goat = (int)2;
goat -= 1.0
print pig, goat
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
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
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’
short height = 1, weight = 3;
short zebra = (byte) weight * (byte) height;
compilation error
* promotes the casted variables to int
which won’t fit in the short on the LHS
do * and % have the same precedence?
yes -
unless there’s parenthesis, modulus and multiply will read LTR
the ______ operator increases a value and returns an original value
while the ______ operator decreases a value and returns a new value
post-increment
pre-decrement
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)
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.
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 ‘ ? : ‘
//1 and //2 and //3
all of them are correct
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);
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