Most important Java interview questions with answer Part -6

Mayank Gupta
4 min readJul 8, 2023

--

What is the purpose of the transient keyword in Java?

The transient keyword is used to indicate that a field should not be serialized when an object is converted to a byte stream. This is typically done for sensitive or temporary data that does not need to be persisted. When an object is deserialized, transient fields are set to their default values (e.g., null for objects, 0 for integers).

What are the different types of inner classes in Java?

In Java, there are four types of inner classes:

  • Static nested class: A static class that is defined within another class and does not have access to the instance variables and methods of the outer class.
  • Non-static nested class (inner class): A non-static class that is defined within another class and has access to the instance variables and methods of the outer class.
  • Local class: A class that is defined within a method or a code block and has limited scope. It can access the variables of the enclosing block.
  • Anonymous class: A class that is defined without a name and is instantiated at the same time it is declared. It is typically used for one-time use cases, such as event handlers.

What is the purpose of the volatile keyword in Java?

The volatile keyword is used to indicate that a variable's value may be modified by multiple threads. It ensures that any changes to the variable are immediately visible to all threads. When a variable is marked as volatile, the compiler and the runtime system take extra precautions to handle memory visibility and synchronization.

What is the purpose of the finalize() method in Java?

The finalize() method is a special method in Java that is called by the garbage collector when an object is about to be reclaimed. It provides an opportunity for an object to release any resources it may hold before being garbage collected. However, it is generally recommended to use other mechanisms, such as try-finally or try-with-resources, to release resources explicitly rather than relying on the finalize() method.

What is the difference between composition and inheritance in Java?

Composition and inheritance are two ways to achieve code reuse and relationship between classes in Java. Composition is a “has-a” relationship where a class contains other objects as its members. It allows for greater flexibility and modularity as the composed objects can be easily swapped or modified. Inheritance, on the other hand, is an “is-a” relationship where a class inherits properties and methods from a superclass. It establishes an “is-a” relationship and promotes code reuse. While both have their uses, composition is generally preferred over inheritance as it provides more flexibility and avoids the limitations of single inheritance.

What are the benefits of using the Stream API in Java?

The Stream API introduced in Java 8 provides a functional programming approach to processing collections of data. It offers a set of high-level operations, such as filter, map, reduce, and collect, that can be combined to perform complex data transformations and aggregations. The benefits of using the Stream API include concise and expressive code, improved code readability, and the potential for better performance through parallel processing.

What is the difference between == and .equals() in Java?

The == operator in Java compares object references to check if they refer to the same memory location. It is used to compare primitive types or to check if two object references point to the same object. The .equals() method, on the other hand, is used to compare the contents of two objects for equality. It is typically overridden in classes to provide a meaningful comparison based on the object's attributes. The behavior of .equals() can be customized, while == always checks for reference equality.

What are lambda expressions in Java? Provide an example.

Lambda expressions are anonymous functions introduced in Java 8. They allow you to pass behavior as a method argument. Here’s an example:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach(n -> System.out.println(n));

What is the difference between ArrayList and LinkedList in Java?

ArrayList and LinkedList are both implementations of the List interface, but they have different underlying data structures. ArrayList is implemented as a resizable array, while LinkedList is implemented as a doubly-linked list. ArrayList provides fast access and retrieval of elements, whereas LinkedList provides efficient insertion and removal of elements.

What is the difference between the synchronized keyword and the volatile keyword in Java?

  • synchronized: It is used to provide mutual exclusion and thread synchronization. It ensures that only one thread can execute a synchronized block or method at a time.
  • volatile: It is used to declare a variable as "volatile," indicating that its value may be modified by multiple threads. It ensures that changes made by one thread are visible to other threads.

What is the Java Memory Model (JMM)?

The Java Memory Model defines the rules and guarantees for how threads interact with memory in a multi-threaded environment. It specifies the visibility of changes made by one thread to other threads and provides atomicity and ordering guarantees for shared variables.

For more articles consider making a follow on my account. Thanks…

--

--

Mayank Gupta

QA Automation Lead | Web Automation | Mobile Automation | API Automation l Performance | Web Security | IOT | Blockchain