• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Applet

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct answer to this Q is B. Is it because text is declared as private? I don't understand.
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.

 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Parimala,
The run time error is caused by the constructor that appears in the Applet. An Applet can not have a constructor contained inside it because it can't be directly instantiated.
Regards,
Manfred.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manfred, but when I try below program , it runs fine.
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(){} // insert this line.
public Sample (String string) {
text = string;
}
}
Please explain why?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Parimala
Did U worked with is code.
I am geting compile time error.so the ans is d.I am using jdk1.3
as invalid method declaration; return type required
public Sample (String string) {
^
1 error
regards
vkswami
reply
    Bookmark Topic Watch Topic
  • New Topic