Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.... :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote