Monday, 19 May 2014

Java Important Question





  


JAVA

 

1. What is JVM? Why is Java called the ‘Platform Independent Programming Language’?
 
Ans : JVM, or the Java Virtual Machine, is an interpreter which accepts ‘Bytecode’ and executes it.

Java has been termed as a ‘Platform Independent Language’ as it primarily works on the notion of ‘compile once, run everywhere’.

Here’s a sequential step establishing the Platform independence feature in Java:
The Java Compiler outputs Non-Executable Codes called ‘Bytecode’.
Bytecode is a highly optimized set of computer instruction which could be executed by the Java Virtual Machine (JVM).

The translation into Bytecode makes a program easier to be executed across a wide range of platforms, since all we need is a JVM designed for that particular platform.

JVMs for various platforms might vary in configuration, those they would all understand the same set of Bytecode, thereby making the Java Program ‘Platform Independent’.


2. What is the Difference between JDK and JRE?

 
Ans: The “JDK” is the Java Development Kit. I.e., the JDK is bundle of software that you can use to develop Java based software.

The “JRE” is the Java Runtime Environment. I.e., the JRE is an implementation of the Java Virtual Machine which actually executes Java programs.

Typically, each JDK contains one (or more) JRE’s along with the various development tools like the Java source compilers, bundling and deployment tools, debuggers, development libraries, etc.

3. What does the ‘static’ keyword mean?

 
Ans: Static variable is associated with a class and not objects of that class.
Example:
public class ExplainStatic
{
public static String name = "Look I am a static variable";
}

We have another class where-in we intend to access this static variable just defined.
public class Application
{ public static void main(String[] args)
{
System.out.println(ExplainStatic.name)
}
}
We don’t create object of the class ExplainStatic to access the static variable. We directly use the class name itself: ExplainStatic.name

4. What are the Data Types supported by Java? What is Autoboxing and Unboxing?

This is one of the most common and fundamental Java interview questions. This is something you should have right at your finger-tips when asked. The eight Primitive Data types supported by Java are:

byte : 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).

short : 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).

int : 32-bit signed two’s complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).

long : 64-bit signed two’s complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).

float : 32 bit signed floating point.

double: 64 bit signed floating point.

boolean : 1 bit sized, capable of storing either true or false.

Autoboxing: The Java compiler brings about an automatic transformation of primitive type (int, float, double etc.) into their object equivalents or wrapper type (Integer, Float, Double,etc) for the ease of compilation.

Unboxing: The automatic transformation of wrapper types into their primitive equivalent is known as Unboxing.


5. What is the difference between STRINGBUFFER and STRING?

String object is immutable. i.e , the value stored in the String object cannot be changed.
StringBuffer/StringBuilder objects are mutable: StringBuffer/StringBuilder objects are mutable; we can make changes to the value stored in the object.

6. What is Function Over-Riding and Over-Loading in Java?

 
Over-Riding: An override is a type of function which occurs in a class which inherits from another class. An override function “replaces” a function inherited from the base class, but does so in such a way that it is called even when an instance of its class is pretending to be a different type through polymorphism. That probably was a little over the top. The code snippet below should explain things better.

public class Car
{
public static void main (String [] args)
{
Car a = new Car();
Car b = new Ferrari(); //Car ref, but a Ferrari object
a.start(); // Runs the Car version of start()
b.start(); // Runs the Ferrari version of start()
}
}
class Car
{
public void start()
{
System.out.println("This is a Generic start to any Car");
}
}
class Ferrari extends Car
{
public void start()
{
System.out.println("Lets start the Ferrari and go out for a cool Party.");
}
}

Over-Loading: Overloading is the action of defining multiple methods with the same name, but with different parameters. It is unrelated to overriding. Functions in Java could be overloaded by two mechanisms ideally:

Varying the number of arguments.
Varying the Data Type.
class CalculateArea
{
void Area(int length){System.out.println(length*2);}
void Area(int length , int width){System.out.println(length*width);}

public static void main(String args[])
{
CalculateArea obj=new CalculateArea();
obj.Area(10); // Area of a Square
obj.Area(20,20); // Area of a Rectangle

}
}

7. What is Constructors?

 
Constructors form the basics of OOPs, for starters.

Constructor: The sole purpose of having Constructors is to create an instance of a class. They are invoked while creating an object of a class. Here are a few salient features of Java Constructors:

1.Constructors can be public, private, or protected.
2.If a constructor with arguments has been defined in a class, you can no longer use a default no-argument constructor – you have to write one.
3.They are called only once when the class is being instantiated.
4.They must have the same name as the class itself.
5.They do not return a value and you do not have to specify the keyword void.
If you do not create a constructor for the class, Java helps you by using a so called default no-argument constructor.

No comments:

Post a Comment