Java Interview Questions
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 classes.||is-a relationship between classes.|
|Used in Dependency Injection||Used 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.in ?
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 System.in 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.