Output of Java program | Set 28
Question 1. What is the output of the following question?
A) t1.a=100 t1.b=200
B) t1.a=10 t1.b=200
C) t1.a=100 t1.b=200
D) t1.a=100 t1.b=200
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?
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?
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?
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?
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 email@example.com. 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.