Java Core Concepts Audio Quiz Quiz

  1. Differences Between List, Set, and Map

    In Java, what is the main difference between a List, a Set, and a Map?

    1. Lists maintain order and allow duplicates; Sets do not allow duplicates; Maps store key-value pairs and do not allow duplicate keys.
    2. Lists do not allow duplicates; Sets maintain insertion order; Maps are unordered and allow duplicates.
    3. Lists and Sets both allow duplicates; Maps do not store any values.
    4. Lists are always sorted; Sets are not sorted; Maps must have numeric keys.
    5. Lists store pairs of values; Sets store values in sequence; Maps do not have keys.
  2. Abstract Class vs Interface

    Which statement correctly describes the main difference between an abstract class and an interface in Java?

    1. Abstract classes can have both concrete and abstract methods, while interfaces can only have abstract methods.
    2. Abstract classes can only have abstract methods; interfaces can only have concrete methods.
    3. Only interfaces support multiple inheritance, while abstract classes do not allow inheritance.
    4. An abstract class can be instantiated, but an interface cannot.
    5. Interfaces cannot define any constants, while abstract classes can.
  3. Concurrency: notify vs notifyAll

    In Java concurrency, what is the key difference between the notify and notifyAll methods?

    1. notify wakes up one waiting thread, while notifyAll wakes up all waiting threads on the object's monitor.
    2. notify is used only for synchronized collections; notifyAll is for concurrent collections.
    3. notify puts the thread to sleep; notifyAll interrupts all threads.
    4. notify wakes up all threads; notifyAll wakes up no threads.
    5. notify releases all locks; notifyAll does not release any lock.
  4. HashMap get() Method

    When you call the get method on a HashMap in Java, how does it retrieve the value associated with a key?

    1. It calculates the key's hash code, finds the bucket index, and searches for the key at that location.
    2. It checks all values sequentially until it finds the correct one.
    3. It searches from the end of the array backwards.
    4. It sorts the keys and uses binary search.
    5. It always throws an exception if the key does not exist.
  5. Deadlock and Its Prevention

    What is a deadlock in Java multithreading, and which strategy can help prevent it?

    1. A deadlock occurs when threads wait for each other’s resources; consistent resource ordering can help prevent it.
    2. A deadlock is when a thread sleeps too long; using interrupt can prevent it.
    3. A deadlock happens if a thread does not release a lock; using atomic variables avoids it.
    4. A deadlock means all threads finish execution; thread pools prevent it.
    5. A deadlock occurs only with static variables; using final prevents it.
  6. Composition vs Aggregation

    What best distinguishes composition from aggregation in Java object relationships?

    1. In composition, the contained object's lifetime is bound to the container object, while in aggregation it is independent.
    2. Aggregation forms a stronger bond than composition; composition is the weakest relationship.
    3. Composition allows multiple inheritance, aggregation does not.
    4. Aggregation and composition are identical; both imply ownership.
    5. Composition requires interfaces, aggregation requires classes.
  7. Producer-Consumer Problem

    Which Java utility or concept is commonly used to implement a producer-consumer pattern?

    1. BlockingQueue from java.util.concurrent
    2. ConcurrentList
    3. HashSet
    4. Scanner
    5. AtomicLong
  8. Preventing SQL Injection

    What is a best practice to prevent SQL injection attacks when working with JDBC in Java?

    1. Use parameterized queries, such as PreparedStatement, for all user input.
    2. Concatenate user input directly into the SQL query.
    3. Avoid using WHERE clauses in queries.
    4. Rely on client-side validation only.
    5. Create queries using only stored procedures without parameters.
  9. Liskov Substitution Principle

    According to the Liskov Substitution Principle in object-oriented programming, which statement is correct?

    1. Objects of a superclass should be replaceable with objects of its subclasses without affecting program correctness.
    2. A subclass must not override any methods of its superclass.
    3. Subclasses should be unrelated to their superclasses.
    4. A superclass cannot be abstract under this principle.
    5. All subclasses must have exactly the same attributes as the superclass.
  10. Ordered vs Sorted Collections

    What is the main difference between an ordered collection and a sorted collection in Java?

    1. An ordered collection maintains insertion order, while a sorted collection maintains elements in a specific order according to a comparator or natural ordering.
    2. Ordered collections never allow duplicates; sorted collections do.
    3. Sorted collections change order during iteration; ordered collections do not.
    4. An ordered collection sorts elements alphabetically by default.
    5. Sorted collections never allow nulls, ordered collections always do.