Java Backend Interview Essentials: 2025 Cognizant Questions Quiz

Test your understanding of important Java backend concepts with this quiz based on real Cognizant interview questions from 2025. Assess your core Java, serialization, HashMap, and collection knowledge to prepare effectively for your next technical interview.

  1. Volatile Keyword

    What does the volatile keyword ensure for a variable in Java?

    1. Its value is always read from main memory by all threads.
    2. It is automatically serialized.
    3. It can only be accessed within the class it is defined.
    4. Its value cannot change after initialization.

    Explanation: The volatile keyword in Java ensures that any thread accessing the variable always reads its value from main memory, never from a cached copy. This is important for making updates to variables visible across threads in concurrent environments. The statement about being accessed only within the class describes the private modifier, not volatile. Making a value unchangeable after initialization is the function of the final keyword. Volatile does not directly relate to serialization.

  2. Synchronization Purpose

    When should you use synchronization in Java?

    1. To convert checked exceptions into unchecked exceptions.
    2. To ensure that only one thread accesses a shared resource at a time.
    3. To protect variables from garbage collection.
    4. To automatically sort a List in reverse order.

    Explanation: Synchronization controls access to shared resources by multiple threads, preventing concurrent modifications that could lead to inconsistent states. Sorting a List or exception conversion has nothing to do with synchronization. Synchronization also does not prevent garbage collection; instead, it deals with thread safety.

  3. Enum Use Case

    Which scenario best demonstrates the use of enum in Java?

    1. Storing user input data in a text file.
    2. Creating a utility class for file operations.
    3. Representing the four seasons: SPRING, SUMMER, AUTUMN, WINTER.
    4. Encrypting sensitive strings at runtime.

    Explanation: Enums are ideal for representing a fixed set of constants, such as the four seasons. Enums are not designed for storing dynamic data or performing file operations. Encryption and utility classes are unrelated to the enum concept, which focuses on grouping unchangeable, related values.

  4. Serialization Concept

    What does serialization enable in Java?

    1. Converting an object's state into a byte stream.
    2. Encrypting variables for security.
    3. Connecting to a database using JDBC.
    4. Sorting a collection in natural order.

    Explanation: Serialization in Java allows an object's state to be represented as a byte stream, making it suitable for storage or network transmission. Sorting collections, database connections, and encryption are not directly related to serialization, which is about object persistence and data transfer.

  5. Serialization of Fields

    If a Student object has normal, static, and transient fields, which fields get serialized?

    1. Static and transient fields.
    2. All fields are always serialized.
    3. Normal and static fields.
    4. Only normal fields.

    Explanation: During serialization, only the non-static, non-transient (normal) fields are saved. Static fields belong to the class, not the instance, and thus are not serialized. Transient fields are explicitly marked to be skipped. The other options incorrectly state the inclusion of static or transient fields or claim all are serialized, which is not accurate.

  6. serialVersionUID Purpose

    Why is serialVersionUID important in serialization?

    1. It ensures serialized objects are compatible during deserialization.
    2. It determines the sorting order in a collection.
    3. It identifies the thread that created an object.
    4. It prevents static fields from being serialized.

    Explanation: serialVersionUID is used to verify compatibility between the serialized form of a class and its current version during deserialization. Sorting order, thread identification, and static field exclusion are unrelated; static fields are naturally not serialized regardless of serialVersionUID.

  7. HashMap Keys and Values

    What types of keys and values does a Java HashMap accept?

    1. Only primitive types like int or boolean.
    2. Only String type keys and integer values.
    3. Only serializable objects.
    4. Any objects, including null.

    Explanation: A HashMap in Java can store any type of object as keys and values, including null for either. It does not restrict to primitive types or only Strings and integers. While serializable objects can be used, it's not a requirement unless serialization is needed.

  8. Transient Keyword

    What does marking a field as transient do in Java?

    1. The field is automatically synchronized.
    2. The field is skipped during object serialization.
    3. The field is read-only in all classes.
    4. The field cannot be modified after initialization.

    Explanation: A transient field is not included in the serialized representation of an object, meaning its data is not saved when storing or transferring objects. Marking something as final would make it unchangeable after initialization, not transient. Synchronization is a different concept, and transient has nothing to do with read-only restrictions.

  9. HashMap Null Keys

    How many null keys are allowed in a Java HashMap?

    1. Exactly one.
    2. As many as the map's capacity.
    3. It depends on the HashMap's load factor.
    4. None at all.

    Explanation: A HashMap allows exactly one null key; any subsequent insertions with a null key replace the previous entry. It is incorrect to say none or as many as capacity, and the load factor affects resizing, not key constraints.

  10. Map.Entry Usage

    Why might you use Map.Entry when iterating over a HashMap?

    1. To automatically sort the HashMap by keys.
    2. To only retrieve values efficiently.
    3. To make all HashMap entries immutable.
    4. To access both keys and values conveniently during iteration.

    Explanation: Map.Entry provides a way to access both the key and the value for each map entry while iterating. It's not specifically for efficiency in retrieving values only, nor does it sort the map or make entries immutable automatically.

  11. HashMap Thread Safety

    Is Java's HashMap thread-safe by default?

    1. Yes, because all operations are synchronized.
    2. Yes, but only if used with String keys.
    3. It depends on the size of the map.
    4. No, it is not thread-safe by default.

    Explanation: HashMap is not designed to be thread-safe; concurrent modifications can cause inconsistent behavior. Option two is false, as HashMap operations are not synchronized. The type of keys or map size does not affect thread safety.

  12. Serialization Interface

    Which interface must a Java class implement to allow serialization?

    1. Serializable
    2. Runnable
    3. Cloneable
    4. Comparable

    Explanation: Implementing the Serializable interface enables Java objects to be serialized. Runnable, Cloneable, and Comparable provide different functionalities relating to threading, cloning, or ordering—not serialization.

  13. Enum Methods

    Which of the following methods is available on all Java Enum types?

    1. toCharArray()
    2. parseInt()
    3. values()
    4. getLength()

    Explanation: The values() method returns an array of all enum constants, and is automatically created for all enums. getLength() is not a standard method. toCharArray() is used with strings, and parseInt() is related to number parsing, not enums.

  14. HashMap Ordering

    What is the ordering of keys in a standard Java HashMap?

    1. Keys are reversed alphabetically.
    2. Insertion order is preserved.
    3. No guaranteed order.
    4. Keys are always sorted.

    Explanation: A HashMap does not guarantee any specific order for its keys. If you need sorted or insertion order, you would use a different map implementation. Sorted or reversed orders are not features of HashMap.

  15. Access Modifier for Serialization

    What should the access level of a no-argument constructor be for successful deserialization in Java?

    1. It is required to be private.
    2. It can be default or protected.
    3. It must be abstract.
    4. It must always be public.

    Explanation: For deserialization, Java can use a default or protected no-arg constructor, not necessarily public. A private constructor would prevent instantiation except in special cases. An abstract constructor does not exist in Java.