In: Computer Science
What is displayed to the screen when the following code executes? Counter c1 = new Counter(); Counter c2 = new Counter(); Counter c3 = new Counter(); Counter[] A = {c1, c2}; JAVA A[0].clickButton(); Counter[] B = new Counter[2]; for(int i = 0; i < A.length; i++) B[i] = A[i]; B[0] = c3; System.out.println("A[0] is " + A[0].getCount()); System.out.println("B[0] is " + B[0].getCount());
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
It depends on the code for the count class.
If it is as below.
package staticclasses;
class Counter{
private int count;
Counter(){
count=0;
}
public void clickButton() {
count++;
}
public int getCount() {
return count;
}
}
public class Main {
public static void main(String[] args) {
Counter c1 = new
Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
Counter[] A = {c1, c2};
A[0].clickButton();
Counter[] B = new Counter[2];
for(int i = 0; i < A.length;
i++)
B[i] =
A[i];
B[0] = c3;
System.out.println("A[0] is " +
A[0].getCount());
System.out.println("B[0] is " +
B[0].getCount());
}
}
output will be