Basic Datatypes
- An expression involving byte, int, and literal numbers is promoted to which of these?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
A literal integer is always an int, but the result of an expression involving anything int-sized or smaller is always an int. In other words, add two bytes together and you'll get an int—even if those two bytes are tiny. Multiply an int and a short and you'll get an int. Divide a short by a byte and you'll get an int.
So an expression involving bytes, ints, shorts, literal numbers, the entire expression is promoted to int before any calculation is done.
An implicit cast happens when you're doing a widening conversion. In other words, putting a smaller thing (say, a byte) into a bigger container (like an int).
- Which of the following are legal lines of Java code?
1. int a = (int)888.8;
2. byte b = (byte)100L;
3. long c = (byte)100;
4. byte d = (byte)100L;
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
All four statements are correct
Option 1. is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses the digits after the decimal but doesn't throw any error.
Option 2 & option 4, are correct because a long can be cast into a byte. If the long is over 127 (byte max value), It loses its most significant (leftmost) bits.
Option 3. It works perfectly and casting is not needed, because a long can store a byte.
- Which data type value is returned by all transcendental math functions?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Only double data type value is returned by all transcendental math functions.
- Which of these methods is used to obtain value of invoking object as a long?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
long longValue() is used to obtain value of invoking object as a long.
- Which of the following is method of wrapper Integer for converting the value of an object into int?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
int intValue();