Skip to main content

Posts

Exception Handling Interview Questions in Java

Exception Handling 1.What is Exception Handling in Java? Java provides Exception Handling mechanism to handle Runtime errors that occur in JVM. There are checked exceptions in a program that we expect to occur in certain situations. Exception handling mechanism catches these checked exceptions and takes relevant actions. 2.In Java, what are the differences between a Checked and Unchecked? Checked Exceptions extend Throwable class, but they do not extend RuntimeException or Error classes. UncheckedException extend RuntimeException class. Checked Exceptions are checked at compile time in Java. Unchecked Exceptions happen at Runtime, so they are not checked at compile time. IOException, SQLException etc. are examples of Checked Exceptions. NullPointerException, ArithmeticException etc. are examples of Unchecked Exceptions. 3. What is the base class for Error and Exception classes in Java? Error as well as Exception class is derived from Throwable class in Java.

Java String Interview Questions

String 1.What is the meaning of Immutable in the context of String class in Java? An Immutable object cannot be modified or changed in Java. String is an Immutable class in Java. Once a String object is created, it cannot be changed. When we assign the String to a new value, a new object is created. 2.Why a String object is considered immutable in java? Java language uses String for a variety of purposes. For this it has marked String Immutable. There is a concept of String literal in Java. Let say there are 2 String variables A and B that reference to a String object “TestData”. All these variables refer to same String literal. If one reference variable A changes the value of the String literal from “TestData” to “RealData”, then it will affect the other variable as well. Due to which String is considered Immutable. In this case, if one variable A changes the value to “RealData”, then a new String literal with “RealData” is created and A will point to new String li

Inner Classes Interview Questions in Java

Inner Classes 1.What is a Nested class? In Java, a Nested class is a class declared inside another class. We can have more than one class declared inside a file. 2. How many types of Nested classes are in Java? Java provides four types of Nested classes: 1.      Member inner class 2.      Local inner class 3.      Anonymous inner class 4.      Static nested class 3.Why do we use Nested Classes? There are following reasons for using nested classes: 1.      Logical Grouping : We can logically group classes in one place. If one class is useful to only one other class, then we put smaller class within the larger class and keep them in one file. This kind of nesting "helper classes" in a toplevel class makes the package more streamlined. 2.      Encapsulation : Nested classes increase encapsulation. Let say there are two top-level classes, Foo and Bar. Bar needs access to private members of Foo. We can hide class Bar within class Foo. In this

Garbage Collection Interview Questions in Java

Garbage Collection 1.What is Garbage Collection in Java? Java has an internal mechanism called Garbage collection to reclaim the memory of unused projects at run time. Garbage collection is also known as automatic memory management. 2. Why Java provides Garbage Collector?   Java, there are no pointers. Memory management and allocation is done by JVM. Since memory allocation is automated, after some time JVM may go low on memory. At that time, JVM has to free memory from unused objects. To help with the process of reclaiming memory, Java provides an automated process called Garbage Collector. 3.What is the purpose of gc() in Java? Java provides two methods System.gc() and Runtime.gc() to request the JVM to run the garbage collection. By using these methods, programmers can explicitly send request for Garbage Collection. But JVM process can reject this request and wait for some time before running the GC. 4.How does Garbage Collection work in Java? Java

Serialization and Deserialization Interview Questions in Java

Serialization and Deserialization 1. What is the serialization? Serialization is a process converting an object into a byte array. This byte array represents the class, version and internal state of the object. JVM can use this byte array to transmit/read the object over a network. 2. What is the purpose of serialization? Some of the uses of serialization are: 1.      Communication : It is used for transmitting an object over network between two machines. 2.      Persistence : We can store the object’s state in a database and retrieve it from database later on. 3.      Caching : Serialization can be used for caching to improve performance. We may need 10 minutes to build an object, but it may take just 10 seconds to de-serialize the object. 4.      Cross JVM Synchronization : It can be used in same way across multiple JVM that follow different architecture. 3. What is Deserialization? Deserialization is the process of reconstructing the object

Reflection Interview Questions in Java

Reflection 1. What is Reflection in Java? Reflection is Java language's ability to inspect and dynamically call classes, methods, attributes etc. at Runtime. It helps in examining or modifying the Runtime behavior of a class at Runtime. 2. What are the uses of Reflection in Java? Reflection is often used in Testing, Debugging and in Integrated Development Environment (IDE). Reflection allows you to write programs that do not have to "know" everything at compile time. It makes programs more dynamic, since they can be tied together at runtime. Many modern frameworks like Spring etc. use Reflection. Some modern languages like Python etc. also use Reflection. JAVA API for XML Parsing (JAXP) also uses Reflection. 3. How can we access private method of a class from outside the class? We can use Reflection to access private method of a class from outside the class. IN Java, we use getDeclaredMethod() to get instance of a private method. Then we

Java Package Interview Questions

Java Package 1. What is the purpose of package in Java? A package is used to encapsulate a group of classes, interfaces and sub-packages. Often, it is a hierarchical structure of storing information. It is easier to organize the related classes and subpackages in this manner. A Package also provides access protection for classes andinterfaces. A package also helps in removing naming collision. 2. What is java.lang package? In Java, java.lang package contains the classes that are fundamental to the design of Java programming language. The most important class in this package is Object class. It also contains wrapper classes like- Integer, Boolean, Character etc. It provides Math class for mathematical operations. 3. Which is the most important class in Java? It is an open-ended question with many answers. In my view, Object class is the most important class of Java programming language. It is the root of all the classes in Java. It provides some very im