Object Reference Assignment and Method Calls
Consider the following IntObject and IntObjectTest classes.
public class IntObject
{
private int num;
public IntObject() // default constructor
{ num = 0; }
public IntObject(int n) // constructor
{ num = n; }
public void increment() // increment by 1
{ num++; }
}
public class IntObjectTest
{
public static IntObject someMethod(IntObject obj)
{
IntObject ans = obj;
ans.increment();
return ans;
}
public static void main(String[] args)
{
IntObject x = new IntObject(2);
IntObject y = new IntObject(7);
IntObject a = y;
x = someMethod(y);
a = someMethod(x);
}
}
After the main method in the IntObjectTest class finishes execution, what are the values of the num field for the objects referenced by x, y, and a, respectively?
A
2, 9, 9
B
3, 8, 9
C
2, 8, 9
D
9, 9, 9
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | chunxiangxu.cxu | 5 | 6 | 1m 07s | 423 |
| #2 | sailajavadlamani33 | 0 | 1 | 0m 00s | -10 |
| #3 | y.seong2027 | 1 | 2 | 7m 51s | -381 |
| #4 | suhanakochhar006 | 1 | 6 | 13m 42s | -772 |
Items per page:
10
1 – 4 of 4
APFIVE