Chapter 3 Flashcards
What is the output of each print line?
int parkAttandance = 0;
System.out.println(++parkAttandance);
System.out.println(parkAttandance);
System.out.println(parkAttandance–);
System.out.println(parkAttandance);
- 1
- 1
- 1
- 0
What is the output?
int lion = 3;
int tiger = ++lion * 5 / lion–;
System.out.println(“lion is “ + lion);
System.out.println(“tiger is “ + tiger);
- lion is 3
- tiger is 5
int tiger = ++lion * 5 / 3; //lion is assigned value of 2
int tiger = 3 * 5 / 3; //lion is assigned value of 3
What is the data type of x * y?
int x = 1; long y = 33; var z = x * y;
Since one of the values is long and the other is int and since long is larger than int, the int value is promoted to a long and the resulting value is long.
What is the data type of x + y?
double x = 33.21; float y = 2.1; var z = x + y;
This code will not compile because float y = 2.1
is required to be postfixed with an ‘f’. Floating-point literals are by default a double.
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 is the output of this code? why?
boolean test = false; if (test = true) { System.out.println("true"); } else { System.out.println("false"); }
The variable test is assigned a value of true (instead of testing if it is true), which also returns true. The output of this code is “true”.
What does the binary and equality operator ‘==’ do when applied to primitives?
Returns true if the two values represent the same value.
What does the binary and equality operator ‘!=’ do when applied to primitives?
Returns true if the two values represent different values.
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.