Given1. public class Program{2. static int x = 20;3. public static void main(String args[]){4. Program pr = new Program();5. pr.x = 5;6. int y = x/pr.x;7. System.out.print("y = ");8. System.out.print(y);9. }10. }What will be the result?*y = 2Compilation fails due to an error on line 8.y = 1y = 4Compilation fails due to an error on line 6.
Question
Given1. public class Program{2. static int x = 20;3. public static void main(String args[]){4. Program pr = new Program();5. pr.x = 5;6. int y = x/pr.x;7. System.out.print("y = ");8. System.out.print(y);9. }10. }What will be the result?*y = 2Compilation fails due to an error on line 8.y = 1y = 4Compilation fails due to an error on line 6.
Solution
The result will be "Compilation fails due to an error on line 6."
Here's why:
In Java, static variables belong to the class, not the object. When you create an object of the class and try to change the value of the static variable, it won't affect the original value of the static variable in the class.
In this case, x is a static variable. When you create an object pr and try to change x to 5 (pr.x = 5;), it won't change the original value of x in the class, which is 20.
Then, when you try to calculate y (int y = x/pr.x;), you're actually doing 20/20, not 20/5.
However, the problem is that you're trying to print y outside of the scope where y is defined. y is defined inside the main method, so it can't be accessed outside of this method. This will cause a compilation error on line 8, where you're trying to print y.
Similar Questions
Given1. public class Program{2. public static void main(String[] args) {3. 4. for(int j = 0,k=5; j < k; k--) ;5. for(int j = 0; j++ < 3;) ;6. for(int i = 0; i < 5; i++, System.out.print(i + ".go ")) ;7. 8. }9. }What will be the result?*Compilation fails due to multiple errors.Compilation fails due to an error on line 6.0.go 1.go 2.go 3.go 4.go 5.goAn exception is thrown at runtime.1.go 2.go 3.go 4.go 5.go
Given1. class Program{2. public static void main(String args[]){3. int x = (int)args[0];4. System.out.print(x);5. }6. }What is true?*Compilation fails due to multiple errorsIf we use command line invocation, java Program 10, the output will be 10.If we use command line invocation, java Program abc, An ClassCastException will be thrown.If we use command line invocation, java Ex, a ArrayIndexOutOfBoundsException will be thrown.Compilation fails due to error on line 3.
Select the correct answerWhat will be the output of the following Java code? class Codetantra { final public int output(int x, int y) { return 1; } } class Program extends Codetantra { public int output(int x, int y) { return 2; } } public class result { public static void main(String args[]) { Program object = new Program(); System.out.print("y is " + y.output(0, 1)); } }Optionsy is : 0y is : 1Compilation ErrorAn exception is thrown at runtime
What is the output of the following code?public class Solution { static int x = 10; public static void main(String args[]) { System.out.println(x); } }Options: Pick one correct answer from below100Compilation errorRuntime error
Given1. class Program{2. 3. public static void main(String[] args){4. 5. int val1 = 1;6. int val2 = 2;7. 8. if(val1 == val2)9. System.out.print("1");10. if(val1!= val2)11. System.out.print("2");12. if(val1 > val2)13. System.out.print("3");14. if(val1 < val2)15. System.out.print(4);16. if(val1 => val2)17. System.out.print("5");18. 19. }20. } Which is the output?*24524Compilation fails due to an error on line 16Compilation fails due to multiple errorsNone of above.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.