File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Exam 1 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 "Marcus Exam 1 " Watch "Marcus Exam 1 " New topic

Marcus Exam 1


Joined: Jun 18, 2000
Posts: 14
This is based on question from marcus exam but I changed the question a little just to see what would be the output. please read and explain why your output would be true. Thanks =)
-What will be the result when you try to compile and run the following code?
public class Base{
int i = 100;
public class Pri extends Base{
static int i = 200;
public static void main(String argv[]){
Pri p = new Pri();
Ken Lai
Ranch Hand

Joined: Aug 06, 2000
Posts: 30
Compile time error: 2 public classes in the same file.
I didn't test it out though so feel free to correct me if I'm wrong.
Ranch Hand

Joined: Jul 27, 2000
Posts: 45
Compile time error because a class can have ATMOST one top level public class.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I think cowboy55 didn't mention about both the classes are in the same file, may be they are in two different files like &
If they both are in two different files, they both will compile and run and gives the output 100, 200. because the Pri class implictly calls the Base class no-arg constructor and prints that value then it prints Pri class's i value.
Correct me, if I am wrong.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thank you all and VR for ur correction.
Yeah, i wanted to know if it would print out 100 , 200.
now i know it does if put on dirrent files.
I agree. Here's the link:
subject: Marcus Exam 1
It's not a secret anymore!