1. What is Java?
Java is a high-level, object-oriented programming language that is designed to be portable and platform-independent. It was created by James Gosling at Sun Microsystems (now owned by Oracle) and was released in 1995.
2. What is the difference between a JDK and a JRE in Java?
A JDK (Java Development Kit) is a software development kit that includes tools and libraries necessary to develop Java applications. It includes a compiler, debugger, and other tools. A JRE (Java Runtime Environment), on the other hand, is a software package that provides a runtime environment for Java programs. It includes the Java Virtual Machine (JVM) and other necessary libraries.
3. What is the main difference between a String and a StringBuilder in Java?
A String is an immutable object in Java, which means that its value cannot be changed after it has been created. A StringBuilder, on the other hand, is a mutable object that can be modified. StringBuilder is more efficient than String when it comes to concatenating or modifying strings.
4. What is the difference between a static and a non-static method in Java?
A static method is a method that belongs to a class and can be called without creating an instance of the class. A non-static method, on the other hand, is a method that belongs to an instance of a class and can only be called on that instance.
5. What is the purpose of the "final" keyword in Java?
The "final" keyword is used in Java to make a variable, method, or class constant and unchangeable. When a variable is declared as final, its value cannot be modified. When a method is declared as final, it cannot be overridden by its subclasses. When a class is declared as final, it cannot be extended by any other class.
6. What is a constructor in Java?
A constructor is a special method that is called when an object of a class is created. It is used to initialize the instance variables of the object.
7. What is the difference between an interface and an abstract class in Java?
An interface is a contract that defines a set of methods that a class must implement. All methods in an interface are abstract, and the class that implements the interface must provide an implementation for all the methods. An abstract class, on the other hand, is a class that cannot be instantiated and is used as a base class for other classes. It may have abstract methods, which must be implemented by its subclasses.
8. What is the difference between method overloading and method overriding in Java?
Method overloading is a feature in Java that allows a class to have multiple methods with the same name but different parameters. Method overriding, on the other hand, occurs when a subclass provides a specific implementation of a method that is already defined in its parent class. Method overriding is used to implement polymorphism in Java.
9. What is the difference between a HashSet and a TreeSet in Java?
A HashSet is an unordered collection that does not allow duplicate elements. It is implemented using a hash table. A TreeSet is an ordered collection that does not allow duplicate elements. It is implemented using a red-black tree. The elements in a TreeSet are sorted in ascending order by default.
10. What is the difference between an ArrayList and a LinkedList in Java?
An ArrayList is a resizable array implementation that stores its elements in a contiguous block of memory. It is best used when you need to access elements randomly. A LinkedList, on the other hand, stores its elements as individual nodes with references to the previous and next nodes. It is best used when you need to manipulate elements frequently, such as inserting or deleting elements in the middle of the list.
11. What is the purpose of the "synchronized" keyword in Java?
The "synchronized" keyword is used in Java to provide thread safety by preventing multiple threads from accessing the same code block or method at the same time. When a method or code block is marked as synchronized, only one thread can access it at a time, ensuring that the code is executed in a thread-safe manner.
12. What is the difference between a private and a protected method in Java?
A private method in Java can only be accessed within the same class in which it is defined. A protected method, on the other hand, can be accessed by the same class and its subclasses. Protected methods are used to provide a level of encapsulation while still allowing subclasses to access certain methods.
13. What is the purpose of the "transient" keyword in Java?
The "transient" keyword is used in Java to indicate that a variable should not be serialized when an object is written to a file or transferred over a network. This is typically used for variables that do not need to be saved or sent, such as temporary variables or caches.
14. What is the difference between a checked and an unchecked exception in Java?
A checked exception is an exception that must be declared in a method or caught in a try-catch block. Examples of checked exceptions include IOException and ClassNotFoundException. An unchecked exception, on the other hand, is an exception that does not need to be declared or caught. Examples of unchecked exceptions include NullPointerException and ArrayIndexOutOfBoundsException.
15. What is the purpose of the "static" keyword in Java?
The "static" keyword is used in Java to define a variable, method, or inner class that belongs to the class itself, rather than to an instance of the class. A static variable is shared by all instances of a class, while a static method can be called without creating an instance of the class. A static inner class is a nested class that does not require an instance of the enclosing class to be created.
16. What is the difference between an abstract class and an interface in Java?
An abstract class is a class that cannot be instantiated and can only be subclassed. It may contain abstract methods that must be implemented by its subclasses. An interface, on the other hand, is a contract that defines a set of methods that a class must implement. Unlike abstract classes, an interface cannot contain any implementation code.
17. What is the purpose of the "volatile" keyword in Java?
The "volatile" keyword is used in Java to indicate that a variable may be modified by multiple threads, and that changes to the variable should be visible to all threads immediately. When a variable is marked as volatile, the JVM ensures that changes to the variable are immediately flushed to main memory, and that reads from the variable always read the latest value from main memory.
18. What is a lambda expression in Java?
A lambda expression is a way to define a block of code that can be passed as a parameter to a method or assigned to a variable. It is similar to an anonymous inner class, but with a more concise syntax. Lambda expressions are used primarily to implement functional interfaces, which are interfaces that contain only one abstract method.
19. What is the difference between a stream and a collection in Java?
A stream is a sequence of elements that can be processed in a pipeline of operations, such as filtering, mapping, and reducing. Streams are designed for processing large amounts of data efficiently, and can be parallelized to take advantage of multiple CPU cores. A collection, on the other hand, is a data structure that stores a group of elements, such as a list or a set. Collections are designed for efficient random access to elements, and can be used to store and manipulate small to moderate-sized data sets.
20. What is the difference between a stack and a queue in Java?
A stack is a data structure that follows the Last-In-First-Out (LIFO) principle, where the last element added to the stack is the first one to be removed. A queue, on the other hand, follows the First-In-First-Out (FIFO) principle, where the first element added to the queue is the first one to be removed. In Java, stacks and queues can be implemented using the Stack and LinkedList classes, respectively.