What is the output of the following program: public class Confuz { public static
ID: 3860403 • Letter: W
Question
What is the output of the following program:
public class Confuz
{
public static void main(String[] args)
{
int y = 4;
foo (y);
System.out.println (y);
}
public static void foo (int n)
{
n += 4;
}
}
Explanation / Answer
Output of this program will be 4
Reason:
when you run the program, it will pass value of y to function foo(y) then inside foo() value of n will add with 4
but foo() not returning any value so it wont effect to main function then after foo() main function will print the value of y which is 4
BUT if you want to print added value then you can return value from foo() and assign that value to y :
public static int foo(int n)
{
n+=4;
return n;
}
or you can simply do this:
void foo (int n)
{
n += 4;
System.out.println (n);
}
I hope this helps you.... :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.