*Based on answer choices A-E, what is the code's output?
6. Given the following code, the output is __.
class Apple { static int i; }
class Fruits
{
public static void main(String args[])
{
Apple.i = 2;
Apple a1 = new Apple();
a1.i++;
System.out.println(a1.i);
}
}
A. 5
B. 4
C. 2
D. 3
E. 0
This code will output 3, so right answer is D.
This will happened because `i` is static variable, so its value is common among all instances of the class `Apple`.
Comments
Leave a comment