java questions
1.Can static block throw exception and how would you handle it?
Ans: Checked exceptions cannot be thrown from static block. In general, it is better to use try/catch block to handle exceptions, but here it does not work. So we can either throw a runtime exception which will end the current thread or call system.exit(1) if the static block execution is very critical in the application.
Ans: Checked exceptions cannot be thrown from static block. In general, it is better to use try/catch block to handle exceptions, but here it does not work. So we can either throw a runtime exception which will end the current thread or call system.exit(1) if the static block execution is very critical in the application.
2. Why would you need a reentrant lock when there is synchronized keyword? How will you favor one over the other?
Ans: The ReentrantLock has the same concurrency and memory semantics as synchronized. It also provides features like lock polling, timed lock waits, and interruptible lock waits. Additionally, it offers far better performance under heavy contention.
Ans: The ReentrantLock has the same concurrency and memory semantics as synchronized. It also provides features like lock polling, timed lock waits, and interruptible lock waits. Additionally, it offers far better performance under heavy contention.
We can use the ReentrantLock when we actually need something it provides that synchronized doesn’t, like timed lock waits, interruptible lock waits etc. It also has scalability benefits, and you should use it if you actually have a situation that exhibits high contention. However, the existences of these obvious benefits are not a good enough reason to always prefer ReentrantLock over synchronized.
In majority of the cases, synchronized works just fine, works on all JVMs, is understood by a wider range of developers, and is less error-prone. So make the decision on the basis of need.
3. What is AtomicInteger class and how is it different than using volatile or synchronized?
Ans: An int value that may be updated atomically. An AtomicInteger is used in applications where we need to atomically increment counters.
Ans: An int value that may be updated atomically. An AtomicInteger is used in applications where we need to atomically increment counters.
If the variable declared as Volatile then it always takes master copy of variable instead of local copy of a variable.
If the method or block of code marked as syschronized then it prevents multiple threads accessing that part of the code.
4. Why do we need to declare a local variable final if inner class declare within a method needs to use it?
Ans: If any variable is declared within a method then the memory is allocated in stack and has method scope which means that the variable is not accessible outside the method.
So the declaration of that variable inside the method cannot be used by the class. If we declare the variable as final then it becomes compile time constant and is accessible outside the method as well.
Ans: If any variable is declared within a method then the memory is allocated in stack and has method scope which means that the variable is not accessible outside the method.
So the declaration of that variable inside the method cannot be used by the class. If we declare the variable as final then it becomes compile time constant and is accessible outside the method as well.
5. How will you awake a blocked thread in java?
Ans: We can awake the thread by throwing InterruptedException if it is blocked by calling wait(), sleep() or join() methods. I don’t think there is a way to awake the thread if it is blocked on IO.
Ans: We can awake the thread by throwing InterruptedException if it is blocked by calling wait(), sleep() or join() methods. I don’t think there is a way to awake the thread if it is blocked on IO.
6. Which part of the memory is involved in Garbage Collection?
Ans: The Heap memory is involved in Garbage Collection.
Ans: The Heap memory is involved in Garbage Collection.
7. Why would you declare a private constructor?
Ans: We would declare a private constructor to prevent creating an instance of a class from other classes. One popular use case for private constructor is singleton design pattern.
Ans: We would declare a private constructor to prevent creating an instance of a class from other classes. One popular use case for private constructor is singleton design pattern.
8. When providing a user defined key class for storing objects in the HashMaps, what methods do you have to provide or override?
Ans: We have to provide or override equals() and/or hashCode() methods.
Ans: We have to provide or override equals() and/or hashCode() methods.
9. What is immutable object? How does it help on writing concurrent application?
Ans: Immutable object state cannot be changed once it is constructed. It helps on writing the concurrent application as its state cannot be changed or corrupted by multiple threads. It simplifies the multi-threaded application as it is simple to construct, use, and test. It automatically provides thread safety (no synchronization issues).
Ans: Immutable object state cannot be changed once it is constructed. It helps on writing the concurrent application as its state cannot be changed or corrupted by multiple threads. It simplifies the multi-threaded application as it is simple to construct, use, and test. It automatically provides thread safety (no synchronization issues).
10. Who catches runtime exception if there is no catch block the code ?
Ans: The finally block will catch the runtime exception if there is no catch block in the code.
Ans: The finally block will catch the runtime exception if there is no catch block in the code.
No comments:
Post a Comment