aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q from Marcus Mock test 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q from Marcus Mock test 2" Watch "Q from Marcus Mock test 2" New topic
Author

Q from Marcus Mock test 2

saikrishna
Greenhorn

Joined: Jul 16, 2002
Posts: 2
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

Joined: Apr 10, 2003
Posts: 62
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


SCJP 1.4 (86%)<br />SCMAD Beta (77%)<br />SCEA (part I:89%,partII:93%)<br />MCAD (cleared 70-315 & 70-320 [Score 980])
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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!


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
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 ]

SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q from Marcus Mock test 2
 
Similar Threads
Mock Exam Question
Packages
Reg. Packages
Marcus Green - Exam 2 - Question 9
Package visibility