Q1. What is the range of short data type in Java?
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
d) None of the mentioned
Answer: b
Explanation: Short occupies 16 bits in memory. Its range is
from -32768 to 32767.
Q2. What is the range of byte data type in Java?
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
d) None of the mentioned
Answer: a
Explanation: Byte occupies 8 bits in memory. Its range is
from -128 to 127.
Q3. An expression involving byte, int, and literal numbers is
promoted to which of these?
a) int
b) long
c) byte
d) float
Answer: a
Explanation: An expression involving bytes, ints, shorts,
literal numbers, the entire expression is promoted to int before any
calculation is done.
Q4. Which of these literals can be contained in float data type
variable?
a) -1.7e+308
b) -3.4e+038
c) +1.7e+308
d) -3.4e+050
Answer: b
Explanation: Range of float data type is -(3.4e38) To
+(3.4e38)
Q5. Which data type value is returned by all transcendental math
functions?
a) int
b) float
c) double
d) long
Answer: c
Explanation: Only double data type value is returned by all
transcendental math functions. Transcendental math functions don’t return int
or long. They return double instead of float as double has larger range.
Q6. Which of the following are legal lines of Java code?
1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;
a) 1 and 2
b) 2 and 3
c) 3 and 4
d) All statements are correct
Answer: d
Explanation: Statements (1), (2), (3), and (4) are correct. (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. (2) and (4) are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant (leftmost) bits. (3) actually works, even though a cast is not necessary, because a long can store a byte.
Q7. What will be the output of the following Java code?
class average {
public static void main(String args[])
{
double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
double result;
result = 0;
for (int i = 0; i < 6; ++i)
result = result + num[i];
System.out.print(result/6);
}
}
a) 16.34
b) 16.566666644
c) 16.46666666666667
d) 16.46666666666666
Answer: c
$ javac average.java
$ java average
16.46666666666667
Q8. What will be the output of the following Java statement?
class output {
public static void main(String args[])
{
double a, b,c;
a = 3.0/0;
b = 0/4.0;
c=0/0.0;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
a) Infinity
b) 0.0
c) NaN
d) all of the mentioned
Answer: d