Java Questions & Answers – Applets

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

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

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

Answer: b
Explanation: paint() is an abstract method defined in AWT.
advertisement
advertisement

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

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

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: None.
Output:
A Simple Applet
(Output comes in a new java application)
advertisement

7. What is the length of the application box made by the following Java program?

advertisement
  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
View Answer

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

  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
View Answer

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

  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
View Answer

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.