Java FAQ



What is Java Virtual Machine? State its purpose.        (Or)
Is Java platform independent? Justify.                          (Or)
How is a Java bytecode interpreted?                              (Or)
What is the need for bytecode generation?

A programming language is said to be platform independent if it can create an application program that can be executed on any platform. Java Virtual Machine (JVM) provides platform independence to Java.
When a Java program is compiled it generates a bytecode file. The JVM is available with all operating systems and can be installed in any host machine. It takes the bytecode file as an input, interprets it generates an output corresponding to the underlying platform (OS) of the host machine and then executes it. Hence Java is platform independent.




Differentiate static binding and dynamic binding.

Static binding
Dynamic binding
Also called early binding
Also called a slate binding
Binding happens at compile time
Binding happens at run time.
Actual object is not used for binding
Actual object used for binding


What is the use of ‘extends’ keyword?

‘extends’ keyword is used in inheritance in Java. It allows the public and protected variables and methods of the super class to be inherited by the subclass.
Example:
          class A
          {
          }
          class B extends A
{
}


What is static in Java?

Static is a keyword in Java used with variables, methods and block of code. Static belongs to the class and not the object. It has only one instance and is shared by all the objects of the class.
Static block of code in a class will be executed before the objects are created for the class. Static variables and methods can be accessed using the class name. Static methods can use / manipulate only static variables.


What is a StringBuffer? How does it differ from String class?
StringBuffer is a peer class of String that provides much of the functionality of Strings. StringBuffer have characters and substrings inserted in the middle or appended to the end. StringBuffer is mutable whereas String class is immutable. StringBuffer is fast and consumes less memory when compared to String class.


What is the use of super keyword?
How is super used in constructors?
‘super’ is a keyword in Java used to access the super class variables and constructors.
Example
import java.io.*;

class A
{
          int x;
          A(int i)
          {
                   x = i;
          }
}       
class B extends A
{
          int y;
          B(int i, int j)
{
                   super(i);                // calls the super class constructor
                    y = j;
          }
          void fnDisplay()
          {       


// Accesses the super class variable
          System.out.println("x = "+ super.x);
          System.out.println("y = " + y);
          }
}
class pgm1
{
          public static void main(String as[])
          {
                   B obj = new B(5, 6);
          obj.fnDisplay();
          }
}


Differentiate constructors and methods.

Constructor
Method
Special member function of class
Member function of a class
Same name as class name
Any name can be used
Always public.
May be public, private or protected.
No return values
May or may not have return values.
Automatically invoked when objects are created.
Have to be explicitly invoked using the object of a class
Mainly used to initialize the variables of a class
Performs a specific task

Syntax – constructor

<Class name> (<Argument list>)
{
}

Syntax – method
<Return type> <Method name> (<Argument list>)
{
}



What is an interface? How is it defined?

Interface in Java is a mechanism to achieve abstraction. An interface is declared using the keyword interface. It has only method declarations without body. A class can implement the interface using the keyword ‘implements’. Each method of the interface has to be defined in the class.
Interface cannot be instantiated. Multiple inheritance in Java can be partially implemented using interface.

Syntax:
interface <interface name>
          {
                   // Method declarations
          }



What is an abstract class? List its characteristics.

A class that is declared with ‘abstract’ keyword is known as abstract class. It cannot be instantiated. They are used to declare common characteristics of subclass. An abstract class can include methods that contain no implementation. If an abstract class is inherited, then the derived class must provide the implementations for all the methods of the abstract class. 


How do you create and initialize arrays in Java?

An array is a set of related elements of same datatype which share a common name and are stored in contiguous memory locations. Each value is identified by its index.
Syntax for declaration
          <data type> [] <array name> = new <datatype>[array size];
 Declaration and initialization
          <data type> [] <array name> = {value1, value2, ….};



What is exception handling?

An exception is an abnormal condition that arises during run time and disrupts the normal flow of the program. Java provides a mechanism called exception handling for handling such situations. Its implemented using keywords try, catch, throw, throws and finally.



What is event delegation model?

Event handling in Java involves source and listener. A source is an awt component like button, checkbox, list, etc. which may generate several types of events. A listener is an object which may register to receive an event. When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument.
To implement the event delegation Model
Implement the appropriate interface in the listener so that it will receive the type of event desired.
Implement code to register and unregister (if necessary) the listener as a recipient for the event notifications.





What is inner class and anonymous inner class?

A class declared inside another class is known as inner class. The inner class can access all the members of the outer class including its private members.
An anonymous inner class is a type of inner class that has no name and can be instantiated only once. It is declared inside the method and terminated by a semicolon.

Example – Inner class
 Class A
{
          Class B        // inner class
          {
          }
}


Example – Anonymous Inner class – MouseAdapter

public class AnonymousInnerClassDemo extends Applet
{
public void init()
{
addMouseListener (new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
showStatus("Mouse Pressed");
}
});
}
}




Differentiate between choice and list objects.

Choice
List
Type of AWT control
Type of AWT control
Pull down menu.
Pull down menu
Choice is displayed in compact form. Only one item is visible.
Multiline display. Multiple items may be visible.
Allows selection of one item only
Multiple selections possible.







No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu