Advance Java Interview Questions – Excellence Technology

Advance Java
Interview Questions

Advance Java Interview Questions

  • Stack Memory: Used for storing local variables and function call information. Memory allocation and deallocation are fast, and it follows a last-in, first-out (LIFO) structure.
  • Heap Memory: Used for dynamic memory allocation, such as objects and instances. Memory management is more complex, and objects in the heap can have varying lifetimes.

Garbage collection is the automatic process of reclaiming memory occupied by objects that are no longer reachable or referenced by the program. The Java Virtual Machine (JVM) identifies unreferenced objects and frees up their memory. Java employs different garbage collection algorithms, such as the generational garbage collector, to efficiently manage memory.

  • synchronized: Ensures mutual exclusion by preventing multiple threads from executing a block of code simultaneously. It can be applied to methods or code blocks.
  • volatile: Guarantees visibility of changes across threads. When a variable is declared as volatile, any thread reading the variable sees the most recent modification made by any other thread.

The Java Memory Model defines the rules and semantics for how threads interact with memory in a multithreaded environment. It ensures that the changes made by one thread to shared variables are visible to other threads. The JMM provides guarantees for the ordering and visibility of reads and writes performed by multiple threads.

The Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. In Java, the Observer interface and Observable class can be used for implementation. However, it's worth noting that these classes are deprecated in newer versions of Java, and alternatives like the java.util.concurrent.Flow package or third-party libraries are recommended.

  • FileInputStream: Reads bytes from a file. It is not buffered, meaning each read operation involves disk access.
  • BufferedInputStream: Wraps around a FileInputStream to provide buffering. It reads data from the file into an internal buffer, making read operations more efficient by minimizing disk access.

Serialization is the process of converting an object into a byte stream, which can be persisted to a file, sent over a network, or stored in a database. In Java, the java.io.Serializable interface is used to mark classes as serializable. Object serialization is performed using ObjectOutputStream for writing objects and ObjectInputStream for reading objects.

Wildcards in Java Generics allow for more flexibility when working with generic types. There are two types of wildcards:

  • Upper-bounded Wildcard (<?>): Represents an unknown type that is a subtype of the specified bound.
  • Lower-bounded Wildcard (<? super T>): Represents an unknown type that is a supertype of the specified bound.

Type erasure is a feature in Java Generics where type information is erased at compile-time, and generic types are replaced with their bounding types or the Object type. This is done to maintain compatibility with code written in older versions of Java that do not support generics. Type erasure ensures that generics do not affect the runtime performance of Java programs.

Java Reflection allows for the examination and manipulation of class metadata at runtime. It provides a way to inspect classes, interfaces, fields, methods, and other elements, as well as instantiate objects and invoke methods dynamically. Reflection is commonly used in scenarios such as creating frameworks, serialization, and dependency injection.

The java.lang.reflect package provides classes and interfaces that support Java Reflection. Some key classes in this package include:

  • Class: Represents classes and interfaces at runtime.
  • Field: Represents fields (variables) in a class.
  • Method: Represents methods in a class.
  • Constructor: Represents constructors in a class.
  • Checked Exceptions: Checked exceptions are checked at compile-time, and the compiler ensures that they are either caught using a try...catch block or declared using the throws clause in the method signature. Examples include IOException and SQLException.
  • Unchecked Exceptions (Runtime Exceptions): Unchecked exceptions are not checked at compile-time. They typically result from programming errors and extend RuntimeException or its subclasses. Examples include NullPointerException and ArrayIndexOutOfBoundsException.

Custom exceptions, also known as user-defined exceptions, allow developers to create their own exception classes to represent specific error conditions in their applications. Custom exceptions must extend either Exception or one of its subclasses. They are useful for improving code readability and handling application-specific errors.

JDBC (Java Database Connectivity) is a Java API that allows Java applications to interact with databases. It provides a standard interface for connecting to relational databases, executing SQL queries, and processing result sets. JDBC consists of the java.sql package, which includes classes and interfaces for database access.

  1. Load the JDBC driver: Use Class.forName("com.mysql.jdbc.Driver") to load the appropriate JDBC driver.
  2. Establish a connection: Use DriverManager.getConnection(url, username, password) to establish a connection to the database.
  3. Create a statement: Create a Statement or PreparedStatement object to execute SQL queries.
  4. Execute queries: Use the statement object to execute SQL queries.
  5. Process the result: Retrieve and process the result set obtained from the executed query.
  6. Close resources: Close the connection, statement, and result set to release resources.

Still have questions? Contact us We’d be Happy to help




    CAN'T FIND ANSWER? ASK US DIRECTLY!