50 multiple-choice questions with answers and detailed explanations. Untimed — choose an answer to check it instantly.
0/50 answered0 correct
Question 1 of 50
What is inheritance used to achieve in object-oriented design?
Correct answer: A) Reusing and extending behavior defined in a parent class
Explanation: Inheritance lets a subclass reuse fields and methods from a superclass while adding or specializing behavior, reducing duplication across related types.
Question 2 of 50
What is an 'is-a' relationship?
Correct answer: B) The relationship expressed by inheritance between subclass and superclass
Explanation: Inheritance models an is-a relationship: a Dog is an Animal. Composition models has-a relationships, such as a Car has an Engine.
Question 3 of 50
When should composition be used instead of inheritance?
Correct answer: C) When the relationship is has-a rather than is-a
Explanation: Composition suits has-a relationships and generally produces more flexible designs. Inheritance should be reserved for genuine is-a specialization.
Question 4 of 50
Can a subclass access private members of its superclass directly?
Correct answer: D) No, private members are not inherited into subclass scope
Explanation: Private members are not accessible in subclasses. Protected or public accessor methods must be used to reach that state.
Question 5 of 50
What happens when a subclass overrides a method and calls super.method()?
Correct answer: A) The superclass implementation executes
Explanation: Calling super.method() invokes the superclass implementation, letting a subclass extend rather than entirely replace the inherited behavior.
Question 6 of 50
Can a final method be overridden in a subclass?
Correct answer: B) No, final prevents overriding
Explanation: Declaring a method final prevents subclasses from overriding it, used to guarantee that critical behavior remains unchanged.
Question 7 of 50
What is an abstract method?
Correct answer: C) A method declared without a body that subclasses must implement
Explanation: An abstract method declares a signature with no implementation. Any concrete subclass must provide an implementation before it can be instantiated.
Question 8 of 50
Can a class containing an abstract method be instantiated?
Correct answer: D) No, the class must be declared abstract and cannot be instantiated
Explanation: A class with any abstract method must itself be abstract, and abstract classes cannot be instantiated directly.
Question 9 of 50
What is the benefit of declaring a variable using a superclass type?
Correct answer: A) It can hold any subclass instance, enabling polymorphic code
Explanation: A superclass-typed reference can hold any subclass object, so code written against the general type works with all current and future subclasses.
Question 10 of 50
In polymorphism, which method implementation executes at runtime?
Correct answer: B) The one belonging to the object's actual runtime type
Explanation: Java resolves overridden method calls using the object's actual runtime type, not the declared type of the reference variable.
Question 11 of 50
What is the Object class method used to compare logical equality?
Correct answer: C) equals()
Explanation: The equals() method defines logical equality. Its default implementation compares references, so classes override it to compare meaningful state.
Question 12 of 50
What contract must hashCode() satisfy?
Correct answer: D) Equal objects must produce equal hash codes
Explanation: The contract requires that objects equal under equals() return the same hashCode(). Unequal objects may share a hash code, which is a collision.
Question 13 of 50
What is a marker interface?
Correct answer: A) An interface with no methods used to tag a class with metadata
Explanation: A marker interface declares no methods and exists solely to signal that a class has a certain property, such as Serializable or Cloneable.
Question 14 of 50
What is a functional interface?
Correct answer: B) An interface with exactly one abstract method
Explanation: A functional interface declares exactly one abstract method, allowing it to be implemented concisely with a lambda expression or method reference.
Question 15 of 50
What is a lambda expression used for in Java?
Correct answer: C) Providing a concise implementation of a functional interface
Explanation: A lambda expression provides an inline implementation of a functional interface's single abstract method, replacing verbose anonymous inner classes.
Question 16 of 50
What does the Iterator interface provide?
Correct answer: D) A standard way to traverse elements of a collection
Explanation: An Iterator supplies hasNext() and next() methods for sequentially traversing a collection independently of its underlying implementation.
Question 17 of 50
Why should you use an Iterator's remove() method rather than modifying a collection during a for-each loop?
Correct answer: A) Direct modification during iteration throws ConcurrentModificationException
Explanation: Structurally modifying a collection while iterating with for-each throws ConcurrentModificationException. Iterator.remove() safely removes the current element.
Question 18 of 50
What is the difference between ArrayList and LinkedList?
Correct answer: B) ArrayList gives fast index access; LinkedList gives fast insertion and removal at ends
Explanation: ArrayList uses a backing array giving constant-time index access, while LinkedList uses nodes giving efficient insertion and removal but slower random access.
Question 19 of 50
What does the Collections.sort() method require of the elements?
Correct answer: C) They must be Comparable or a Comparator must be supplied
Explanation: Sorting requires a defined ordering, either through elements implementing Comparable or by passing an explicit Comparator to the sort method.
Question 20 of 50
What is the difference between Comparable and Comparator?
Explanation: Comparable is implemented by the class itself for its natural ordering. Comparator is a separate object allowing multiple alternative orderings.
Question 21 of 50
What is a checked exception example in Java?
Correct answer: A) IOException
Explanation: IOException is checked, so it must be caught or declared. The others extend RuntimeException and are unchecked.
Question 22 of 50
What does the throws clause in a method declaration indicate?
Correct answer: B) The checked exceptions the method may propagate to its caller
Explanation: The throws clause declares which checked exceptions may escape the method, requiring callers to handle or further declare them.
Question 23 of 50
Can a single try block have multiple catch blocks?
Correct answer: C) Yes, each handling a different exception type
Explanation: Multiple catch blocks may follow a try, each handling a specific exception type. They are evaluated in order, so more specific types must come first.
Question 24 of 50
What happens if a catch block for a superclass exception is placed before a subclass exception catch?
Correct answer: D) A compile error occurs because the subclass catch is unreachable
Explanation: A broader catch placed first would handle the subclass exception, making the later specific block unreachable, which the compiler rejects.
Question 25 of 50
What is a custom exception in Java?
Correct answer: A) A user-defined class extending Exception or RuntimeException
Explanation: Custom exceptions extend Exception or RuntimeException to represent domain-specific error conditions with meaningful names and additional context.
Question 26 of 50
What is the purpose of the Serializable interface?
Correct answer: B) Marking a class whose objects can be converted to a byte stream
Explanation: Serializable is a marker interface indicating that a class's objects may be serialized into a byte stream for storage or transmission.
Question 27 of 50
What is a stream in the Java Collections context?
Correct answer: C) A sequence of elements supporting functional-style operations
Explanation: The Stream API processes sequences of elements with functional operations such as filter, map, and reduce, often more expressively than explicit loops.
Question 28 of 50
What does the filter() operation on a stream do?
Correct answer: D) Retains only elements matching a given predicate
Explanation: filter() takes a predicate and produces a stream containing only the elements for which the predicate returns true.
Question 29 of 50
What does the map() operation on a stream do?
Correct answer: A) Transforms each element using a supplied function
Explanation: map() applies a function to each element, producing a new stream of the transformed results, leaving the source unchanged.
Question 30 of 50
What is the significance of streams being lazy?
Correct answer: B) Intermediate operations execute only when a terminal operation runs
Explanation: Intermediate operations build a pipeline without processing elements. Work begins only when a terminal operation such as collect() or forEach() is invoked.
Question 31 of 50
What is the purpose of the Optional class?
Correct answer: C) Representing a value that may or may not be present, avoiding null
Explanation: Optional explicitly models the possible absence of a value, encouraging callers to handle the empty case rather than risking a NullPointerException.
Question 32 of 50
What is a thread in Java?
Correct answer: D) An independent path of execution within a program
Explanation: A thread is a separate flow of execution allowing concurrent work within a single program, such as keeping a user interface responsive during long operations.
Question 33 of 50
Which method starts a new thread's execution?
Correct answer: A) start()
Explanation: Calling start() creates a new thread and invokes run() on it. Calling run() directly executes the code on the current thread instead.
Question 34 of 50
What is a race condition?
Correct answer: B) Incorrect behavior caused by unsynchronized concurrent access to shared data
Explanation: A race condition occurs when multiple threads access shared mutable state concurrently and the result depends on unpredictable timing of their execution.
Question 35 of 50
What does the synchronized keyword provide?
Correct answer: C) Mutual exclusion so only one thread executes the block at a time
Explanation: The synchronized keyword acquires a lock so only one thread at a time executes the protected code, preventing concurrent corruption of shared state.
Question 36 of 50
Why are immutable objects inherently thread-safe?
Correct answer: D) Their state cannot change, so concurrent reads are always safe
Explanation: Because immutable objects never change after construction, multiple threads can read them concurrently with no risk of inconsistent state or need for synchronization.
Question 37 of 50
What is the purpose of the java.util.Random class?
Correct answer: A) Generating sequences of pseudorandom numbers
Explanation: Random generates pseudorandom values of various types. For security-sensitive purposes, SecureRandom should be used instead.
Question 38 of 50
What does the LocalDate class represent?
Correct answer: B) A date without time or time zone information
Explanation: LocalDate represents a calendar date such as 2026-07-23 with no time-of-day or time zone component, part of the modern java.time API.
Question 39 of 50
What is the advantage of the java.time API over the older Date class?
Correct answer: C) Immutable, thread-safe classes with a clearer domain model
Explanation: The java.time classes are immutable and thread-safe with a clear separation of concepts, resolving longstanding design problems in the legacy Date and Calendar classes.
Question 40 of 50
What is the purpose of an anonymous inner class?
Correct answer: D) Providing a one-off implementation of an interface or class inline
Explanation: An anonymous inner class defines and instantiates an implementation in a single expression, useful for short one-time implementations, now often replaced by lambdas.
Question 41 of 50
What does the enhanced switch expression (Java 14+) provide over the traditional switch?
Correct answer: A) It returns a value and avoids accidental fall-through
Explanation: Switch expressions using the arrow form return a value directly and do not fall through between cases, eliminating a longstanding source of bugs.
Question 42 of 50
What is a record in modern Java?
Correct answer: B) A concise class for immutable data carriers with generated methods
Explanation: A record concisely declares an immutable data carrier, with the compiler generating the constructor, accessors, equals(), hashCode(), and toString().
Question 43 of 50
What is the purpose of the var keyword introduced in Java 10?
Correct answer: C) Letting the compiler infer a local variable's type
Explanation: The var keyword enables local variable type inference. The variable remains strongly and statically typed; only the explicit declaration is omitted.
Question 44 of 50
What is a common cause of a StackOverflowError?
Correct answer: D) Unbounded recursion without a reachable base case
Explanation: Each method call consumes stack space. Recursion that never reaches its base case exhausts the stack and triggers StackOverflowError.
Question 45 of 50
What is an OutOfMemoryError typically caused by?
Correct answer: A) The heap being exhausted, often by retaining too many objects
Explanation: OutOfMemoryError occurs when the JVM cannot allocate memory, commonly due to memory leaks where unneeded objects remain reachable and uncollectable.
Question 46 of 50
What is a memory leak in a garbage-collected language like Java?
Correct answer: B) Unneeded objects remaining reachable so they are never collected
Explanation: Even with garbage collection, objects that remain referenced, such as entries never removed from a long-lived collection, cannot be reclaimed and accumulate over time.
Question 47 of 50
What is the purpose of unit testing frameworks such as JUnit?
Correct answer: C) Automating verification that code units behave as expected
Explanation: JUnit provides annotations and assertions for writing automated tests that verify individual units of code, enabling rapid regression detection.
Question 48 of 50
What does an assertion in a unit test verify?
Correct answer: D) That an actual result matches the expected result
Explanation: An assertion compares the actual outcome against the expected value and fails the test when they differ, defining the test's pass criteria.
Question 49 of 50
What is the value of writing tests before or alongside implementation?
Correct answer: A) It clarifies requirements and catches defects immediately
Explanation: Writing tests early forces clear thinking about expected behavior and surfaces defects as soon as they are introduced, when they are cheapest to fix.
Question 50 of 50
What is the single most important habit for writing maintainable Java code?
Correct answer: B) Writing clear, well-named, small units with consistent structure
Explanation: Clear naming, small focused units, and consistent structure make code understandable to the next reader, which is the dominant cost factor over a system's lifetime.
Set 4 complete
You scored 0%
0 correct out of 0 answered (50 questions in this set).