aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question from marcus green Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question from marcus green" Watch "question from marcus green" New topic
Author

question from marcus green

samdeep aarzoo
Ranch Hand

Joined: Jun 09, 2005
Posts: 160
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);
}
}



answer is given
1) Compile time error

i think it must be 3
can anybody explain
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
One reason for a compile-time error is this line:

private int iAcres=3.5;

3.5 is a double value, which cannot be assigned to an int variable without a cast. You'll get a "possible loss of precision" error.


SCJA 1.0 (98%), SCJP 1.4 (98%)
Ganesh Hariharan
Greenhorn

Joined: Jul 08, 2005
Posts: 4
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);
}
}

double value is typecasted.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
I did the same mistake. These questions tests your power of concentration.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
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.

Marcus


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question from marcus green