Commonly Asked Java Interview Questions

What is JDBC?

JDBC stands for Java Database Connectivity. It is an API (Application Programming Interface) that provides a set of Java classes and methods for connecting and interacting with databases. JDBC allows Java programs to access and manipulate data stored in relational databases, such as MySQL, Oracle, and SQL Server. It provides a standard way for Java applications to communicate with databases, regardless of the specific database management system being used.

What does the static keyword mean?

In Java, the static keyword is used to define a class-level or static variable, method, or block. When a variable or method is declared as static, it belongs to the class itself rather than an instance of the class. This means that the variable or method can be accessed without creating an object of the class. Static variables are shared among all instances of the class, while static methods can be called directly using the class name.

Can you override private or static method in Java?

No, it is not possible to override a private or static method in Java. Private methods are only accessible within the same class and cannot be inherited by subclasses. Static methods, on the other hand, belong to the class itself and cannot be overridden by subclasses. However, it is possible to declare a method with the same name in a subclass, but it will not be considered as an overriding method.

What is the importance of the finally block in exception handling?

The finally block is an optional block that is used in conjunction with try-catch blocks for exception handling in Java. The finally block is executed regardless of whether an exception is thrown or caught. It is typically used to perform cleanup operations, such as closing database connections or releasing system resources, that should always be executed, regardless of whether an exception occurs or not.

What is the difference between an exception and an error in Java?

In Java, exceptions and errors are both subclasses of the Throwable class, but they represent different types of problems. Exceptions are exceptional conditions that can be caught and handled by the program. They are usually caused by user input, incorrect data, or unexpected conditions. Errors, on the other hand, are severe problems that are usually caused by the environment or the JVM itself. They are typically unrecoverable and can lead to the termination of the program.

When does an object become eligible for garbage collection in Java?

In Java, an object becomes eligible for garbage collection when it is no longer reachable or accessible by the program. This means that the object is not referenced by any variable or data structure in the program. The Java garbage collector automatically identifies and frees the memory occupied by objects that are no longer in use. However, it is important to note that the exact timing of garbage collection is determined by the JVM and cannot be controlled explicitly by the programmer.

What is an iterator?

An iterator is an object that allows the traversal and retrieval of elements from a collection in a sequential manner. It provides a uniform way to access elements from different types of collections, such as arrays, lists, and sets, without exposing their underlying implementation. The Iterator interface in Java provides methods like hasNext() to check if there are more elements, and next() to retrieve the next element in the collection. Iterators are commonly used in loops to iterate over the elements of a collection.

What are pass by reference and pass by value?

In Java, all method arguments are passed by value. When a primitive data type, such as int or boolean, is passed as an argument to a method, a copy of the value is passed. Any changes made to the parameter within the method do not affect the original value. When an object is passed as an argument, the reference to the object is passed by value. This means that a copy of the reference is passed, not the actual object. Any changes made to the object itself (e.g., modifying its properties) will be reflected outside the method, but reassigning the reference within the method will not affect the original reference.

How does HashMap work in Java?

HashMap is a class in Java that implements the Map interface and provides a way to store and retrieve key-value pairs. It uses an array of linked lists and a hash code to store and retrieve elements efficiently. When a key-value pair is added to a HashMap, the key’s hash code is used to calculate the index in the array where the element should be stored. If multiple elements have the same hash code, they are stored in a linked list at that index. When retrieving a value for a given key, the hash code is used again to locate the correct index and then the linked list is traversed to find the matching key-value pair. This allows for fast insertion, retrieval, and deletion of elements in a HashMap.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *