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
Question
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
Solution
The correct answer is 1 5.
Here's the step by step explanation:
- Two objects of class 'access', obj1 and obj2 are created.
- For obj1, x is set to 0 and y is also set to 0. Then the method cal is called with parameters 1 and 2. This adds 1 to x and 2 to y. So, obj1.x becomes 1 and y becomes 2.
- For obj2, x is set to 0. Then the method cal is called with parameters 2 and 3. This adds 2 to x and 3 to y. So, obj2.x becomes 2. But y becomes 5 because y is a static variable, which means it is common to all objects of the class. The previous value of y (which was 2) is added with 3, making it 5.
- Finally, obj1.x and obj2.y are printed, which are 1 and 5 respectively.
Similar Questions
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
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
e 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)); } }
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
Select the correct answerPredict the output of the following program.? class CT{ int i = 1; int j = 2; CT func(CT obj) { CT obj3 = new CT(); obj3 = obj; obj3.i = obj.i++ + ++obj.j; obj.j = obj.j; return obj3; } public static void main(String[] args) { CT obj1 = new CT(); CT obj2 = obj1.func(obj1); System.out.println("obj1.i = " + obj1.i + " obj1.j = " + obj1.j); System.out.println("obj2.i = " + obj2.i + " obj1.j = " + obj2.j); }}OptionsCompilation errorobj1.i = 4 obj1.j = 3obj2.i = 4 obj2.j = 3obj1.i = 1 obj1.j = 2obj2.i = 4 obj2.j = 3non of these
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.