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
Question
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
Solution
The correct answer is 7 7.
Here's the step by step explanation:
- In the main method, two objects of the class static_out are created, obj1 and obj2.
- The variable 'a' is initialized with the value 2.
- The add method is called on obj1 with parameters a and a+1 (which is 2 and 3). This sets x to 5 (2+3) and y to 8 (5+3).
- The add method is then called on obj2 with parameters 5 and a (which is 5 and 2). This sets x to 7 (5+2) and y to 7 (7+2-2, because x is now 7).
- The println statement prints the values of x and y. Since x and y are static variables, they are shared among all objects of the class. So, the values that are printed are the ones that were last set, which are 7 (from obj2.add) and 7 (from obj2.add).
- Therefore, the output of the program is "7 7".
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 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
ect 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); } }
What will be the output of the following program?class test {int a;int b;test(int i, int j) {a = i;b = j;}void meth(test o) {o.a *= 2;o.b /= 2;} } class Output{public static void main(String args[]){test obj = new test(10 , 20);obj.meth(obj);System.out.println(obj.a + " " + obj.b); }}Select one:40 2010 2020 1020 40
What will be the output of the following program?class string_class {public static void main(String args[]){String obj = "hello";String obj1 = "world"; String obj2 = obj;obj2 = " world";System.out.println(obj + " " + obj2); }}
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.