
Java Programming
May 16, 2025 at 02:52 PM
*Intermediate Java Interview Questions with Answers– Part 5*
*21. What is a static block in Java and when is it executed?*
A static block is used to initialize static variables. It runs once when the class is first loaded into memory.
*Example:*
class MyClass {
static int value;
static {
value = 10;
System.out.println("Static block executed");
}
}
*22. Can a class extend multiple classes in Java? Why not?*
No, Java does not support multiple inheritance with classes to avoid ambiguity. A class can only extend one class, but it can implement multiple interfaces.
*23. What is marker interface in Java?*
A marker interface is an interface with no methods or fields. It's used to convey metadata or behavior to the JVM or frameworks.
*Example:*
Serializable, Cloneable are marker interfaces.
*24. What is the difference between wait(), sleep(), and join() in Java?*
wait() – releases the lock and waits to be notified. Used in inter-thread communication.
sleep() – pauses the thread for a specific time but does not release the lock.
join() – makes the current thread wait until another thread finishes execution.
*25. What is the volatile keyword in Java?*
The volatile keyword ensures visibility of changes to variables across threads. It prevents thread caching of variables and always reads from the main memory.
*Example:*
volatile boolean running = true;
*React ❤️ for more*
❤️
❤
👍
😮
🎂
🙏
♥
😮💨
🚡
💻
140