Open In App
Related Articles

Output of Java program | Set 17

Improve Article
Improve
Save Article
Save
Like Article
Like

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.

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 30 Apr, 2019
Like Article
Save Article
Previous
Next
Similar Reads