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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q.64 from John Hunt" Watch "Q.64 from John Hunt" New topic
Author

Q.64 from John Hunt

Punitha krishna
Greenhorn

Joined: Nov 22, 2000
Posts: 16
The following code defines a simple applet:



It is accessed form the following HTML page:



What is the result of compiling and running this applet:
A. Prints "Hello World".
B. Generates a runtime error.
C. Does nothing.
D. Generates a compile time error.
Select the most appropriate answer.
The correct answer is B
I have couple of questions regarding this question.
1. Can we have a constructor in a applet.
2. I don't understand what made the run time error to be generated.
Can anyone please help me with this.
Thx.
I added UBB Code to make this more readable.
Bill
[This message has been edited by bill bozeman (edited November 22, 2000).]
Chandru kotai
Greenhorn

Joined: Nov 17, 2000
Posts: 7
It is the init() method which gets executed whenever the applet is loaded.
Punitha krishna
Greenhorn

Joined: Nov 22, 2000
Posts: 16
Chandru,
Thanx for replying. I still don't understand what caused the run time error.
manav kher
Ranch Hand

Joined: Nov 21, 2000
Posts: 98
yeah i think the answer is that it will do nothing its because the it is never made visible ..i don't see where a error could be generated
manav
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi ...
It causes a Runtime Error 'Class cannot be instantiated' because the JVM is looking for the default no-arg constructor. If you add this in and recompile it works fine.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

Jane your so smart ;^)
anytime you declare a constructor the JVM cannot automatically create a default one the way it does when you dont declare any. I dont see how you can actually use a constructor in an applet as there is no way i know of to pass the arguments to the constructor.


SCJP
Visit my download page
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Randall,
I think you're right ... maybe someone out there knows how but I've been unable to figure it out.
Think a ctor in an applet is redundant since, if you've written an applet, it will be loaded in an HTML page and you can use PARAM to pass any initializing variables.
One thing I did discover in my attempts at using a ctor ... I tried to create a new instance of the class and then assign it to this ... got a 'cannot assign a value to final variable this' error!
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q.64 from John Hunt