Question 1
Back to Exercise
What is the output of the following Java program?
class Counter {
int value;
void inc() { value++; }
}
public class Main {
public static void main(String[] args) {
Counter a = new Counter();
Counter b = new Counter();
a.inc(); a.inc(); b.inc();
System.out.println(a.value + " " + b.value);
}
}
- A 2 1 Correct
- B 3 3
- C 1 1
- D 2 2
Correct Answer: Option A
Each object has its own value field.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!