Ch 2 - Operators and Statements Flashcards
Name the three types of Java operators
unary
binary
ternary
All arithmetic operators may be applied to any java primitives, except which types?
boolean and string
Only which operators can be applied to String values?
+ and +=
Name the two short circuit logical operators
|| and &&
Name the logical operators and what they are used for
& - AND - only true if both operands are true
| - INCLUSIVE OR - only false if both operands are false
^ - EXCLUSIVE OR - only true if the operands are different
What is the resulting data type of the equation x * y where:
int x = 1;
long y = 33;
since one of the values is long and the other is int, the int value is promoted to a long.
What is the data type of x + y, where
double x = 39.21;
float y = 2.1;
This is a trick questions because y is not properly formatted. Floating point literals are assumed to be double, unless postfixed with an f, as in 2.1f.
What is the data type of x / y, where
short x = 10;
short y = 3;
x and y will both be promoted to int before the operation, resulting in an output type of int.
What is the data type of x * y / Z, where
short x = 14;
float y = 13;
double z = 30;
x will automatically be promoted to int solely because it is a short and is being used in an arithmetic binary operation. The promoted x value will then be automatically promoted to a float so that it can be divided with y. The result of x * y will then be automatically promoted to a double, so that it can be multiplied with z, resulting in a double value.
Name the four numeric promotion rules:
- if two values have different data types, java will automatically promote one of the values to the larger of the two data types.
- If one of the values is integral and the other is floating-point, java will automatically promote the integral value to the floating-point value’s data type.
- Smaller data types, namely byte, short, and char, are first promoted to int any time they’re used with a java binary arithmetic operator, even if neither of the operands is int.
- After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.
Given the following code, provide the values for x and y: int x = 3; int y = ++x * 5 / x-- + --x; System.out.println("x = " + x); System.out.println("y = " + y);
x = 2 y = 7
How do you fix the following lines of code: int x = 1.0; short y = 1921222; int z = 9f; long t = 192301398193810323;
int x = (int) 1.0;
short y = (short)1921222; // Stored as 20678
int z = (int)9f;
long t = 192301398193810323L;
Given the following code, provide the values for x and y: long x = 5; long y = (x=3); System.out.println(x); System.out.println(y);
x = 3 y = 3
What is the output of the following code: boolean b = false; if (b = true) { System.out.println("its true"); } else { System.out.println("its not true"); }
its true
What is the output of the following code: boolean b = true; if (b = false) { System.out.println("its true"); } else { System.out.println("its not true"); }
its not true
What is the output of the following code: int x1 = 1; if (x1) { System.out.println("its true"); } else { System.out.println("its not true"); }
Compiler error on this line:
if (x1) {
What is the output of the following code: int x1 = 1; if (x1 = 5) { System.out.println("its true"); } else { System.out.println("its not true"); }
Compiler error on this line:
if (x1 = 5) {
The short-circuit operators are && and ||. They are nearly identical to the & and | logical operators except…
the right hand side of the expression may never be evaluated if the final result can be determined by the left hand side of the expression.
What is the output of the following code?
int x = 6;
boolean y = (x >= 6) || (++x <= 7);
System.out.println(x);
Because x >= 6 is true, the increment operator on the right-hand side of the expression is never evaluated, so the output is 6.
true/false: the following code compiles:
boolean x = true == 3;
false