Java Basic Online Test – 1

1. Which of these literals can be contained in float data type variable?
A.+1.7e+308
B.-1.7e+308
C.-3.4e+050
D.-3.4e+038

Correct Answer:C.(Range of float data type is -(3.4e38) To +(3.4e38))
Advertisement

2. Which of the following are legal lines of Java code?
1.int w=(int)888.8;
2.byte x=(byte)100L;
3.longy= (byte)100;
4.byte z=(byte)1000L;

A.3 and 4
B.2 and 3
C.All statements are correct
D.1 and 2

Correct Answer:C.(Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point number (adouble in this case) is cast to an int, it simply loses the digits after the decimal. (2) and (4) are correct because along can be cast into a byte. lf the long is over 127, it loses its precision due to narrowing of the conversion. (3)actually works, even though a cast is not necessary, because a long can store a byte.)
Advertisement

3. What is the range of short data type in java?
A.-2147483648 t0 2147483647
B.None of the mentioned
C.-128 to 127
D.-32768 to 32767

Correct Answer:D.(Short occupies 16 bits in memory, lts range is from -32768 to 32767.)
Advertisement

4. 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.0.0
B.NaN
C.all of the mentioned
D.Infinity

Correct Answer:C.(For floating point literals, we have constant value to represent (1 0/0.0) infinity either positive ornegative and also have NaN (not a number for undefined like 0/0.0), but for the integral type, we don’t have anyconstant that’s why we get an arithmetic exception.)
Advertisement

5. What will be the output of the followingjava 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.46666666666666
B.16.34
C.16.46666666666667
D.16.566666644

Correct Answer:C.(None.
output:
$ javac average.java
$ java average
16.46666666666667.)
Advertisement

6. What will be the output of the following Java code?
class area {
public static void main(String args[])
{
double r, pi, a;
r=9.8;
pi= 3.14;
a=pi*r*r;
System.out.println(a);
}
}

A.301.56560000*
B.301
C.301.5656
D.301.56

Correct Answer:C.(None.
output:
$ javac area.java
$ java area
301.5656)
Advertisement

7. Which data type value is returned by all transcendental math functions?
A.float
B.double
C.int
D·long

Correct Answer:B.(Only double data type value is returned by all transcendental math functions. Transcendentalmath functions don’t return int or long. They return double instead of float as double has larger range.)
Advertisement

8. What is the range of byte data type in java?
A.-32768 to 32767
B.-128 to 127
C.-2147483648 to 2147483647
D.None of the mentioned

Correct Answer:B.(Byte occupies 8 bits in memory. lts range is from -128 to 127)
Advertisement

9. What will be the output of the following Java code?
class increment {
public static void main(string args[])
{
int g= 3;
System.out.print(++g* 8);
}
}

A.25
B.32
C.24
D.33

Correct Answer:B.(Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32
output:
$ javac increment.java
$ java increment
32)
Advertisement

10. An expression involving byte, int, and literal numbers is promoted to which of these?
A.byte
B.float
C.long
D.int

Correct Answer:B.( An expression involving bytes, ints, shorts, literal numbers, the entire expression is promoted toint before any calculation is done.)
Advertisement

Leave A Comment?