Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q from Marcus Mock test 2

 
saikrishna
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you attempt to compile and run these two files in the same directory?
//File P1.java
package MyPackage;
class P1{void afancymethod(){ System.out.println("What a fancy method");
}
}
//File P2.java
public class P2 extends P1{
public static void main(String argv[]){P2 p2 = new P2();
p2.afancymethod();
}}
1) Both compile and P2 outputs "What a fancy
method" when run
2) Neither will compile
3) Both compile but P2 has an error at run time
4) P1 compiles cleanly but P2 has an error at
compile time
Answer given as 4.
Please let me know in detail why the remaining options are incorrect.
Thank you in advance.
:roll:
 
Lawrence Chettiar
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well
Lets see each of them
1)P2 will not compile for 2 reasons first it does not import package MyPackage,secondly main tries to execute a method afancymethod which has default scope means it is not accessible to class outside this package MyPackage.
2) Nope there is nothing wrong with P1 why will it not compile, it will compile properly.
3) see point 1
Hope this helps
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saikrishna,
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by saikrishna:
What happens when you attempt to compile and run these two files in the same directory?
//File P1.java
package MyPackage;
class P1{void afancymethod(){ System.out.println("What a fancy method");
}
}
//File P2.java
public class P2 extends P1{
public static void main(String argv[]){P2 p2 = new P2();
p2.afancymethod();
}}
1) Both compile and P2 outputs "What a fancy
method" when run
2) Neither will compile
3) Both compile but P2 has an error at run time
4) P1 compiles cleanly but P2 has an error at
compile time
Answer given as 4.
Please let me know in detail why the remaining options are incorrect.
Thank you in advance.
:roll:

For class P2 to inherit P1 which is in package Mypackage,u have to either write import statement import MyPackage.P1 before class definition of P2 or say class P2 extends Mypackage.P1.You should specify package name to which P1 belongs .So answer is 4.
Hope that helps.
Veena
[ June 18, 2003: Message edited by: Veena Point ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic