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
Question
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
Solution
The output of the program will be "20 10".
Here's the step by step explanation:
- An object of class
testis created withaas 10 andbas 20. - The
methmethod is called with the object itself as the argument. - Inside the
methmethod,ais multiplied by 2 andbis divided by 2. So,abecomes 20 andbbecomes 10. - Finally, the values of
aandbare printed which are 20 and 10 respectively.
Similar Questions
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); }}
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); }}
What 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); } }Options3421
What is the output of this program? class Output { public static void main(String args[]) { int a1[] = new int[10]; int a2[] = {1, 2, 3, 4, 5}; System.out.println(a1.length + " " + a2.length); } }Select one:a.0 5b.10 5c.5 10d.0 10
What is the Output of following Java Program?abstract class Demo{ public int a; Demo() { a = 10; } abstract public void set(); }class Test extends Demo{ final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.get(); }}Select one:a.Compile Time Errorb.a=10c.Runtime Exception
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.