aspose file tools*
The moose likes Mock Exam Errata and the fly likes Markus, exam 1, Q43 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Reply locked New topic
Author

Markus, exam 1, Q43

Jeff L.
Greenhorn

Joined: Sep 02, 2003
Posts: 8
What will be the result when you try to compile and run the following code?
private class Base{
Base(){
int i = 100;
System.out.println(i);
}
}
public class Pri extends Base{
static int i = 200;
public static void main(String argv[]){
Pri p = new Pri();
System.out.println(i);
}
}
1) Error at compile time
2) 200
3) 100 followed by 200
4) 100
---------------------->
The answer listed is 1), because the code doesn't compile, presumably assuming that the java filename is Base.java, but why assume that? Why couldn't the file be named Pri.java? I compiled a Java class with the public class listed second just this afternoon and it worked fine.
Jeff L.
Greenhorn

Joined: Sep 02, 2003
Posts: 8
Oops! I should have said I tried earlier to compile where the public class was listed after another package scope class. Apparantly, package classes and private classes are treated differently. Private classes cause an error "modifyier private not allowed here", while package classes compile just fine. IMO, this aspect of Java is silly, but the question was correct about there being a compiler error. My bad.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Markus, exam 1, Q43