CORE JAVA INTERVIEW QUESTIONS
1. What is the JAR file?
JavaARchive files are a big glob of Java classes, images, audio, etc., compressed to make one simple, smaller file to ease Applet downloading. Normally when a browser encounters an applet, it goes and downloads all the files, images, audio, used by the Applet separately. This can lead to slower downloads.
2. Why java is not 100% oops?
Java uses primitive types such as int, char, double. But then all the rest are objects.
3. Is finally block will be executed always?
Ans. Yes finally block will be executed always irrespective of whether exception raised or not raised whether exceptions are handled or not handle. There is one situation where the finally block won’t be executed if the JVM is going to be shut down.
4. What is similarities/difference between an Abstract class and Interface?
Differences are as follows:
• Interfaces provide a form of multiple inheritance. A class can extend only one other
• Interfaces are limited to public methods and constants with no implementation.
Abstract classes can have a partial implementation, protected parts, static methods,
• A Class may implement several interfaces. But in the case of an abstract class, a class may extend only one abstract class.
• Interfaces are slow as it requires extra indirection to find the corresponding method in the actual class. Abstract classes are fast.
• Neither Abstract classes or Interface can be instantiated.
5. What is an I/O filter?
An I/O filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another.
6. What is numeric promotion?
Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place. In numerical promotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required.
7. What are various methods to print Exception information? and differentiate them.
Throwable class defines the following method to print exception or error information.
1. printStackTrace() :- This method print exception information in the following format.
Name of the Exception: Description
2.toString():- This method print exception information in the following format.
Name of the Exception: Description
3.getMessage():- This method
prints only description of the exception.
8. Is it possible to take a try, catch inside try block?
Yes, It is possible to take a try, catch inside try block. That is nesting of try-catch is possible.
9. Is it possible to write any statement between try-catch and finally?
Ans. No, it is not possible to write any statement between try catch and finally. If we will try to write any statement between them then we will get compile time error.
10. Is it possible to throw any java object?
Ans. No, we can use throw keyword only for throwable objects otherwise we will get compile time error saying incompatible type.
11. What is the difference between Error and Exception?
Ans. Throwable class contain two child classes.
Exception:- These are mostly caused by our program and are recoverable.
Error:- These are not caused by our program, mostly caused by lake of system resources. These are non recoverable.
10.Is overloading of run() method is possible?
Yes, we can overload run() method but Thread class start() method always invokes no-argument run() method only. The other run() method we have to call explicitly then only will be executed.
11. If two threads having same priority then which thread will get chance first for execution?
If two threads having the same priority then which thread will get the chance first for execution decided by Thread Scheduler. It is the part of JVM and its behavior is vendor dependent and we can’t expect exact output.
12 .How we can prevent a thread from execution?
We can prevent a Thread from executin by using the following methods:
13. What is the advantage of synchronized block over synchronized method?
Ans. If very few lines of the code required synchronization then declaring entire method as the synchronized is not recommended. We have to declare those few lines of the code inside synchronized block. This approach reduces waiting time of the Thread and improves performance of the system.
14. Once we created a new thread is it daemon or non-daemon.
Once we created a new Thread, The Daemon nature will be inheriting from parent to child. If the parent is Daemon the child is also Daemon and if the parent is non-daemon then child is also non-daemon.
15. When the Daemon thread will be terminated?
Once last non-daemon Thread terminates automatically every Daemon Thread will be terminated.