This section of our 1000+ Java MCQs focuses on Applets in Java Programming Language.
1. Which of these functions is called to display the output of an applet?
a) display()
b) paint()
c) displayApplet()
d) PrintApplet()
View Answer
Explanation: Whenever the applet requires to redraw its output, it is done by using method paint().
2. Which of these methods can be used to output a string in an applet?
a) display()
b) print()
c) drawString()
d) transient()
View Answer
Explanation: drawString() method is defined in Graphics class, it is used to output a string in an applet.
3. Which of these methods is a part of Abstract Window Toolkit (AWT) ?
a) display()
b) paint()
c) drawString()
d) transient()
View Answer
Explanation: paint() is an abstract method defined in AWT.
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?
a) transient
b) volatile
c) global
d) No modifier is needed
View Answer
Explanation: 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.
5. Which of these operators can be used to get run time information about an object?
a) getInfo
b) Info
c) instanceof
d) getinfoof
View Answer
Explanation: None.
6. What is the Message is displayed in the applet made by the following Java program?
import java.awt.*;
import java.applet.*;
public class myapplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A Simple Applet", 20, 20);
}
}
a) A Simple Applet
b) A Simple Applet 20 20
c) Compilation Error
d) Runtime Error
View Answer
Explanation: None.
Output:
A Simple Applet
(Output comes in a new java application)
7. What is the length of the application box made by the following Java program?
import java.awt.*;
import java.applet.*;
public class myapplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A Simple Applet", 20, 20);
}
}
a) 20
b) 50
c) 100
d) System dependent
View Answer
Explanation: the code in pain() method – g.drawString(“A Simple Applet”,20,20); draws a applet box of length 20 and width 20.
8. What is the length of the application box made the following Java program?
import java.awt.*;
import java.applet.*;
public class myapplet extends Applet
{
Graphic g;
g.drawString("A Simple Applet", 20, 20);
}
a) 20
b) Default value
c) Compilation Error
d) Runtime Error
View Answer
Explanation: 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.
9. What will be the output of the following Java program?
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();
}
}
}
a) abc
b) abcd
c) abcde
d) none of the mentioned
View Answer
Explanation: 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
Sanfoundry Global Education & Learning Series Java Programming Language.
- Practice BCA MCQs
- Practice Information Technology MCQs
- Apply for Java Internship
- Check Programming Books
- Apply for Computer Science Internship