Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help??

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am a newbie in java. I am preparing for SCJP2 right now.
this is a mock question during my study.
I did not understand what is wrong with the code. please throw some light in.
The following code defines a simple applet:
import java.applet.Applet;
import java.awt.*;
public class Sample extends Applet {
private String text = "Hello World";
public void init() {
add(new Label(text));
}
public Sample (String string) {
text = string;
}
}
It is accessed form the following HTML page:
< html>
< title>Sample Applet< /title>
< body>
< applet code="Sample.class" width=200 height=200>< /applet>
< /body>
< /html>
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 answer given is B. I need a clear explanation, plz.
[Spaces added inside HTML tags to make them visible to browsers. - Jim]
[This message has been edited by Jim Yingst (edited July 06, 2000).]
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Applets should not have constructors and all initialization activity for them should be in the <CODE>init()</CODE> method.
In this example, the JDK can't instantiate the class Sample. Take it out as below and the applet works as expected.
Finally, good advice if you're serious in passing the SCPJ2 Certification: Get a Java compiler and practice, practice, practice
This code works:
<PRE>import java.applet.Applet;
import java.awt.*;
public class Sample extends Applet {
private String text = "Hello World";
public void init() {
add(new Label(text));
}
/*
public Sample (String string) {
text = string;
}
*/
}</PRE>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic