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.
Which principle of Object-Oriented Programming involves hiding the internal details of objects using accessors like getters and setters?
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.
In Java, which keyword is used to enable one class to inherit fields and methods from another class?
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.
If a class has multiple methods with the same name but different parameters, which concept does this demonstrate?
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.
Which statement about interfaces and abstract classes in Java is correct?
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.
What is the main difference between '==' and '.equals()' when comparing objects in Java?
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.
What is the primary purpose of the Java Collections Framework?
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.
Which of the following is true about 'Set' in the Java Collections Framework?
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.
Which statement correctly describes the performance of ArrayList compared to LinkedList for accessing elements 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.
Which Java collection stores key-value pairs using hashing and allows one null key?
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.
What is a key difference between HashMap and Hashtable in Java?
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.
Which capability does ListIterator provide that Iterator does not?
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.
Which type of collection throws ConcurrentModificationException when modified during iteration?
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.
What is a key difference between an Array and an ArrayList in Java?
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.
In Java, what is a thread?
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.
Which is a valid way to create a thread in Java?
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.