Java MCQs on Applets fundamentals in Java Programming Language.
1. Which of these functions is called to display the output of an applet? Answer: b 2. Which of these methods can be used to output a string in an applet? Answer: c 3. Which of these methods is a part of Abstract Window Toolkit (AWT) ? Answer: b 4. Which of these modifiers can be used for a variable so that it can be accessed from any thread or parts of a program? Answer: b 5. Which of these operators can be used to get run time information about an object? Answer: c 6. What is the Message is displayed in the applet made by the following Java program? a) A Simple Applet Answer: a 7. What is the length of the application box made by the following Java program? a) 20 Answer: a 8. What is the length of the application box made the following Java program? a) 20 Answer: c 9. What will be the output of the following Java program? a) abc
a) display()
b) paint()
c) displayApplet()
d) PrintApplet()
Clarification: Whenever the applet requires to redraw its output, it is done by using method paint().
a) display()
b) print()
c) drawString()
d) transient()
Clarification: drawString() method is defined in Graphics class, it is used to output a string in an applet.
a) display()
b) paint()
c) drawString()
d) transient()
Clarification: paint() is an abstract method defined in AWT.
a) transient
b) volatile
c) global
d) No modifier is needed
Clarification: The volatile modifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other part of the program. Specially used in situations involving multithreading.
a) getInfo
b) Info
c) instanceof
d) getinfoof
Clarification: None.
import java.awt.*;
import java.applet.*;
public class myapplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A Simple Applet", 20, 20);
}
}
b) A Simple Applet 20 20
c) Compilation Error
d) Runtime Error
Clarification: None.
Output:
A Simple Applet
(Output comes in a new java application)
import java.awt.*;
import java.applet.*;
public class myapplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A Simple Applet", 20, 20);
}
}
b) 50
c) 100
d) System dependent
Clarification: the code in pain() method – g.drawString(“A Simple Applet”,20,20); draws a applet box of length 20 and width 20.
import java.awt.*;
import java.applet.*;
public class myapplet extends Applet
{
Graphic g;
g.drawString("A Simple Applet", 20, 20);
}
b) Default value
c) Compilation Error
d) Runtime Error
Clarification: To implement the method drawString we need first need to define abstract method of AWT that is paint() method. Without paint() method we can not define and use drawString or any Graphic class methods.
import java.io.*;
class Chararrayinput
{
public static void main(String[] args)
{
String obj = "abcdefgh";
int length = obj.length();
char c[] = new char[length];
obj.getChars(0, length, c, 0);
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 1, 4);
int i;
int j;
try
{
while((i = input1.read()) == (j = input2.read()))
{
System.out.print((char)i);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
b) abcd
c) abcde
d) none of the mentioned
Clarification: No output is printed. CharArrayReader object input1 contains string “abcdefgh” whereas object input2 contains string “bcde”, when while((i=input1.read())==(j=input2.read())) is executed the starting character of each object is compared since they are unequal control comes out of loop and nothing is printed on the screen.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput