Java Basics: OOP Principles, Collections, and Threads Quiz Quiz

Test your understanding of Java's core Object-Oriented Programming concepts, Collections Framework, and Multithreading basics with this easy-level quiz. Ideal for interview preparation, it covers essential Java topics with clear, accessible questions.

  1. OOP Principles

    Which principle of Object-Oriented Programming involves hiding the internal details of objects using accessors like getters and setters?

    1. Encapsulation
    2. Instantiation
    3. Composition
    4. Polymorphism

    Explanation: Encapsulation is about bundling data and methods together while hiding internal details from outside access, commonly using getters and setters. Polymorphism allows objects to take many forms, which is unrelated to access control. Composition means building classes by combining objects, and instantiation simply refers to creating an object, not controlling data visibility.

  2. OOP Inheritance

    In Java, which keyword is used to enable one class to inherit fields and methods from another class?

    1. extends
    2. inherits
    3. implements
    4. interface

    Explanation: The 'extends' keyword is used for establishing inheritance between classes in Java. 'implements' is for interfaces, not classes. 'inherits' is not a valid Java keyword, and 'interface' declares a new interface rather than supporting inheritance.

  3. Polymorphism Example

    If a class has multiple methods with the same name but different parameters, which concept does this demonstrate?

    1. Method Overriding
    2. Encapsulation
    3. Immutability
    4. Method Overloading

    Explanation: Method overloading refers to having multiple methods with the same name but different parameter lists in the same class. Method overriding involves redefining methods in a subclass, while encapsulation hides data implementation. Immutability relates to objects whose state cannot change.

  4. Abstract Class vs Interface

    Which statement about interfaces and abstract classes in Java is correct?

    1. Both interface and abstract class cannot have any methods.
    2. A class can implement multiple interfaces but only extend one abstract class.
    3. Interfaces can have instance variables by default.
    4. A class can extend multiple abstract classes but only implement one interface.

    Explanation: Java allows multiple interface implementations, promoting flexibility, but restricts classes to single inheritance from abstract classes. Extending multiple abstract classes is not permitted. Both constructs can declare methods; interfaces cannot have instance variables by default.

  5. Equals vs == Operator

    What is the main difference between '==' and '.equals()' when comparing objects in Java?

    1. Both perform the same comparison.
    2. '.equals()' checks reference equality, while '==' checks value equality.
    3. '==' checks reference equality, while '.equals()' checks value equality.
    4. '==' works only with strings.

    Explanation: '==' determines if two references denote the same object, while '.equals()' checks logical content equality. They do not switch roles. They are not equivalent, and '==' works on references for all objects, not only strings.

  6. Collections Framework Purpose

    What is the primary purpose of the Java Collections Framework?

    1. To generate user interface windows
    2. To store and manipulate groups of data in various structures
    3. To access files on the filesystem
    4. To perform mathematical calculations

    Explanation: The Collections Framework is designed for efficient data storage and manipulation using structures like lists, sets, and maps. It is not focused on calculations, user interfaces, or file access. These other tasks are handled by different APIs.

  7. List, Set, Map Difference

    Which of the following is true about 'Set' in the Java Collections Framework?

    1. It maps keys to values.
    2. It maintains elements in insertion order.
    3. It does not allow duplicate elements.
    4. It stores data strictly in a sorted format.

    Explanation: Sets ensure there are no duplicate entries. Lists can maintain insertion order and allow duplicates. Maps pair keys to values. Not all sets are sorted; only specific types like TreeSet maintain order.

  8. ArrayList vs LinkedList

    Which statement correctly describes the performance of ArrayList compared to LinkedList for accessing elements by index?

    1. LinkedList is optimized for indexing.
    2. LinkedList is always faster for access by index.
    3. Both are equally fast for index access.
    4. ArrayList is faster for random access by index.

    Explanation: ArrayList provides efficient index-based access due to its underlying array structure. LinkedList requires traversal from the start or end for each index, making access slower. They do not offer equal performance for this operation, and LinkedList is not optimized for indexing.

  9. Collections: HashMap

    Which Java collection stores key-value pairs using hashing and allows one null key?

    1. HashTable
    2. ArrayList
    3. TreeSet
    4. HashMap

    Explanation: HashMap supports key-value pairs, allows one null key, and uses hashing for access. TreeSet stores unique elements, ArrayList holds ordered objects, and 'HashTable' (properly spelled 'Hashtable') does not allow null keys.

  10. HashMap vs Hashtable

    What is a key difference between HashMap and Hashtable in Java?

    1. Hashtable allows multiple null keys.
    2. Hashtable is faster than HashMap for all operations.
    3. HashMap is always synchronized.
    4. HashMap allows null keys, while Hashtable does not.

    Explanation: HashMap is not synchronized and permits one null key and values. Hashtable is synchronized and prohibits null keys and values. Hashtable is not necessarily faster—this depends on thread safety mechanisms. HashMap is not always synchronized.

  11. Comparing ListIterator

    Which capability does ListIterator provide that Iterator does not?

    1. Ability to edit the class file
    2. Removing objects in a set
    3. Bidirectional traversal (forward and backward)
    4. Automatic sorting of lists

    Explanation: ListIterator allows moving both forwards and backwards through a list. Iterator only moves forward. Neither allows class file editing or automatic sorting, and removing items from a set is unrelated to iterators.

  12. Fail-fast vs Fail-safe

    Which type of collection throws ConcurrentModificationException when modified during iteration?

    1. Compile-time
    2. Fail-fast
    3. Thread-safe
    4. Fail-safe

    Explanation: Fail-fast collections detect structural changes during iteration and throw ConcurrentModificationException. Fail-safe collections avoid exceptions by working on a copy. Thread-safety relates to concurrent access management, and 'compile-time' is unrelated.

  13. Array vs ArrayList

    What is a key difference between an Array and an ArrayList in Java?

    1. Array has a fixed size, while ArrayList is dynamic.
    2. Both can only store primitive types.
    3. Array size can change after creation.
    4. ArrayList can store primitive types directly.

    Explanation: Arrays are initialized with a fixed length, while ArrayLists can grow or shrink as needed. ArrayLists cannot directly store primitive types; they store objects. Array sizes are immutable after creation, and both are not restricted to primitives.

  14. Thread Basics

    In Java, what is a thread?

    1. A data structure for storing keys and values
    2. A static variable
    3. A lightweight subprocess for concurrent execution
    4. A subclass of String

    Explanation: A thread enables concurrent execution within a program. Static variables are specific data fields, not execution entities. Data structures for storage and String subclasses do not represent threads.

  15. Creating Threads

    Which is a valid way to create a thread in Java?

    1. By implementing the Runnable interface
    2. By instantiating a Comparator
    3. By extending the Object class
    4. By importing the ThreadUtils class

    Explanation: Implementing Runnable or extending Thread enables thread creation. ThreadUtils and Comparator are unrelated; Object class does not support threading directly. Only the Runnable interface and Thread class are used for thread construction.