Java Interview Questions with Answers
Java Interview Questions with Answers |
Q)Is null keyword in java?
ANS:No
Q) What is difference between throw and throws?
ANS:
throw:throw is used to explicitly throw an exception.checked exceptions can not be propagated with throw only,throw is followed by an instance,throw is used within the method,You cannot throw multiple exception
throws:throws is used to declare an exception,checked exception can be propagated with throws.throws is followed by class. throws is used with the method signature.You can declare multiple exception e.g. public void method()throws IOException,SQLException.
Q) What is difference between JDK,JRE and JVM?
ANS:
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 stands for Java Runtime Environment. It is the implementation of JVM.
JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.
Q)What is the main difference between Java platform and other platforms?
ANS: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)
Q)What gives Java its 'write once and run anywhere' nature?
ANS: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.
Q)What is classloader?
ANS: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.
Q) Is Empty .java file name a valid source file name?
ANS: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.
Q)If I don't provide any arguments on the command line, then the String array of Main method will be empty or null?
ANS:It is empty. But not null.
Q)What if I write static public void instead of public static void?
ANS:Program compiles and runs properly.
Q) 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.
Q) What is difference between object oriented programming language and object based programming language?
ANS:Object based programming languages follow all the features of OOPs except Inheritance.Examples of object based programming languages are JavaScript, VBScript etc.
Q) 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.
Q) What is constructor?
ANS:
Q)
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.
Q) 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.
Q)Does constructor return any value?
ANS:yes, that is current instance (You cannot use return type yet it returns a value).
Q)Can you make a constructor final?
ANS:No, constructor can't be final.
Q)What is static method?
ANS: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.
Q)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
Q)Why main method is static?
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
Q) What is static block?
ANS:Is used to initialize the static data member.It is excuted before main method at the time of classloading.
Q) What is the use of super keyword in Java?
ANS:It is a keyword that refers to the immediate parent class object
Q) What is final class?
ANS:Final class can't be inherited.
Q)What is blank final variable?
ANS:A final variable, not initalized at the time of declaration, is known as blank final variable.
Q)What is difference between wait() and sleep() method?
ANS:
The wait() method is defined in Object class.wait() method releases the lock.
The sleep() method is defined in Thread class.The sleep() method doesn't releases the lock.
Q)Is it possible to start a thread twice?
ANS:No, there is no possibility to start a thread twice. If we does, it throws an exception
Q)Can we call the run() method instead of start()?
ANS: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.
Q)What about the daemon threads?
ANS: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.
Q) What is synchronization?
ANS: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.
Q)What is the purpose of Synchronized block?
ANS: Synchronized block is used to lock an object for any shared resource.Scope of synchronized block is smaller than the method.
Q)What is difference between Checked Exception and Unchecked Exception?
ANS:
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.
Q)What is the base class for Error and Exception?
ANS:Throwable.
Q)Is it necessary that each try block must be followed by a catch block?
ANS: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
Q)Is there any case when finally will not be executed?
ANS:finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).
Q)What is the meaning of immutable in terms of String?
ANS:The simple meaning of immutable is unmodifiable or unchangeable. Once string object has been created, its value can't be changed.
Q)What is the basic difference between string and stringbuffer object?
ANS:String is an immutable object. StringBuffer is a mutable object.
Q)What is the difference between StringBuffer and StringBuilder ?
ANS:StringBuffer is synchronized whereas StringBuilder is not synchronized.
Q)What is the purpose of toString() method in java ?
ANS:The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding
the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
Q)Is there any difference between nested classes and inner classes?
ANS:Yes, inner classes are non-static nested classes i.e. inner classes are the part of nested classes.
Q) Can we access the non-final local variable, inside the local inner class?
ANS:No, local variable must be constant if you want to access it in local inner class.
Q) 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.
Q)Which class is the superclass for every class?
ANS:Object class.
Q)What is multithreading?
ANS: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.
Study Material @ Placement Papers Hub
Java Interview Questions with Answers
Reviewed by @NA
on
8:42:00 PM
Rating:
No comments: