250+ TOP MCQs on Applets Fundamentals and Answers

Java MCQs on Applets fundamentals 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()

Answer: b
Clarification: 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()

Answer: c
Clarification: 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()

Answer: b
Clarification: 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

Answer: b
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.

5. Which of these operators can be used to get run time information about an object?
a) getInfo
b) Info
c) instanceof
d) getinfoof

Answer: c
Clarification: None.

6. What is the Message is displayed in the applet made by the following Java program?

  1.     import java.awt.*;
  2.     import java.applet.*;
  3.     public class myapplet extends Applet
  4.     {
  5.         public void paint(Graphics g)
  6.         {
  7.             g.drawString("A Simple Applet", 20, 20);    
  8.         }
  9.     }

a) A Simple Applet
b) A Simple Applet 20 20
c) Compilation Error
d) Runtime Error

Answer: a
Clarification: 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?

  1.     import java.awt.*;
  2.     import java.applet.*;
  3.     public class myapplet extends Applet
  4.     {
  5.         public void paint(Graphics g)
  6.         {
  7.             g.drawString("A Simple Applet", 20, 20);    
  8.         }
  9.     }

a) 20
b) 50
c) 100
d) System dependent

Answer: a
Clarification: 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?

  1.     import java.awt.*;
  2.     import java.applet.*;
  3.     public class myapplet extends Applet
  4.     {
  5.         Graphic g;
  6.         g.drawString("A Simple Applet", 20, 20);    
  7.     }

a) 20
b) Default value
c) Compilation Error
d) Runtime Error

Answer: c
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.

9. What will be the output of the following Java program?

  1.     import java.io.*;
  2.     class Chararrayinput
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6. 	    String obj  = "abcdefgh";
  7.             int length = obj.length();
  8.             char c[] = new char[length];
  9.             obj.getChars(0, length, c, 0);
  10.             CharArrayReader input1 = new CharArrayReader(c);
  11.             CharArrayReader input2 = new CharArrayReader(c, 1, 4);
  12.             int i;
  13.             int j;
  14.             try
  15.             {
  16. 		while((i = input1.read()) == (j = input2.read()))
  17.                 {
  18.                     System.out.print((char)i);
  19.                 }
  20.        	    } 
  21.             catch (IOException e)
  22.             {
  23.                 e.printStackTrace();
  24. 	    }
  25. 	}
  26.     }

a) abc
b) abcd
c) abcde
d) none of the mentioned

Answer: d
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

Leave a Reply

Your email address will not be published. Required fields are marked *