Java Interview Questions

Prev Tutorial Next Tutorial

Java Interview Questions

What is a class loader ?

Part of JVM which is used to load classes and interfaces.

What are the different class loaders used by JVM ?

Bootstrap , Extension and System are the class loaders used by JVM.

Different types of memory used by JVM ?

Class , Heap , Stack , Register , Native Method Stack.

What is the purpose of the System class ?

System class provide access to system resources.

Variable of the boolean type is automatically initialized as ?

The default value of the boolean type is false.

Difference between static vs. dynamic class loading ?

static loading: Classes are statically loaded with Java new operator.
dynamic class loading: Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time.

Difference between composition and inheritance in Java ?

has-a relationship between relationship between classes.
Used in Dependency InjectionUsed in Runtime Polymorphism
Single class objects can be composed within multiple classes.Single class can only inherit 1 Class.
Its the relationship between objects.Its the relationship between classes.

What do you mean by "Java is a statically typed language" ?

It means that the type of variables are checked at compile time in Java.The main advantage here is that all kinds of checking can be done by the compiler and hence will reduce bugs.

What are different ways of object creation in Java ?


  • Using new operator - new xyzClass()
  • Using factory methods - xyzFactory.getInstance( )
  • Using newInstance( ) method
  • By cloning an already available object - (xyzClass)obj1.clone( )

Explain Autoboxing ?

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.

What is an Enum type ?

An enum type is a special data type that enables for a variable to be a set of predefined constants.

What are Wrapper Classes ?

A wrapper class is any class which "wraps" or "encapsulates" the functionality of another class or component.

What are Primitive Wrapper Classes ?

A Wrapper Class that wraps or encapsulates the primitive data type is called Primitive Wrapper Class..

Why use Scanner class ?

For reading Data Stream from the input device.

Difference between loadClass and Class.forName ?

loadClass only loads the class but doesn't initialize the object whereas Class.forName initialize the object after loading it.

Should we override finalize method ?

Finalize is used by Java for Garbage collection. It should not be done as we should leave the Garbage Collection to Java itself.

Why Java don't use pointers ?

Pointers are vulnerable and slight carelessness in their use may result in memory problems and hence Java intrinsically manage their use.

Do we need to import java.lang.package ?

No, It is loaded by default by the JVM.

What is the difference between System.out ,System.err and ?

System.out and System.err both represent the monitor by default and hence can be used to send data or results to the monitor. But System.out is used to display normal messages and results whereas System.err is used to display error messages and represents InputStream object, which by default represents standard input device, i.e., keyboard.

Is it possible to compile and run a Java program without writing main( ) method ?

Yes, it is possible by using a static block in the Java program.

Can we call the garbage collector explicitly ?

We can call garbage collector of JVM to delete any unused variables and unreferenced objects from memory using gc( ) method. This gc( ) method appears in both Runtime and System classes of java.lang package.

Are true and false keywords in java ?

No, true and false are not keywords in java. They are literals in java. You can not use them as identifiers in your program. They are reserved words in java.

Can we declare local inner class as private ?

No, local inner class can not be declared as private or protected or public.

Is "abc" a primitive value ?

No, "abc" is not a primitive value. It is a string object.

Is an exception occurred in one thread causes other threads to terminate.?

No, exception is thread wise. Only that thread will terminate in which exception has occurred. Other threads will continue to execute.

Can array size be negative.?

No, array size can not be negative. If you specify array size as negative, there will be no compile time error, but you will get NegativeArraySizeException at run time.

If class A and class B are two sub classes of class C, then can a reference variable of Class A type refer to an object of class B type or Vice versa.?

No. Class A type reference variable can not refer to class B type object or class B type reference variable can not refer to class A type object.

What is the priority of Garbage Collector thread. is it low or high.?

Garbage Collector thread is a low priority thread.

Is an object garbage collected even after an exception is occurred in the program ?

Yes, Garbage collector ignores any exceptions occurred in the program.

Is Map of Collection type ?

No, Map is not a Collection type. Even though Map is included in the collection framework, it does not inherit from Collection interface.

Can we define sub class first and super class later in a java file ?

Yes, Order of sub class and super class does not matter.

Which package is always imported by default ?

java.lang package is always imported by default.

Can a class implement two interfaces having same method ?

Yes, a class can implement two interfaces having same method but that method should be implemented only once ( or can be overloaded ) in the class.

Which one will be faster ?

  • for(int i = 0; i < 1000; i++) {}
  • for(int i = 1000; i > 0; i–) {}

for(int i = 1000; i > 0; i–) {} will be faster.

Can we declare interface methods as static ?

No, we can’t declare interface methods as static.

Prev Tutorial Next Tutorial

Facebook Likes