Ch3 Operators Flashcards
What is the data type of x * y?
short x = 10;
short y = 3;
var z = x * y;
x and y will both be promoted to int before the binary multiplication operation, resulting in an output type of int.
To have an output type of short again, use casting:
var z = (short) x * y;
What is the data type of w * x / y?
short w = 14;
float x = 13;
double y = 30;
var z = w * x / y;
First, w will automatically be promoted to an int because it is a short and it is being used in an arithmetic binary operation.
The promoted w value will then be promoted to a float so it can be multiplied with x.
The result of w * x will then be promoted to a double so it can be divided by y, resulting in a double value.
What is the data type of y?
short x = 10;
var y = x++;
Unary operators are excluded from the numeric promotion rules, resulting in a short data type.
Does the following code compile?
short mouse = 10;
short hamster = 3;
short capybara = mouse * hamster;
Short values are automatically promoted to in when applying any arithmetic operator, with the resulting value being an int. The code does not compile.
What is the value of wolf and coyote? why?
long wolf = 5;
long coyote = (wolf = 3);
System.out.println(wolf);
System.out.println(coyote);
- wolf = 3;
- coyote = 3;
The result of an assignment operator is an expression in and of itself, equal to the value of the assignment. wolf = 3 also returns the value 3.
What does the binary and equality operator ‘==’ do when applied to objects?
Returns true if the two values reference the same object.
What does the binary and equality operator ‘!=’ do when applied to objects?
Returns true if the two values do not reference the same object.
True or false: The following code returns true:
5 == 5.00
If the numeric values are of different data types, the values are automatically promoted.
What is the output of this code? why?
System.out.println(null == null);
In Java, comparing null with null results in true.
What is the output of this code? why?
System.out.println(null instanceof Object);
Calling instanceof on the null literal or any null reference always returns false.
What is the output of this code? why?
System.out.println(null instanceof null);
This code does not compile because null is used on the right side of the instanceof operator.
What change, when applied independently, would allow the following code to compile?
3: long ear = 10;
4: int hearing = 2 * ear;
A) No change.
B) Cast ear on line 4 to int.
C) Change the data type of ear on line 3 to short.
D) Cast 2 * ear on line 4 to int.
E) Change the data type of hearing on line 4 to short.
F) Change the data type of hearing on line 4 to long.
B, C, D, F
What is the output of the following code snippet?
4: int pig = (short) 4;
5: pig = pig++;
6: long goat = (int) 2;
7: goat -= 1.0;
8: System.out.println(pig + “ - “ + goat);
A) 4 - 1
B) 4 - 2
C) 5 - 1
D) 5 - 2
E) The code does not compile
F) None of the above
A
What is the output of the following code?
public class MyClass {
static long test(double fruit, float vegetable) {
return (int) fruit + vegetable;
}
public static void main(String args[]) {
int y = 4;
int x = (y = 1);
System.out.println(test((long)x, (float) y));
}
}
The code cannot be compiled because the cast ‘(int) fruit + vegetable’ only casts fruit to int but not the whole expression.
What is the output of the following code?
short height = 1, weight = 3;
short zebra = (byte) weight * (byte) height;
double ox = 1 + height * 2 + weight;
long giraffe = 1 + 9 % height + 1;
System.out.println(zebra);
System.out.println(ox);
System.out.println(giraffe);
The code does not compile because of the second line. multiplying weight and height will result in an int, which does not fit in a short, unless casted.