Question 19
Back to Exercise
What is the output of the following Java program?
public class Main {
static int total = 100;
static void add(int total) {
total += 5; // the parameter
Main.total += total; // the static field
}
public static void main(String[] args) {
add(10);
int total = 1;
System.out.println(total + " " + Main.total);
}
}
- A 1 115 Correct
- B 1 110
- C 115 115
- D 1 100
Correct Answer: Option A
Inside add, total is the parameter (10 → 15) and Main.total is the field (100 + 15). In main the local total hides the 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!