Tuesday 24 April 2018

Core Java Interview Questions

1) What is difference between JDK,JRE and JVM?

JVM: JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the runtime environment in which java bytecode can be executed. It is a specification.
JVMs are available for many hardware and software platforms (so JVM is platform dependent).
JRE: JRE stands for Java Runtime Environment. It is the implementation of JVM.


JDK:JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.


2) How many types of memory areas are allocated by JVM?

Many types:
Class(Method) Area
Heap
Stack
Program Counter Register
Native Method Stack


3) 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.


4) What is platform?
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 provides software-based platform.


5) What is the main difference between Java platform and other platforms?
The Java platform differs from most other platforms in the sense that it's a software-based platform that runs on top of other hardware-based platforms.It has two components:

  • Runtime Environment
  • API(Application Programming Interface)


6) What gives Java its 'write once and run anywhere' nature?
The bytecode. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform.


7) What is classloader?
The 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.


8) Is Empty .java file name a valid source file name?
Yes, save your java file by .java only, compile it by javac .java and run by java yourclassname Let's take a simple example:


//save by .java only  
class A{  
public static void main(String args[]){  
System.out.println("Hello java");  
}  
}  
//compile by javac .java  
//run by     java A
compile it by javac .java


run it by java A


9) Is delete,next,main,exit or null keyword in java?
No.


10) If I don't provide any arguments on the command line, then the String array of Main method will be empty or null?
It is empty. But not null.


11) What if I write static public void instead of public static void?
Program compiles and runs properly.


12) What is the default value of the local variables?
The local variables are not initialized to any default value, neither primitives nor object references.
There is given more than 50 OOPs (Object-Oriented Programming and System) interview questions. But they have been categorized in many sections such as constructor interview questions, static interview questions, Inheritance Interview questions, Abstraction interview question, Polymorphism interview questions etc. for better understanding.


13) What is difference between object oriented programming language and object based programming language?
Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.


14) What will be the initial value of an object reference which is defined as an instance variable?
The object references are all initialized to null in Java.


15) What is constructor?
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.


16) What is the purpose of default constructor?
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.


17) Does constructor return any value?
Ans:yes, that is current instance (You cannot use return type yet it returns a value).


18)Is constructor inherited?
No, constructor is not inherited.


19) Can you make a constructor final?
No, constructor can't be final.


20) What is static variable?
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.


21) What is static method?
A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.


22) Why main method is static?
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.


23) What is static block?
Is used to initialize the static data member.

It is excuted before main method at the time of classloading.


24) What is difference between static (class) method and instance method?

static or class method instance method

static or class method
instance method
1)A method i.e. declared as static is known as static method.
A method i.e. not declared as static is known as instance method.
2)Object is not required to call static method.
Object is required to call instance methods.
3)Non-static (instance) members cannot be accessed in static context (static method, static block and static nested class) directly.
static and non-static variables both can be accessed in instance methods.
4)For example: public static int cube(int n){ return n*n*n;}
For example: public void msg(){...}.

No comments:

Post a Comment