Chapter 2 : Operators & Statements Flashcards
Chapter 2
For the following code:
int x = 1;
long y = 33;
What is the data type of x * y ?
a) int
b) long
c) double
d) does not compile
b) long
If two values have different data types, Java promotes smaller data type to larger data type of the two
For the following code
double x = 39.21;
float y = 2.1;
What is the data type of x + y ?
a) double
b) float
c) exception thrown
d) does not compile
d) does not compile
Literal floating-point numbers default to double.
y = 2.1f would compile and the result of x + y would be a) due to the rule : “If two values have different data types, Java promotes smaller data type to larger data type of the two”
For the following code
short x = 10;
short y = 3;
What is the data type of x / y and result?
a) short = 3
b) int = 3
c) float = 3.333333…
d) double = 3.333333…
e) does not compile
b) int = 3
Smaller data types are always promoted to int, even when neither is int
For the following code
short x = 10;
float y = 3;
What is the data type of x / y and result?
a) short = 3
b) int = 3
c) float = 3.333333…
d) double = 3.333333…
e) does not compile
c) float = 3.333333…
If one values is integral and the other floating point then Java will promote integral to floating-point data type
float y = 3 compiles just fine even though there is no “f” after the literal, since 3 is an integral. If the number where 3.1 then we would not compile. 3.1f would compile however.
Which of the following compile, choose all that apply
a) int a = !5;
b) float b = -3;
c) float c = -0b010;
d) boolean d = -true;
e) boolean e = !false;
f) boolean f = !0;
b) float b = -3;
- 3 is an integral so this is fine. If the number was -3.1 then it would be considered double and it would not compile. However -3.1f would be fine.
c) float c = -0b010;
A binary integral. Same a b) essentially
e) boolean e = !false;
”!” operator can be used against booleans only. “-“ can only be used with numbers only
What will the following statement print?
int x = 5;
int y = x++ + x++ + x++;
System.out.println(y);
a) 15
b) 21
c) 18
d) does not compile
c) 18
Post-increment operator uses the current value of the variable and then immediately increments it (in the same statement). Therefore, the question reads as :
int y = 5 + 6 + 7
What will the following statement print?
int x = 5
int y = x++ + ++x + x++;
System.out.println(y);
a) 15
b) 19
c) 21
d) 20
b) 19
The question reads as : int y = 5 + 7 + 7
What will the following statements print?
int x = 3;
int y = ++x * 5 / x– + –x;
System.out.print(x + “ “);
System.out.println(y);
a) 4 8
b) 2 8
c) 2 7
d) 9 2
c) 2 7
int y = ++x * 5 / x– + –x;
int y = 4 * 5 / x– + –x;
int y = 4 * 5 / 4 + –x; // use 4 but decrement to x to 3
int y = 4 * 5 / 4 + 2; // x is now 2
int y = 20 / 4 + 2;
int y = 5 + 2;
int y = 7;
What will the following code print?
byte x = 2;
byte y = 4;
byte z = 2 * 4;
System.out.println(z);
a) 8
b) 1
c) exception thrown
d) does not compile
a) 8
Java relaxes its assignment conversion rule for literals. It is ok to assign a literal int value (2 * 4) to a narrower primitive type (byte, short, or char) as long as the literal value falls within the legal range of the primitive type.
http://www.java2s.com/Tutorial/SCJP/0080__Type-Casting/Javarelaxesitsassignmentconversionrule.htm
What will the following code print?
byte x = 2;
byte y = 4;
byte z = x * y;
System.out.println(z);
a) 8
b) 1
c) exception thrown
d) does not compile
d) does not compile
x * y results in an int but it cannot be placed in a byte variable z. Java does not relax is assignment conversion rule for variables, only for literal integrals.
What will the following code print?
short x = 2;
short y = 4;
short z = (byte)(x * y);
System.out.println(z);
a) 8
b) 1
c) exception thrown
d) does not compile
a) 8
It compiles. There are a few things happening here.
- x * y are turned into int because of the * operator
- casting from int to byte solves potential compilation error
- stuffing a byte into a short is ok (smaller to larger is fine)
What will the following code print?
short x = 2;
short y = 4;
x *= y;
System.out.println(x);
a) 8
b) 1
c) exception thrown
d) does not compile
a) 8
It compiles. x = x * y does not compile but… x *= y compiles because the compound assignment operator automatically casts to the correct type
What will the following code print?
short x = 2;
short y = 4;
short z = x = x = y;
System.out.println(z);
a) 4
b) 2
c) exception thrown
d) does not compile
a) 4
The result of an assignment operation returns the value being assigned. Therefore:
short z = x = x = y;
short z = x = x = 4;
short z = x = 4;
short z = 4;
What are the return values for variables a and b if:
boolean x = true;
boolean y = false;
boolean a = x ^ y;
boolean b = x ^ x;
a) a is true, b is true
b) a is true, b is false
c) a is false, b is true
d) a is false, b is false
b) a is true, b is false
The exclusive OR is true only when at most one side of the expression is true. If both sides are true (or false) then it returns false.
______|_TRUE __|_ FALSE
TRUE__|_FALSE_|_TRUE
FALSE_|_TRUE__|_FALSE
For the following statement, choose all that apply
int z = 0b101L == 5.00f ? 4 : 3;
System.out.print(z);
a) 4 is printed
b) 3 is printed
c) Does not compile
d) Exception is thrown
a) 4 is printed
0b101L is a long 5.00 is a float.
The equality operator automatically promotes the smaller numeric primitive to the larger primitive’s type