Wednesday 31 July 2013

Java interview questions and answers 3



101. What are the three types of  Thread priority ?
Ans: Thread have following three priority
  1. Thread.MIN_PRIORITY - minimum thread priority.
  2. Thread.MAX_PRIORITY - maximum thread priority.
  3. Thread.NORM_PRIORITY - default thread priority
102. What is the use of synchronizations ?
Ans: The synchronized keyword can be used for locking in program or utilize for lock and free the resource for any thread/program.
103. Garbage collector thread belongs to which priority ?
Ans: Low-priority Thread.

104. What is meant by time-slicing ?
Ans: Time- slice means which provided threads of equal priority and enable to share a processor equally, even if the thread has not finished executing while the quantum expires, the processor is taken away from that thread and given to the next thread of equal priority, if another thread is available.
105. What is the use of ‘this’ ?
Ans: Simple this keyword is refer to the current state of an object and also enable to call a constructor to another constructor

·          
107. How to compare two strings ?
Ans: By equal() method.
108. What are the interfaces defined by java.lang.Package?
Ans:  Following interface defined in java.lang.Package are
  • Appendable - An object to which char sequences and values can be appended. The Appendable interface must be implemented by any class whose   instances are intended to receive formatted output from a Formatter. Appendables are not necessarily safe for multithreaded access.
    (Introduce since JDK- 1.5 ).
     
  • CharSequence -A CharSequence is a readable sequence of char values. This interface provides uniform, read-only access to many different kinds of char sequences. A char value represents a character in the Basic Multilingual Plane (BMP) or a surrogate.                                                          (Introduce since JDK- 1.4 ).
     
  • Cloneable -A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. it is important that Cloneable doesn't contain any clone()method.                                                                     (Introduce since JDK- 1.0 ).
     
  • Comparable<T> -This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo() method is referred to as its natural comparison method.
*T - the type of objects that this object may be compared to                                                                                                                              ( Introduce since JDK- 1.2)
  • Deprecated -A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.                     (Introduce since JDK- 1.4 ).
     
  • Iterable<T> -Implementing this interface allows an object to be the target of the "foreach" statement .                                                                   (Introduce since JDK- 1.5 ).
     
  • Override - Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.                                                                              (Introduce since JDK- 1.5 ).
     
  • Readable - A Readable is a source of characters. Characters from a Readable are made available to callers of the read method via a CharBuffer. (Introduce since JDK- 1.5 ).
     
  • Runnable -The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run. This interface is designed to provide a common protocol for objects that wish to execute code while they are active.  (Introduce since JDK- 1.0 ).
     
  • SuppressWarnings - Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element).
    (Introduce since JDK- 1.5 ).
    (** Following data taken form sun/java)
109. What is the purpose of run-time class and system class? The System class provide system level functionality to Java applications. Some of the facilities provided by System class: * Reading input from the keyboard. * Writing output to console. * Reading and writing system properties and environment variables Runtime class provides support for runtime environment.

110. What is meant by Stream and Types of Stream ?
Ans:  Generally, Streams are a way of transferring and filtering information or use data in one form or another, however it is as input, output, or both. The sources of input and output can vary between a local file, a database, variables in memory, a socket on the network, or another program. Streams may be in between byte and character streams and various stream classes define into the java.io package, so stream are of two type-
  • Input Stream
  • Output Stream
111. What is the method used to clear the buffer ?
Ans: For Clear the buffer following method is call-
  • clear() -The position is set to zero, the limit is set to the capacity, and the mark is discarded.
  • flip() - The limit is set to the current position and then the position is set to zero.
  • rewind()- The position is set to zero and the mark is discarded.
112. What is meant by StreamTokenizer ?
Ans: The StreamTokenizer class takes an input stream and parses it into "tokens", allowing the tokens to be read one at a time. 
113. What is serialization and de-serialization ?
Ans: Serialization is the process of  transforming a data structure/object into a sequence of bits stream so that it can be stored in a memory buffer or file, or transmitted across a network while in case of Deserialization is the inverse process of reconstructing an object from a byte stream to the same state in which the object was previously serialized.

  // Serialization code

   FileOutputStream out = new FileOutputStream( “save.ser” );

   ObjectOutputStream x =  new ObjectOutputStream( out );

   oos.writeObject( new Date() );

   x.close();



  //Deserialization code

   FileInputStream in =  new FileInputStream( “save.ser” );

   ObjectInputStream x = new ObjectInputStream( in );

   Date d = (Date) ois.readObject();

   x.close();

