Method Overriding and Exceptions
When overriding a method, can you broaden the scope of exceptions if the original method declares checked exceptions?
- Yes, you can broaden the scope of checked exceptions during method overriding.
- No, you cannot broaden the scope of checked exceptions during method overriding.
- Yes, but only for RuntimeExceptions.
- It depends on the compiler settings.
- Only if the superclass method doesn't declare any exceptions.
Thread Access to Methods
If one thread is accessing a synchronized method m1 of an object, can another thread access a non-synchronized method m2 on the same object concurrently?
- No, it cannot because the object is locked.
- Yes, it can as m2 does not require a lock.
- Yes, but only if m2 is static.
- No, only if m1 releases the lock first.
- It depends on thread priorities.
HashMap and Custom Objects
If you use a custom object as a key in a HashMap and then modify the object's attributes after it's been added, what happens when you try to retrieve the object using the modified key?
- It will successfully retrieve the value associated with the key.
- It will return null.
- It will throw an exception.
- It will retrieve a different entry in the map.
- It will create a new entry in the map.
Garbage Collection Tuning
How does the NewRatio parameter affect garbage collection in Java?
- It controls the frequency of full GCs.
- It determines the size ratio between the young and old generations.
- It specifies the minimum heap size.
- It sets the maximum heap size.
- It enables or disables garbage collection.
ConcurrentHashMap vs. SynchronizedMap
What is the primary difference between using Collections.synchronizedMap and ConcurrentHashMap for making a map thread-safe?
- Collections.synchronizedMap provides better performance.
- ConcurrentHashMap locks the entire map object, while Collections.synchronizedMap locks only part of it.
- Collections.synchronizedMap locks the entire map object, while ConcurrentHashMap locks only a segment of the map.
- ConcurrentHashMap is only suitable for read-only operations.
- There is no difference; they are interchangeable.
Iterator and Concurrent Modification
What exception is thrown when you attempt to modify a HashMap while iterating over it using an iterator?
- NullPointerException
- IllegalArgumentException
- ConcurrentModificationException
- IOException
- IndexOutOfBoundsException
Singleton Design Pattern
What is double-checked locking in the Singleton design pattern used for?
- To ensure only one thread can access the singleton instance.
- To make the singleton class immutable.
- To improve performance by caching the singleton instance.
- To make the Singleton thread-safe.
- To prevent class loading issues.
Java 8 Enhancements
Which of the following is a major feature introduced in Java 8?
- Generics
- Annotations
- Lambada Expressions
- Reflection
- Multi-threading
Memory Leak Analysis
Which tool can be used to analyze a Java application for memory leaks?
- JConsole
- JVisualVM
- Eclipse Memory Analyzer Tool
- JProfiler
- All of the above
Method Overloading
Can two methods in the same class have the same name and parameters but different return types in Java?
- Yes, method overloading supports different return types.
- No, method overloading only supports different parameter lists.
- Yes, if the methods have different access modifiers.
- No, the compiler will throw an error due to ambiguous method signatures.
- Yes, but only if one method is static and the other is not.