Â
What is the difference between abstract class and interface in Java?
A: An abstract class can have instance variables, and instance methods (including an abstract method), and it can provide a method body. An interface cannot have instance variables and its methods are implicitly abstract. It can only declare method signatures and fields.
What is the difference between HashMap and Hashtable in Java?
A: HashMap is non-synchronized and permits null values, while Hashtable is synchronized and does not allow null values.
Can you explain the difference between ArrayList and LinkedList?
A: ArrayList is backed by an array and has better performance when accessing elements randomly, while LinkedList has better performance when adding/removing elements in the middle.
What is the difference between final, finally and finalize in Java?
A: final is a keyword that can be used to declare a constant or a class that cannot be subclassed. finally is a block used with try-catch to provide code that will always be executed, whether or not an exception is thrown. finalize is a method used for garbage collection.
Can you explain the Singleton design pattern in Java?
A: Singleton is a design pattern that ensures that a class has only one instance, while providing a global point of access to this instance.
What is the difference between == and .equals() in Java?
A: == compares object references, while .equals() compares the contents of two objects.
What is the difference between throw and throws in Java?
A: throw is used to throw an exception explicitly, while throws is used to declare an exception that may be thrown by a method.
Can you explain the difference between a Stack and a Queue in Java?
A: Stack implements last-in-first-out (LIFO), where the most recent item added to the stack will be the first one to be removed. Queue implements first-in-first-out (FIFO), where the first item added to the queue will be the first one to be removed.
What is the difference between public, protected, default, and private access modifiers in Java?
A: public: accessible from any class, protected: accessible within the same class and its subclasses, default (no keyword): accessible within the same package, private: accessible only within the same class.
Can you explain the difference between HashSet and TreeSet in Java?
A: HashSet is an unsorted, unordered set stored in a hash table structure for fast access, while TreeSet is a sorted, ordered set stored in a tree structure.
What is the use of the synchronized keyword in Java?
A: The synchronized keyword is used to control access to a shared resource in a multithreaded environment, ensuring that only one thread can access the resource at a time.
Can you explain the use of the wait() and notify() methods in Java?
A: The wait() method is used to release a lock and wait for notification of a change in the resource, while the notify() method is used to send a notification that a change has occurred. Both methods are used in synchronization to coordinate access to shared resources.
Can you explain the difference between an Iterator and a ListIterator in Java?
A: An Iterator is used to traverse a collection of objects and remove elements in a forward direction, while a ListIterator allows bi-directional iteration and allows elements to be added or removed from a list.
Can you explain the use of the Assert keyword in Java?
A: The assert keyword is used to check for conditions during testing and debugging, and it will cause an AssertionError if the condition is false.
What is the difference between the Scanner and BufferedReader classes in Java?
A: The Scanner class is used for simple text scanning and parsing operations, while the BufferedReader class is used for more efficient reading of text from a character-input stream.
Can you explain the use of the finally block in a try-catch-finally statement in Java?
A: The finally block is used to ensure that certain code is executed regardless of whether an exception is thrown or not, and it is typically used to release resources acquired in a try block.
Can you explain the difference between a HashMap and a TreeMap in Java?
A: HashMap is an unsorted, unordered map stored in a hash table structure for fast access, while TreeMap is a sorted, ordered map stored in a tree structure.
Can you explain the use of the volatile keyword in Java?
A: The volatile keyword is used to indicate that a variable may be modified asynchronously by multiple threads, and it ensures that any thread accessing the variable will see the latest value written by any other thread.
What is the difference between a constructor and a method in Java?
A: A constructor is used to create an object and initialize its state, while a method is used to perform an action on an object.
Can you explain the use of the this keyword in Java?
A: The this keyword is used to refer to the current instance of a class, and it is often used to refer to instance variables when a local variable has the same name.
What is the difference between a break and a continue statement in Java?
A: A break statement is used to exit a loop, while a continue statement is used to skip the current iteration of a loop and continue with the next iteration.
Can you explain the use of the Abstract keyword in Java?
A: The abstract keyword is used to declare an abstract class or method, meaning it cannot be instantiated and must be overridden by a subclass.
What is the difference between a Class and an Interface in Java?
A: A class is a blueprint for creating objects, while an interface defines a set of methods that a class must implement.
Post a Comment