Select the correct answerWhat will be the output of the following Java program? class access { static int x; void increment() { x++; } } class static_use { public static void main(String args[]) { access obj1 = new access(); access obj2 = new access(); obj1.x = 0; obj1.increment(); obj2.increment(); System.out.println(obj1.x + " " + obj2.x); } }Options 2 21 11 2Compilation Error
Question
Select the correct answerWhat will be the output of the following Java program? class access { static int x; void increment() { x++; } } class static_use { public static void main(String args[]) { access obj1 = new access(); access obj2 = new access(); obj1.x = 0; obj1.increment(); obj2.increment(); System.out.println(obj1.x + " " + obj2.x); } }Options 2 21 11 2Compilation Error
Solution
The correct answer is 2 2.
Here's the step by step explanation:
- Two objects of the class 'access', obj1 and obj2, are created.
- The variable 'x' is static, which means it is shared among all objects of the class.
- obj1.x is set to 0.
- The increment() method is called on obj1, which increases 'x' by 1. So, 'x' becomes 1.
- The increment() method is called on obj2, which again increases 'x' by 1. So, 'x' becomes 2.
- When we print obj1.x and obj2.x, we get 2 2 because 'x' is shared among all objects of the class 'access'.
Similar Questions
Select the correct answerWhat will be the output of the following Java program? class access { public int x; static int y; void cal(int a, int b) { x += a ; y += b; } } class static_specifier { public static void main(String args[]) { access obj1 = new access(); access obj2 = new access(); obj1.x = 0; obj1.y = 0; obj1.cal(1, 2); obj2.x = 0; obj2.cal(2, 3); System.out.println(obj1.x + " " + obj2.y); } }Options3 22 31 21 5
Select the correct answerWhat will be the output of the following Java program? class static_out { static int x; static int y; void add(int a , int b) { x = a + b; y = x + b; } } class static_use { public static void main(String args[]) { static_out obj1 = new static_out(); static_out obj2 = new static_out(); int a = 2; obj1.add(a, a + 1); obj2.add(5, a); System.out.println(obj1.x + " " + obj2.y); } }Options7 9 6 67 79 7
Select the correct answerWhat will be the output of the following Java program?class java { public static void main(String args[]) { int var1 = 3; int var2 = 8; if ((var2 = 2) == var1) System.out.print(var2); else System.out.print(++var2); } }Options4213
e correct answerWhat is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}
Select the correct answerWhat will be the output of the following Java code? class main { public int x; protected int y; } class derived extends main { int y; void display() { super.y = 5; System.out.println(x + " " + y); } } class Output { public static void main(String args[]) { derived obj = new derived(); obj.x=2; obj.y=3; obj.display(); } }Options3 22 32 55 2
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.