asked 110k views
5 votes
Consider the two methods (within the same class)

public static int foo(int a, String s)
{
s = "Yellow";
a=a+2;
return a;
}
public static void bar()
{
int a=3;
String s = "Blue";
a = foo(a,s);
System.out.println("a="+a+" s="+s);
}
public static void main(String args[])
{
bar();
}
What is printed on execution of these methods?
(a) a = 3 s = Blue
(b) a = 5 s = Yellow
(c) a = 3 s = Yellow
(d) a = 5 s = Blue
(e) none of the above.

asked
User Yly
by
8.3k points

1 Answer

6 votes

Final answer:

The value of a is 5 and the value of s is Blue.

Step-by-step explanation:

The methods defined are:

  • public static int foo(int a, String s) {
    s = "Yellow";
    a=a+2;
    return a;
    }
  • public static void bar() {
    int a=3;
    String s = "Blue";
    a = foo(a,s);
    System.out.println("a="+a+" s="+s);
    }
  • public static void main(String args[]) {
    bar();
    }

In the foo method, the value of the string parameter s is changed to "Yellow". The value of the integer parameter a is incremented by 2 and returned. In the bar method, the integer variable a is assigned the value 3, and the string variable s is assigned the value "Blue". The foo method is then called with the arguments a and s. The returned value of the foo method is assigned to a. The System.out.println statement prints the values of a and s which are 5 and "Blue" respectively.

answered
User Jaydeep
by
8.5k points