Discussion Forum : Introduction To Methods And Streams
Question -


What is the output of this 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);
}
}
Options:
A .  10 20
B .  20 10
C .  20 40
D .  40 20
Answer: Option B

class objects are always passed by reference, therefore changes done are reflected back on 

original arguments. obj.meth(obj) sends object obj as parameter whose variables a & b are 

multiplied and divided by 2 respectively by meth() function of class test. a & b becomes 20 

& 10 respectively.
output:
$ javac Output.java
$ java Output
20 10



Was this answer helpful ?
Next Question
Submit Your Solution hear:

Your email address will not be published. Required fields are marked *