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.
What does the volatile keyword ensure for a variable in Java?
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.
When should you use synchronization in Java?
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.
Which scenario best demonstrates the use of enum in Java?
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.
What does serialization enable in Java?
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.
If a Student object has normal, static, and transient fields, which fields get serialized?
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.
Why is serialVersionUID important in serialization?
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.
What types of keys and values does a Java HashMap accept?
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.
What does marking a field as transient do in Java?
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.
How many null keys are allowed in a Java HashMap?
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.
Why might you use Map.Entry when iterating over a HashMap?
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.
Is Java's HashMap 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.
Which interface must a Java class implement to allow serialization?
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.
Which of the following methods is available on all Java Enum types?
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.
What is the ordering of keys in a standard Java HashMap?
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.
What should the access level of a no-argument constructor be for successful deserialization in Java?
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.