asked 195k views
0 votes
What is the output of the following program:

public class testmeth
{
static int i = 1;
public static void main(String args[])
{
System.out.println(i+" , ");
m(i);
System.out.println(i);
}
public void m(int i)
{
i += 2;
}
}
(a) 1 , 3
(b) 3 , 1
(c) 1 , 1
(d) 1 , 0
(e) none of the above.

1 Answer

7 votes

Final answer:

The output of the given program is 1 , 1. The method m() does not modify the value of the variable i, resulting in the original value of 1 being printed.

Step-by-step explanation:

The output of the given program is 1 , 1.

The reason for this is that the method m() is not actually modifying the value of the variable i. Inside the method, a new local variable i is created and assigned the value of the parameter i, which is 1. Then, this local variable is incremented by 2, resulting in a value of 3. However, this does not affect the original value of the static variable i, which remains 1. Therefore, when the value of i is printed after calling the method, it is still 1.

answered
User Ader
by
8.7k points

Related questions