Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marcus Exam 1

 
cowboy55
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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{
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);
}
}
 
Ken Lai
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
antraarora
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile time error because a class can have ATMOST one top level public class.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think cowboy55 didn't mention about both the classes are in the same file, may be they are in two different files like Base.java & pri.java
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.
Thanks
VR.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic