Solved Interview Questions of JAVA…..

Mar 11 • Engineering Sample Papers • 1509 Views • No Comments on Solved Interview Questions of JAVA…..

Solved Interview Questions of JAVA…

Interview Questions of JAVA

Interview Questions of JAVA

1. What are the different ways to handle exceptions?

ans.There are two ways to handle exceptions,
1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions. and
2. List the desired exceptions in the throws clause of the method and let the caller of the method hadle those exceptions.

2. If my class already extends from some other class what should I do if I want an instance of my class to be thrown as an exception object?

ans.One can not do anytihng in this scenarion. Because Java does not allow multiple inheritance and does not provide any exception interface as well.

3. What is the difference between error and an exception?

ans.An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error.
These JVM errors and you can not repair them at runtime. While exceptions are conditions that occur because of bad input etc. Example: FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference.

4. Can I import same package/class twice? Will the JVM load the package twice at runtime?

ans.One can import the same package or same class multiple times. compiler and JVM will not complains about it. And the JVM will internally load the class only once no matter how many times you import the same class.

5. What are Checked and UnChecked Exception?

ans.A checked exception is a subclass of Exception or Exception itself, excluding class RuntimeException and its subclasses. Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown.
Example: IOException thrown by java.io.FileInputStream’s read() method·
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn’t force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown.
Example: StringIndexOutOfBoundsException·
Checked exceptions must be caught at compile time.

6. What is Overriding?

ans.When a class defines a method using the same name, return type, and arguments as a method in its superclass, the method in the class overrides the method in the superclass.

7. Primitive data types are passed by reference or pass by value?

ans.Primitive data types are passed by value.

8. Objects are passed by value or by reference?

ans.Java only supports pass by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object.

9. What is serialization?

ans.Serialization is a concept by which you can save the state of an object by converting it to a byte stream.

10. How do I serialize an object to a file?

ans.The class whose instances are to be serialized should implement an interface Serializable ,pass the instance to the ObjectOutputStream which is connected to a fileoutputstream,That will save the object to a file.

11. Which methods of Serializable interface should I implement?

ans.The serializable interface is an empty interface, it does not contain any methods.

12. How can I customize the seralization process? i.e. how can one have a control over the serialization process?

ans.Yes it is possible to have control over serialization process. The class should implement Externalizable interface. This interface contains two methods namely readExternal and writeExternal.

13. What are wrapper classes?

ans.Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper classes.
They are example: Integer, Character, Double etc.

14. Why do we need wrapper classes?

ans.It is sometimes easier to deal with primitives as objects. Moreover most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many utility methods also.
Because of these reasons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection. Also we can pass them around as method parameters where a method expects an object.

15. What are checked exceptions?

ans.Checked exception are those which the Java compiler forces you to catch.
Example: IOException are checked exceptions.

16. What are runtime exceptions?

ans.Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not checked by the compiler at compile time.

17. Can I have multiple main() methods in the same class?

ans.No the program fails to compile. The compiler says that the main() method is already defined in the class.
18. Do I need to import java.lang package any time? Why ?

ans.No. It is by default loaded internally by the JVM.

19. Can an application have multiple classes having main() method?

ans.Yes it is possible.at the start of the application you have to mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned.Hence there is not conflict amongst the multiple classes having main() method.

20. How to create custom exceptions?

ans.Your class should extend class Exception, You should implement these methods and write the logic for customizing the serialization process.

Tell us Your Queries, Suggestions and Feedback

Your email address will not be published.

« »