Consider the following class definition:
public class Counter
{
private int myN;
public Counter( int n )
{
myN = n;
}
public Counter nextCounter()
{
return new Counter( myN + 1 );
}
public int getN()
{
return myN;
}
}
What is the value of t after the following code is executed?
int t = 0;
int i;
Counter c = new Counter( 10 );
for ( i = 0 ; i < 5 ; i++ )
{
c = c.nextCounter();
t = c.getN();
}
A. 15
B. 14
C. 10
D. 5
E. 4
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
The for-loop iterates 5 times, and c.myN is incremented by one in each loop. So the answer is A.