What is the result of compiling and executing the following class?
package sports; //from w ww. j a v a 2 s.co m public class MyClass { String color = "red"; private void printColor(String color) { color = "purple"; System.out.print(color); } public static void main(String[] rider) { new MyClass().printColor("blue"); } }
B.
First off, the color variable defined in the instance and set to red is ignored in the method printColor()
as local scope overrides instance scope, so Option A is incorrect.
The value of color passed to the printColor()
method is blue, but that is lost by the assignment to purple, making Option B the correct answer and Option C incorrect.
Option D is incorrect as the code compiles and runs without issue.