Java Interview Question

50 most important Java Interview Questions  that every one should know before facing any technical interview so that you can gear up well

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

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

5) 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

6) 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.
7) 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.
8) Difference between method Overloading and Overriding.

Method Overloading Method Overriding
1) Method overloading increases the readability of the program. Method overriding provides the specific implementation of the method that is already provided by its super class.
2) method overlaoding is occurs within the class. Method overriding occurs in two classes that have IS-A relationship.
3) In this case, parameter must be different. In this case, parameter must be same.
9) What is final Keyword In Java

Final variable
Final method
Final class
Is final method inherited?
Blank final variable
Static blank final variable
Final parameter
Can you declare a final constructor
The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be:

Variable
Method
class
The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only.

10) Why we cannot override static method?

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.

11) What is abstract class?

A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.

12) What is interface in Java?

Interface is a blueprint of a class that have static constants and abstract methods.It can be used to achieve fully abstraction and multiple inheritance.

13) Can an Interface be final?

No, because its implementation is provided by another class.

14) What is difference between abstract class and interface?

Abstract class Interface
1) An abstract class can have method body (non-abstract methods). Interface have only abstract methods.
2) An abstract class can have instance variables. An interface cannot have instance variables.
3) An abstract class can have constructor. Interface cannot have constructor.
4) An abstract class can have static methods. Interface cannot have static methods.
5) You can extends one abstract class. You can implement multiple interfaces.
15) What is package?

A package is a group of similar type of classes interfaces and sub-packages. It provides access protection and removes naming collision.

16) What is Exception Handling?

Exception Handling is a mechanism to handle runtime errors.It is mainly used to handle checked exceptions.

17) What is difference between Checked Exception and Unchecked Exception?

1) Checked Exception

The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are checked at compile-time.

2) Unchecked Exception

The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at compile-time.

18) What is the base class for Error and Exception?

Throwable.

19) Is it necessary that each try block must be followed by a catch block?

It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

20) What is difference between throw and throws?

throw keyword throws keyword
1) throw is used to explicitly throw an exception. throws is used to declare an exception.
2) checked exceptions can not be propagated with throw only. checked exception can be propagated with throws.
3) throw is followed by an instance. throws is followed by class.
4) throw is used within the method. throws is used with the method signature.
5) You cannot throw multiple exception You can declare multiple exception e.g. public void method()throws IOException,SQLException.
21) Why string objects are immutable in java?

Because java uses the concept of string literal. Suppose there are 5 reference variables,all referes to one object “sachin”.If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.

22) What is the basic difference between string and stringbuffer object?

String is an immutable object. StringBuffer is a mutable object.

23) What is the difference between StringBuffer and StringBuilder ?

StringBuffer is synchronized whereas StringBuilder is not synchronized.

24) What is Garbage Collection?

Garbage collection is a process of reclaiming the runtime unused objects.It is performed for memory management.

25) What is gc()?

gc() is a daemon thread.gc() method is defined in System class that is used to send request to JVM to perform garbage collection.

26) What is the purpose of finalize() method?

finalize() method is invoked just before the object is garbage collected.It is used to perform cleanup processing.

27) Can an unrefrenced objects be refrenced again?

Yes.

28)What kind of thread is the Garbage collector thread?

Daemon thread.

29)What is difference between final, finally and finalize?

final: final is a keyword, final can be variable, method or class.You, can’t change the value of final variable, can’t override final method, can’t inherit final class.
finally: finally block is used in exception handling. finally block is always executed.
finalize():finalize() method is used in garbage collection.finalize() method is invoked just before the object is garbage collected.The finalize() method can be used to perform any cleanup processing.
30) What is the purpose of the Runtime class?

The purpose of the Runtime class is to provide access to the Java runtime system.

31) How will you invoke any external process in Java?

By Runtime.getRuntime().exec(?) method.

32) What is serialization?

Serialization is a process of writing the state of an object into a byte stream.It is mainly used to travel object’s state on the network.

33) What is Deserialization?

Deserialization is the process of reconstructing the object from the serialized state.It is the reverse operation of serialization.

34) What is transient keyword?

If you define any data member as transient,it will not be serialized.

35) What is Externalizable?

Externalizable interface is used to write the state of an object into a byte stream in compressed format.It is not a marker interface.

36) What is the difference between Serializalble and Externalizable interface?

Serializable is a marker interface but Externalizable is not a marker interface.When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject() two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class’s serialization process.

37) What are wrapper classes?

Wrapper classes are classes that allow primitive types to be accessed as objects.

38) What is a native method?

A native method is a method that is implemented in a language other than Java.

39) What is the purpose of the System class?

The purpose of the System class is to provide access to system resources.

40) What comes to mind when someone mentions a shallow copy in Java?

Object cloning.

41) What is singleton class?

Singleton class means that any given time only one instance of the class is present, in one JVM

42) What is multithreading?

Multithreading is a process of executing multiple threads simultaneously. Its main advantage is:

Threads share the same address space.
Thread is lightweight.
Cost of communication between process is low.
43) What is thread?

A thread is a lightweight subprocess.It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.

44) What is the difference between preemptive scheduling and time slicing?

Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

45) What does join() method?

The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.

46) Is it possible to start a thread twice?

No, there is no possibility to start a thread twice. If we does, it throws an exception.

47) Can we call the run() method instead of start()?

Yes, but it will not work as a thread rather it will work as a normal object so there will not be context-switching between the threads.

48) What about the daemon threads?

The daemon threads are basically the low priority threads that provides the background support to the user threads. It provides services to the user threads.

49) Can we make the user thread as daemon thread if thread is started?

No, if you do so, it will throw IllegalThreadStateException

50) What is synchronization?

Synchronization is the capabilility of control the access of multiple threads to any shared resource.It is used:

To prevent thread interference.
To prevent consistency problem.

Leave a Reply

Your email address will not be published. Required fields are marked *