• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q.64 from John Hunt

 
Punitha krishna
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the init() method which gets executed whenever the applet is loaded.
 
Punitha krishna
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandru,
Thanx for replying. I still don't understand what caused the run time error.
 
manav kher
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic