Output of Java program | Set 28

Question 1. What is the output of the following question?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test {
    int a = 10;
    static int b = 20;
  
public
    static void main(String[] args)
    {
        Test t1 = new Test();
        t1.a = 100;
        t1.b = 200;
  
        Test t2 = new Test();
  
        System.out.println("t1.a =" + t1.a + " t1.b =" + t1.b);
        System.out.println("t2.a =" + t2.a + " t2.b =" + t2.b);
    }
}

chevron_right


Option
A) t1.a=100 t1.b=200
t2.a=10 t2.b=200

B) t1.a=10 t1.b=200
t2.a=10 t2.b=200

C) t1.a=100 t1.b=200
t2.a=10 t2.b=20

D) t1.a=100 t1.b=200
t2.a=100 t2.b=200

Output: A

Explanation : static variable is class level variable. If we create multiple objects of class and all objects can point same reference of static variable means If you can change the value of static variable in any object, then compiler automatically updates the value of all object static variables.

Question 2. What is the output of the following question?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test2 {
public
    static void main(String[] args)
    {
        byte x = 12;
        byte y = 13;
        byte result = x + y;
        System.out.print(result);
    }
}

chevron_right


Option
a) 25
b) Error
c) -25
d) none

Output : b

Explanation : If we apply any arithmetic operator between two variables x and y, then the result type is max(int, type of x, type of y). Therefore, here the compiler will give the error possible lossy conversion int to byte.

Question 3. What is the output of the following question?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test3 {
public
    static void main(String[] args)
    {
        int x = 011;
        int y = 0xfee;
        int result = x + y;
        System.out.print(x + ":" + y + ":" + result);
    }
}

chevron_right


Options
a) Error
b) 010 : 0xfee : 4089
c) 9 : 4078 : 4087
d) 010 : 0xfee : 4087

Output : C

Explanation : If any integer number start with 0, then that number is treated as octal number hence 011 means (1*8^1 + 1*8^0) 9 and also if any integer start with 0x means then that number is treated as Hexadecimal number means we can have the value between[0-9] and [a-z or A-Z]. oxfee =(15*16^2 + 14*16^1 + 14*16^0) =>4078.

Question 4. What is the output of the following question?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test4 {
public
    static void main(String[] args)
    {
        int x = 0198;
        int y = 0xfree;
        int result = x + y;
        System.out.print(x + " : " + y + " : " + result);
    }
}

chevron_right


options
a) Error
b) 0198 : 0xfree : 68734
c) 144 : 68590 : 68734
d) 0198 :68590 : 68788

Output : a

Explanation : If we assign a 0 before integer number, then that number is treated as octal and we know octal number range is [0-7]. If we assign the number outside of this range then we get error. Similar to this, if any integer number start with 0x then that number is treated as Hexadecimal means we can assign the value in between the [0-9] [a-z or A-Z]. If we assign it out of range, then program will get the error.

Question 5. What is the output of the following question?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test5 {
public
    static void main(String[] args)
    {
        final int a = 1, b = 5;
  
        for (int i = 0; a < b; i++) {
            System.out.println("Hello");
        }
        System.out.println("Hi");
    }
}

chevron_right


option
a)Hello
Hello
…..
…..
…..
b) Error
c)Hello
Hello
Hello
Hello
Hello

d)Hello
Hello
Hello
Hello
Hello
Hi

Output: b

Explanation : In Java, if we declare any variable as final then that variable value is fixed and can not be changed at run time. In final variable, values are assigned at compile time means in this program, compiler see both a and b are final variable and final variable value never change and condition is always true. “Hi” statement is unreachable so compiler give the error Unreachable statement “Hi”.

This article is contributed by Shivakant Jayswal. 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 :

Be the First to upvote.


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