SOLID Principles - Substitution
According to the Liskov Substitution Principle, which of the following statements best demonstrates compliance with the principle when extending a class Shape with subclasses Circle and Square?
- A. Methods accepting Shape should work correctly with both Circle and Square instances.
- B. Subclasses like Circle must add new exceptions to parent methods.
- C. Circle should require additional arguments compared to Shape in its public methods.
- D. Square must remove inherited properties from Shape.
- E. Shape must be declared final to prevent extension.
Inheritance vs Interface
Which statement accurately differentiates between abstract classes and interfaces in Java as of Java 8?
- A. Abstract classes may contain state (fields), while interfaces cannot.
- B. Interfaces can have both abstract and non-abstract methods if declared default or static.
- C. A Java class can extend multiple abstract classes.
- D. An interface may not have any methods at all.
- E. Abstract classes must implement all inherited abstract methods.
Object Relationships
Given two classes, Library and Book, if the Library manages the lifecycle of Books (Books do not exist without Library), which term best defines their relationship?
- A. Association
- B. Aggregation
- C. Composition
- D. Inheritance
- E. Dependency
Collection Types
Suppose you need to maintain an ordered collection of unique elements in Java. Which collection type would best fit this requirement?
- A. ArrayList
- B. HashMap
- C. LinkedList
- D. TreeSet
- E. Vector
Concurrency Utilities
Which of the following Java concurrency utilities guarantees that only one thread executes a particular section of code at a time, and automatically releases the lock even if an exception occurs?
- A. ReentrantLock without try-finally
- B. Semaphore
- C. synchronized block
- D. CountDownLanch
- E. CyclicBarier
JVM and Garbage Collection
If a Java object is no longer referenced by any variable and cannot be accessed, what will happen to it during the next garbage collection cycle?
- A. It throws a NullPointerException.
- B. It is moved to the permanent generation.
- C. It becomes eligible for garbage collection.
- D. It is immediately removed from memory.
- E. Its finalize() method is called synchronously.
Design Patterns - Singleton
Which option presents a common risk when implementing the Singleton pattern in a multi-threaded Java application without synchronization?
- A. Memory leak due to retained references
- B. Multiple instantiations occurring concurrently
- C. Singleton object not being serializable
- D. Excessive garbage collection cycles
- E. Reduced execution speed due to heavy locking
Exception Handling
Suppose a method declares 'throws IOException, RuntimeException'. What happens if you call this method without a try-catch block?
- A. Both exceptions must be caught in the calling method.
- B. IOException must be handled or declared, RuntimeException can be ignored.
- C. Neither exception needs to be handled by the caller.
- D. The Java compiler generates a warning but not an error.
- E. The method cannot be called outside a try-catch block.
Functional Programming with Streams
When using Java Streams, which of the following operations is considered terminal and triggers stream processing?
- A. map
- B. filter
- C. sorted
- D. forEach
- E. peak
Unit Testing Best Practices
When using JUnit for unit testing a Java method that accesses a database, which practice leads to the most reliable and maintainable tests?
- A. Hard-coding database credentials in test methods
- B. Using mock objects to simulate database operations
- C. Deleting the database after each test run
- D. Testing all possible exceptions in the same test case
- E. Including print statements to debug test outputs