Java J2EE interview questions and updates

What you do to learn the basics of Java??

Java is one of the pure object oriented programming language where all concepts are from C++ and syntax is from C.It is easy for one to learn java and get into IT porvided you have good interest for programming.But of-course Java is an ocean and unlike .Net, Java has its own advantages.


Applet programming with java-Interview preparation

          Applets are  small applications that are accessed on an internet server, transported over the Internet, automatically installed, and run as a part of a web document.
          Applets must be run under an appletviewer or a java compatible browser.

Life Cycle of an Applet
Ø  init()
Ø  start()
Ø  paint()
Ø  stop()
Ø  destroy()
Ø         The init() method is the first method to be called and where variables are initialised.
Ø          This method is called only once during the run time of the applet.

Ø    The start() method is called  
     after init().
Ø    It is also called to restart an 
     applet after it has been stopped.

        This method is called each time the applet’s output must be redrawn.

       This method is called when a web browser leaves the HTML document containing the applet.


Ø           The destroy() method is called when the environment determines that the applet need to be removed completely from memory.
Ø            At this point, the user should free up any resources the applet may be using.


Applet Fundamentals
import java.awt.*;
import java.applet.*;
 <applet code="simpleapplet" width=200 height=200>
public class simpleapplet extends Applet {
  public void paint(Graphics g) {
      g.drawString(“A simple Applet”,20,20);

          Applets interact with the user through the AWT, not through the console-based I/O classes.

          It imports two packages.

          paint() is defined by AWT and must be overridden by the applet.

          The paint() method has one parameter of type Graphics. This contains the graphic context, which describes the graphics environment in which the applet is running.This context is used whenever output to the applet is required.


          Graphics class provides methods for drawing simple geometric shapes, text, and images to the graphics destination.
          All of the graphics-related operations on a component or image occur via one of these methods.
The Graphics class provides two methods that draw text on a component or an image
          void drawString(String str, int x, int y)
          void drawChars(char [] data, int offset, int length, int x, int y)

void drawString(String message, int x, int y)

Ø     drawString() is a member of Graphics class
Ø    This method outputs the string.
public void paint(Graphics g)


   g.drawString("abc", 25, 25);


public void paint(Graphics g)


   char [] rgc = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };

   g.drawChars(rgc, 0, 5, 25, 25);

   g.drawChars(rgc, 5, 5, 25, 50);

Running an Applet
 <applet code="AnotherHello.class" width="300" height="150">

Ø   This tells the browser to run the applet AnotherHello.class, and to use an area of the screen 300 pixels wide and 150 high.
The HTML Applet Tag
 [CODEBASE=codebase URL]
 [NAME=appletInstance Name]
[<PARAM NAME=attributeName VALUE=value>]


Typical parameters for an applet would be:
          color used by the applet
          font and font size to be used on text in the applet
          name of an image file to be inserted in the applet

Drawing Lines
g.drawLine(x1, y1, x2, y2);

Ø   where (x1, y1) and (x2, y2) are the endpoints of your lines
Drawing Rectangles
public void drawRect(int x, int y, int width, int height);

Ø  the first int is the left hand side of the rectangle, the second is the top of the rectangle, the third is the width and the fourth is the height.

Ovals and Circles
          public void drawOval(int left, int top, int width, int height)
          public void fillOval(int left, int top, int width, int height)
          public void drawArc(int left, int top, int width, int height, int startAngle, int stopAngle)

           public void fillArc(int left, int top, int width, int height, int startAngle, int stopAngle)
          Color medGray = new Color(127, 127, 127);
          Color cream = new Color(255, 231, 187);

          Color lightGreen = new Color(0, 55, 0); 


Question asked in Capgemini interview for our fresher student!!

What are the methods in thread class??

static methods:

instance methods:
Static Methods of Thread Class

   static int activeCount() - returns the number of currently
            active threads. For example:
            -       num_ threads = Thread. activeCount();

   static Thread currentThread() - returns the object corresponding to the currently executing thread (self reference)
            -       Thread myself = Thread. currentThread();

   static void sleep( long millis) throws InterruptedException -
            this causes the current thread to sleep for the specified
            amount of time. Other threads will run at this time.

   You can also specify the number of nanoseconds as well
            -       static void sleep(long millis, int nano);

   static void yield() - causes the thread to yield the processor
            to any other waiting threads - 

                  Java does not guarantee preemption, you should use this to ensure fairness.

Instance Methods of Thread Class
      These methods control the thread represented by a Thread

      int getPriority() - returns the threads priority - a value
            between Thread. MIN_ PRIORITY and
            Thread. MAX_ PRIORITY

      void setPriority( int newpri)
            -         this sets the threads priority
            -         high priority threads will preempt lower ones when they become ready to run.
                           Thread myself = Thread. currentThread();
                           myself. setPriority( Thread. MAX_ PRIORITY);

      A ThreadGroup may restrict the maximum priority of all its
            member threads - therefore the setPriority method may not

      void start() throws IllegalThreadStateException - actually
            starts the thread - the thread starts and enters the run() method

      void stop() throws SecurityException - stops the thread

      void run() - this method is called when the thread is started.

      This is what the thread will execute while it is alive.

      boolean isAlive() - returns a value indicating whether the
            thread is currently alive - i.e. Started more recently and has not yet been died.

      void suspend() - suspends the threads execution

      void resume() - resumes the execution of the thread

      void join() – causes the caller to wait until the thread dies

 Applet creation in java-video tutorials