Saturday, 20 December 2014

Top Core Java Interviews Questions Part One

This post contains series of  frequently asked core java questions.

Que: What is Java ?

Ans: Java is Object Oriented Programing language and it follow OOPs concepts. java orignally developed by Sun Microsystem Company but now it is product of Oracle Corporation. To see more click on  Features of Java

Que: What is OOPs concepts ?

Ans: OOPs stands for Object Oriented Programming Standard.Under OOPs comes different
concepts like Object,Class,Inheritance,Encapsulation,Polymorphism and Abstraction.
To see more click on  OOPs concepts


Que: What is the main difference between Java platform and other platforms ?

Ans:The Java platform differs from  other platforms in the sense that it's a software-based platform that runs on top of other hardware-based platforms(OS).It has two components:
1. Runtime Environment
2. API(Application Programming Interface)

Que: What is platform?

Ans: A platform is basically the hardware or software environment in which a program runs.There are two types of platforms software-based and hardware-based.Java is a software-based platform.

Que: What is difference between JDK,JRE and JVM ?

Ans:  JVM(Java Virtual Machine)

JVM is an acronym of Java Virtual Machine,it is an abstract machine which provides the runtime
environment in which java bytecode can be executed.
JVMs are available for many hardware and software platforms (so JVM is plateform dependent).

JRE(Java Runtime Environment)

JRE stands for Java Runtime Environment. It is the implementation of JVM and it provide environment to run byte code and physically exists.

JDK(Java Development Kit)

JDK is an acronym of Java Development Kit. It physically exists. It contains JRE + development tools(javac,javap).
To see more click on  What is JVM, JRE, JDK

Que: What is byte code in Java ?

Ans: Byte code is binary code(machine code) compile by java compiler so that byte code run on any OS plateform. Byte code is  responsible for Java is Platform Independent language.

Que: How many types of memory areas are allocated by JVM ?

Ans: Five(5) types:
1. Class(Method) Area
2. Heap
3. Stack
4. Program Counter Register
5. Native Method Stack

To see more click on   JVM

Que: What is JIT compiler ?

Ans: Just-In-Time(JIT) compiler:It is used to improve the performance.JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation.Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

Que: Why Java known as 'write once and run anywhere' programming ?

Ans: Because Java compiled the code into byte code which is the intermediate language between source code and machine code. This byte code is not platorm specific and hence can be run on any platform.

Que: What is classloader ?

Ans: Classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.

Que: Can you run java program with blank name (.java) file name ?

Ans: Yes, save your java file by .java only, compile it by javac .java and run by java yourclassname.

For example:

Save as  .java only.

class Test{
public static void main(String []args){
System.out.println("Welcome Java World");
 }
}

To compile:>javac .java
To run    :>java Test(class name)

Que:What if I write static public void instead of public static void ?

Ans: Program compiles and runs properly.

Que: What is the default value of the local variables ?

Ans: The local variables are not initialized to any default value, neither primitives nor object references.

Que: What will be the initial value of an object reference which is defined as an instance variable ?

Ans: The object references are all initialized to null in Java.

Que:What is constructor ?

Ans: Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.To see more click on  Constuctor

Que: What is the purpose of default constructor ?

Ans: The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.

Que: Does constructor return any value ?

Ans: Yes, that is current instance(Object) but you cannot use return type yet it returns a value.

Que: Can  constructor inherited ?

Ans: No, constructor is not inherited.

Que: What is static block ?

Ans: Is used to initialize the static data member.
It is excuted before main method at the time of classloading.

Que: What is static variable ?

Ans: static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
static variable gets memory only once in class area at the time of class loading.

Que: What is static method ?

Ans: because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation.
To see more click on   static keyword

Que: Can we execute a program without main() method ?

Ans: Yes,by static block we can run program. To see more click on  static keyword

Que: What if the static modifier is removed from the signature of the main method ?

Ans: Program compiles. But at runtime throws an error "NoSuchMethodError".

Que: What is this in java ?

Ans: It is a keyword that that refers to the current object.To see more click on  this keyword

Que: What is Inheritance ?

Ans: Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship.It is used for Code Resusability and Method Overriding.
To see more click on  Inheritance

Que: Which class is the superclass for every class.

Ans: Object class.Defined under java.lang package

Que: Why multiple inheritance is not supported in java ?

Ans: To reduce the complexity and simplify the language,multiple inheritance is not supported in java in case of class.To see more click on  Inheritance

Que: What is composition ?

Ans: Holding the reference of the other class within some other class is known as composition.

Que: What is difference between aggregation and composition ?

Ans: Aggregation represents weak relationship whereas composition represents strong relationship.
For example:
Car has an indicator (aggregation) but Car has an engine (compostion).


Que: Why Java does not support pointers ?

Ans: Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.

Que: What is super in java ?

Ans: It is a keyword that refers to the immediate parent class object.To see more click on
super keyword

Que: Can you use this() and super() both in a constructor ?

Ans: No. Because super() or this() must be the first statement.

Que: What is method overriding ?

Ans: If a subclass provides a specific implementation of a method that is already provided by its parent class,it is known as Method Overriding.It is used for runtime polymorphism and to provide the specific implementation of the method.To see more click on  method overriding

Que: Can we override static method ? 

Ans: No, you can't override the static method because they are the part of class not object.

Que: Why we cannot override static method ?

Ans: It is because the static method is the part of class and it is bound with class whereas instance method is bound with object and static gets memory in class area and instance gets memory in heap.

Que: Can we override the overloaded method ?

Ans: Yes.

Que: What is object cloning ?

Ans: The object cloning is used to create the exact copy of an object.To see more click on
object cloning

Que: What is method overloading ?

Ans: If a class have multiple methods by same name but different parameters, it is known as Method
Overloading. It increases the readability of the program.To see more click on method overloading

Que: Why method overloading is not possible by changing the return type in java ?

Ans: Because of code ambiguity(compile time error).To see more click on  method overloading

Que: Can we overload main() method ?

Ans: Yes, ofcourse! You can have many main() methods in a class by overloading the main method.
To see more click on   method overloading

Que: What is covariant return type ?

Ans: Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type.

Que Can you declare the main method as final ?

Ans: Yes, such as, public static final void main(String []args){}.

Que: What is final variable ?

Ans: If you make any variable as final, you cannot change the value of final variable(It will be
constant).To see more click on  final keyword

Que: What is final method ?

Ans: Final methods can't be overriden.To see more click on  final keyword

Que: What is final class ?

Ans: Final class can't be inherited.To see more click on  final keyword

Que: What is blank final variable ?

Ans: A final variable, not initalized at the time of declaration,is known as blank final variable.
To see more click on   final keyword

Que: Can we intialize blank final variable ?

Ans: Yes, only in constructor if it is non-static. If it is static blank final variable, it can be initialized only in the static block.To see more click on   final keyword







No comments:

Post a Comment