This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
What will happen when you attempt to compile and run the following code?
class Base{ public int iAcres=3; } public class Mfields extends Base{ private int iAcres=3.5; public static void main(String args[]){ Base mf = new Mfields(); System.out.println(mf.iAcres); } }
the code gives error because of assigning double value to integer.
the code will work if
class Base{ public int iAcres=3; } public class Mfields extends Base{ private int iAcres=(int)3.5; public static void main(String args[]){ Base mf = new Mfields(); System.out.println(mf.iAcres); } }
When faced with a question like this I recommend that you attempt to compile the code to see what happens. A key way to understand the Java language is to write many many small programs. Also, when selecting mock exams you should expect to not only be given which options are correct but some explanation of why that option is correct. So for this question in my JDK 1.4 database I have included the following explanation (which is visible to anyone who can see the question)...
"An int cannot have a fractional component, under JDK1.4 this produced the error message
possible loss of precision found : double required: int private int iAcres=3.5; "
My JDK1.5/tiger/Java 5 questions also include explanations wherever possible and all code examples have been compiled under JDK1.5. This is important as some questions that are still relevent from the JDK 1.4 objectives now result in different answers, particularly where Autoboxing is a factor.