Discussion Forum : Introduction To Methods And Streams
Question -


What is the output of this program?


class test {
int a;
int b;
void meth(int i , int j) {
i *= 2;
j /= 2;
}
}
class Output {
public static void main(String args[])
{
test obj = new test();
int a = 10;
int b = 20;
obj.meth(a , b);
System.out.println(a + " " + b);
}
}
Options:
A .  10 20
B .  20 10
C .  20 40
D .  40 20
Answer: Option A

Variables a & b are passed by value, copy of their values are made on formal parameters of 

function meth() that is i & j. Therefore changes done on i & j are not reflected back on original 

arguments. a & b remain 10 & 20 respectively.
output:
$ javac Output.java
$ java Output
10 20



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

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