114. What is meant by Applet ?
Ans: Applets are small applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as part of a Web document.
115. How to find the host from which the Applet has originated ?
Ans: Following two method may be used-

   
public java.net.URL getDocumentBase();
   
public java.net.URL getCodeBase();
116. What is the life cycle of an Applet ?
Ans: Life cycle of an Applet totally dependent upon following four method which is-
   
   public void init();
   public void start();
   public void stop();
   public void destroy();

  // A very simpleDemo of an Applet life

  package r4r.co.in;

  import java.applet.Applet;
  import java.awt.Color;
  import java.awt.Graphics;

  public class AppletDemo extends Applet {

    StringBuffer Demo;

    public void init() {
        Demo = new StringBuffer();
        addItem("initializing/java//java/. ");            // initializing an applet
    }

    public void start() {
        addItem("starting of an applet. ");       //start an applet
    }

    public void stop() {
        addItem("stopping an applet. ");          //stop an applet
    }

    public void destroy() {
        addItem("Finally unload.");               //Free all the resource
    }

    private void addItem(String newWord) {
        System.out.println(newWord);
        Demo.append(newWord);

    }

    public void paint(Graphics g) {
        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);      // Rectangular area
        g.setColor(Color.BLUE);                                 // Color in applet

        g.drawString(Demo.toString(), 10, 15);                 //Display the string inside the rectangle.

    }
 }

 Result:
    
     initializing/java//java/.
     starting of an applet.
    
     //Remaining two method call on closing the applet.
    
     stopping an applet.
     Finally unload.

117. How do you load an HTML page from an Applet ?
Ans: For such operation used small code which written in HTML file
   
 <applet code="AppletDemo" width=300 height=100>
        </applet>

118. What is meant by AppletStub Interface ?
Ans: The
AppletStub interface is a way to get information from the run-time browser environment. The Applet class provides following methods-
  • public abstract boolean isActive()-The isActive() method returns the current state of the applet.
     
  • public abstract URL getDocumentBase()- The getDocumentBase() method returns the complete URL of the HTML file that loaded the applet.
     
  • public abstract URL getCodeBase()- The getCodeBase() method returns the complete URL of the .class file that contains the applet.
     
  • public abstract String getParameter(String name)- The getParameter() method allows you to get parameters from <PARAM> tags within the <APPLET> tag of the HTML file that loaded the applet.
     
  • public abstract AppletContext getAppletContext()- The getAppletContext() method returns the current AppletContext of the applet.The getAppletContext() method returns the current AppletContext of the applet.
     
  • public abstract void appletResize(int width, int height)- The appletResize() method is called by the resize method of the Applet class.
    (** Following data taken from JAVA AWT Reference( Chapter 14))
119. What is meant by getCodeBase and getDocumentBase method ?(Not sure)
Ans: The getCodebase() method is used to establish a path to other files/folders that are in the same location as the class being run.
         
     URL getCodeBase() -  Gets the base URL.

     URL getDocumentBase() -  Gets the URL of the document in which the applet is embedded.       
       
120. How can you call an applet from a HTML file?
Ans: Following example is used

  <HTML>
    <HEAD>
       <TITLE>AppletDemo example- call applet parameter from HTML file </TITLE>
     </HEAD>
       <BODY>
          <APPLET CODE="AppletDemo.class" WIDTH="500" HEIGHT="100">
         </APPLET>
      </BODY>
  </HTML>
121. What is meant by Applet Flickering ?
Ans: When an applet is executing some graphics using a thread, some color desolation will be happening, this is called as flickering.
122. What is the use of parameter tag ?
Ans: The <param> tag is used to define parameters or variables for an object or applet element.
123. What is audio clip Interface and what are all the methods in it ?
Ans:Three methods define the AudioClip interface-
  • void play()-  Starts playing this audio clip from the beginning.
  • void loop()- Starts playing this audio clip in a continuously loop
  • void stop()- Stops playing this audio clip.
     
124. What is the difference between getAppletInfo and getParameterInfo ?
Ans: getAppletInfo()method is used for returns information about this applet in the String form.
           (public String  getAppletInfo())    
       getParameterInfo() method is used for returns information about the parameters that are understood by this applet in the array of Strings.
           ( public String[][]   getParameterInfo())

125. How to communicate between applet and an applet ?
Ans:JavaScript functions enable communication between two applets by receiving messages from one applet and invoking methods of other applets.

No comments:

Post a Comment