Output of Java program | Set 17

1) What is the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

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());
    }
}    

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
        }    
    }
}

chevron_right


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 right to left. 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?

filter_none

edit
close

play_arrow

link
brightness_4
code

public class Test
{
    public static void main(String[] args)
    {
        int temp = 9;
        int data = 8;
        System.out.println(temp & data);
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

public class Test
{
    public static void main(String[] args)
    {
        int temp = null;
        Integer data = null;
        System.out.println(temp + " " + data);
    }
}

chevron_right


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.

This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.