Differences Between List, Set, and Map
In Java, what is the main difference between a List, a Set, and a Map?
- Lists maintain order and allow duplicates; Sets do not allow duplicates; Maps store key-value pairs and do not allow duplicate keys.
- Lists do not allow duplicates; Sets maintain insertion order; Maps are unordered and allow duplicates.
- Lists and Sets both allow duplicates; Maps do not store any values.
- Lists are always sorted; Sets are not sorted; Maps must have numeric keys.
- Lists store pairs of values; Sets store values in sequence; Maps do not have keys.
Abstract Class vs Interface
Which statement correctly describes the main difference between an abstract class and an interface in Java?
- Abstract classes can have both concrete and abstract methods, while interfaces can only have abstract methods.
- Abstract classes can only have abstract methods; interfaces can only have concrete methods.
- Only interfaces support multiple inheritance, while abstract classes do not allow inheritance.
- An abstract class can be instantiated, but an interface cannot.
- Interfaces cannot define any constants, while abstract classes can.
Concurrency: notify vs notifyAll
In Java concurrency, what is the key difference between the notify and notifyAll methods?
- notify wakes up one waiting thread, while notifyAll wakes up all waiting threads on the object's monitor.
- notify is used only for synchronized collections; notifyAll is for concurrent collections.
- notify puts the thread to sleep; notifyAll interrupts all threads.
- notify wakes up all threads; notifyAll wakes up no threads.
- notify releases all locks; notifyAll does not release any lock.
HashMap get() Method
When you call the get method on a HashMap in Java, how does it retrieve the value associated with a key?
- It calculates the key's hash code, finds the bucket index, and searches for the key at that location.
- It checks all values sequentially until it finds the correct one.
- It searches from the end of the array backwards.
- It sorts the keys and uses binary search.
- It always throws an exception if the key does not exist.
Deadlock and Its Prevention
What is a deadlock in Java multithreading, and which strategy can help prevent it?
- A deadlock occurs when threads wait for each other’s resources; consistent resource ordering can help prevent it.
- A deadlock is when a thread sleeps too long; using interrupt can prevent it.
- A deadlock happens if a thread does not release a lock; using atomic variables avoids it.
- A deadlock means all threads finish execution; thread pools prevent it.
- A deadlock occurs only with static variables; using final prevents it.
Composition vs Aggregation
What best distinguishes composition from aggregation in Java object relationships?
- In composition, the contained object's lifetime is bound to the container object, while in aggregation it is independent.
- Aggregation forms a stronger bond than composition; composition is the weakest relationship.
- Composition allows multiple inheritance, aggregation does not.
- Aggregation and composition are identical; both imply ownership.
- Composition requires interfaces, aggregation requires classes.
Producer-Consumer Problem
Which Java utility or concept is commonly used to implement a producer-consumer pattern?
- BlockingQueue from java.util.concurrent
- ConcurrentList
- HashSet
- Scanner
- AtomicLong
Preventing SQL Injection
What is a best practice to prevent SQL injection attacks when working with JDBC in Java?
- Use parameterized queries, such as PreparedStatement, for all user input.
- Concatenate user input directly into the SQL query.
- Avoid using WHERE clauses in queries.
- Rely on client-side validation only.
- Create queries using only stored procedures without parameters.
Liskov Substitution Principle
According to the Liskov Substitution Principle in object-oriented programming, which statement is correct?
- Objects of a superclass should be replaceable with objects of its subclasses without affecting program correctness.
- A subclass must not override any methods of its superclass.
- Subclasses should be unrelated to their superclasses.
- A superclass cannot be abstract under this principle.
- All subclasses must have exactly the same attributes as the superclass.
Ordered vs Sorted Collections
What is the main difference between an ordered collection and a sorted collection in Java?
- An ordered collection maintains insertion order, while a sorted collection maintains elements in a specific order according to a comparator or natural ordering.
- Ordered collections never allow duplicates; sorted collections do.
- Sorted collections change order during iteration; ordered collections do not.
- An ordered collection sorts elements alphabetically by default.
- Sorted collections never allow nulls, ordered collections always do.