java
What is the difference between an abstract class and an interface in Java?
An abstract class is a class that cannot be instantiated and may contain both abstract (unimplemented) and concrete (implemented) methods. Abstract classes are used to provide a common structure and behavior for subclasses. An interface is a collection of abstract methods (and default methods, starting with Java 8) that can be implemented by any class. Interfaces define a contract that implementing classes must adhere to. A class can implement multiple interfaces, whereas it can only extend a single abstract class.
What are the main differences between ArrayList and LinkedList in Java?
ArrayList uses a dynamic array to store elements, while LinkedList uses a doubly-linked list data structure. ArrayList provides constant-time access to elements by index, whereas LinkedList requires linear-time traversal to access elements by index. LinkedList provides constant-time insertion and deletion at the beginning and end of the list, while ArrayList may require resizing and shifting of elements, which can take linear time. ArrayList typically uses less memory overhead than LinkedList,
Explain the difference between checked and unchecked exceptions in Java.
Checked exceptions are exceptions that must be either caught or declared in the method signature using the "throws" keyword. They are typically used to indicate recoverable error conditions, such as I/O errors or invalid user input. Examples of checked exceptions include IOException and SQLException. Unchecked exceptions are exceptions that do not need to be caught or declared in the method signature. They are usually caused by programming errors, such as null pointer dereferences, array index out of bounds, or illegal arguments. Examples of unchecked exceptions include NullPointerException, ArrayIndexOutOfBoundsException, and IllegalArgumentException.
What are the four pillars of Object-Oriented Programming (OOP)?
Encapsulation: The bundling of data (attributes) and methods (functions) that operate on the data within a single unit (class). Encapsulation promotes data hiding by making attributes private and providing public getter and setter methods. Inheritance: The mechanism of creating a new class (subclass) from an existing class (superclass) by inheriting its properties and methods. Inheritance promotes reusability and a hierarchical organization of classes. Polymorphism: The ability to take multiple forms. In OOP, polymorphism allows a single interface or method to represent different types or classes, enabling the same code to work with different objects. Abstraction: The process of hiding complex details and exposing only essential features of a class or an interface. Abstraction simplifies complex systems by breaking them down into smaller, more manageable components.
What is the difference between JDK, JRE, and JVM?
JDK (Java Development Kit) is a software development environment used for developing Java applications. It includes JRE, JVM, and other tools like the Java compiler (javac), Java debugger (jdb), and Java documentation generator (javadoc). JRE (Java Runtime Environment) is a software package that provides the minimum requirements to run Java applications. It includes JVM, Java core libraries, and other necessary components. JVM (Java Virtual Machine) is a virtual machine that interprets Java bytecode and executes Java programs. It provides a platform-independent way of running Java applications by abstracting the underlying hardware and operating system.
What are the main features of Java?
Object-oriented: Java follows OOP principles, making it easier to create modular and reusable code. Platform-independent: Java bytecode runs on any platform with a compatible JVM, allowing developers to write code once and run it anywhere. Multithreaded: Java supports multithreading, enabling the concurrent execution of multiple threads within a single program. Garbage-collected: Java manages memory automatically, freeing developers from manual memory management tasks. Robust: Java has a strong type-checking system, exception handling, and garbage collection, which helps create reliable and fault-tolerant applications. Secure: Java's sandbox security model and bytecode verification process help ensure the safety of executing Java applications.
Explain the differences between overloading and overriding in Java.
Overloading occurs when two or more methods in the same class have the same name but different parameters (different number or types of parameters). It is a form of static polymorphism or compile-time polymorphism. Overriding occurs when a subclass provides a new implementation for a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass. Overriding is a form of dynamic polymorphism or runtime polymorphism.
What are the access modifiers in Java?
Private: Accessible only within the same class. Default (package-private): Accessible within the same package (no keyword is used). Protected: Accessible within the same package and by subclasses in other packages. Public: Accessible from any class and package.
What are the non-access modifiers in Java?
Static: Indicates that a member (method or variable) belongs to the class rather than an instance of the class. Static members can be accessed without creating an instance of the class. Final: Indicates that a variable, method, or class cannot be modified further. A final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be subclassed. Abstract: Used with classes and methods to indicate that they cannot be instantiated or implemented, respectively. Abstract methods must be implemented by subclasses, and abstract classes must be extended to create objects. Synchronized: Indicates that a method can only be accessed by one thread at a time, ensuring thread safety in multithreaded applications. Transient: Indicates that a variable should not be serialized as part of an object's state when the object is serialized. Transient variables are skipped during the serialization process. Volatile: Indicates that a variable's value can be modified by multiple threads and ensures that the most recent value is read and written from main memory rather than a cached value in a specific thread.
What are the main differences between the String, StringBuffer, and StringBuilder classes in Java?
String: Strings are immutable, meaning their value cannot be changed once created. When a String object is modified, a new object is created instead. String objects are stored in the string constant pool, and multiple references to the same string literal will refer to the same object in the pool. StringBuffer: StringBuffer is mutable and synchronized, allowing for safe manipulation of character sequences in a multithreaded environment. However, the synchronization adds some overhead, making it slower than StringBuilder for single-threaded use cases. StringBuilder: StringBuilder is mutable and unsynchronized, allowing for efficient manipulation of character sequences in a single-threaded environment. It is recommended for most string manipulation tasks where thread safety is not a concern.
What is the difference between "==" and "equals()" in Java?
The "==" operator compares the reference values (memory addresses) of two objects, checking if they refer to the same object in memory. The "equals()" method is used to compare the content (state) of two objects, determining if they are logically equal based on their attributes. By default, the "equals()" method in the Object class performs reference comparison, but it can be overridden in subclasses to perform a custom content comparison.