Output of Java program | Set 17
Last Updated :
30 Apr, 2019
1) What is the output of following program?
public class Test
{
private static float temp()
{
public static float sum = 21 ;
return (--(sum));
}
public static void main(String[] args)
{
Test test = new Test();
System.out.println(test.temp());
}
}
|
a) 21
b) 20
c) Compilation error
d) Runtime error
Ans. (c)
Explanation: static variables are associated with the class and are therefore, not allowed inside a method body.
2) What is the output of the following program?
public class Test
{
public static void main(String[] args)
{
int value = 3 , sum = 6 + -- value;
int data = --value + ++value / sum++ * value++ + ++sum % value--;
System.out.println(data);
}
}
|
a) 1
b) 2
c) 0
d) 3
Ans. (b)
Explanation: Refer to Operator precedence rule in java.
3) What is the output of the following program?
public class Test
{
public static void main(String[] args)
{
int temp = 40 ;
if (temp == 30 && temp/ 0 == 4 )
{
System.out.println( 1 );
}
else
{
System.out.println( 2 );
}
}
}
|
a) 1
b) 2
c) Runtime Exception of java.lang.ArithmeticException
d) Compilation error due to divisibility by 0
Ans. (b)
Explanation: && operator is evaluated from left to right. If the first expression of && operator evaluates to false, then the second operator is not evaluated. There is no compilation error because divide by 0 is a runtime exception.
4) What is the output of the following program?
public class Test
{
public static void main(String[] args)
{
int temp = 9 ;
int data = 8 ;
System.out.println(temp & data);
}
}
|
a) 9
b) 8
c) 1000
d) 1001
Ans. (b)
Explanation: & operator is logical bit-wise and operator in java. The and of 9(1001) and 8(1000) is 1000 which is 8.
5) What is the output of the following program?
public class Test
{
public static void main(String[] args)
{
int temp = null ;
Integer data = null ;
System.out.println(temp + " " + data);
}
}
|
a) null null
b) Compilation error due to temp
c) Compilation error due to data
d) Runtime error
Ans. (b)
Explanation: temp is a primitive data type. Primitive data types cannot be assigned null values. data is an instance of class Integer and therefore can hold null values.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...