• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Q from Marcus Mock test 2

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic