1. In Spring MVC how to handle exceptions?
- Controller Based: We annotate our controller with @ExceptionHandlerThis takes exception class as argument.
- Global Exception Handler: Here we use @ControllerAdvice annotation that we can use with any class to define our global exception handler.
- HandlerExceptionResolver: Interface that we can implement to create global exception handler
2. What are the annotations would you use?
@ControllerAdvice,@ExceptionHandler
3. For Global exception handler? What would you use?
@ControllerAdvice
@ControllerAdvice
4. For Controller Based?
@ExceptionHandler
5. Get and Load Method?
load() - returns the proxy object with an identifier.
get() - returns the complete object from database.
6. How to create custom exceptions?
7. Checked, Unchecked Exceptions?
8. How would you except sql injection using java?
9. Which design pattern did you use?
-Design patterns make your code more flexible, reusable and maintainable.
-Design pattern represents an idea, not a particular implementation.
1) Factory Design Patterns
-Define an interface or abstract class for creating an object but let the
subclasses decide which class to instantiate
-When to use
- When a class doesn't know what sub-classes will be required to create
- When a class wants that its sub-classes specify the objects to be created.
2) Abstract Factory Patterns
- Define an interface or abstract class for creating families of related
(or dependent) objects but without specifying their concrete sub-classes.
- When to use
- When the system needs to be independent of how its object are created,
composed, and represented.
- When you want to provide a library of objects that does not show
implementations and only reveals interfaces.
3) Singleton Design Pattern
- Define a class that has only one instance and provides a global point of access to it
@ExceptionHandler
5. Get and Load Method?
load() - returns the proxy object with an identifier.
get() - returns the complete object from database.
6. How to create custom exceptions?
- Extend the exception-class
- We also have to provide a constructor that takes a String as the error message and called the parent class constructor.
7. Checked, Unchecked Exceptions?
- Are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.
- Unchecked are the exceptions that are not checked at compiled time.so it is not forced by the compiler to either handle or specify the exception.
- It is up to the programmers to be civilized, and specify or catch the exceptions.
8. How would you except sql injection using java?
9. Which design pattern did you use?
-Design patterns make your code more flexible, reusable and maintainable.
-Design pattern represents an idea, not a particular implementation.
1) Factory Design Patterns
-Define an interface or abstract class for creating an object but let the
subclasses decide which class to instantiate
-When to use
- When a class doesn't know what sub-classes will be required to create
- When a class wants that its sub-classes specify the objects to be created.
2) Abstract Factory Patterns
- Define an interface or abstract class for creating families of related
(or dependent) objects but without specifying their concrete sub-classes.
- When to use
- When the system needs to be independent of how its object are created,
composed, and represented.
- When you want to provide a library of objects that does not show
implementations and only reveals interfaces.
3) Singleton Design Pattern
- Define a class that has only one instance and provides a global point of access to it
Two forms:
1) Early Instantiation: creation of instance at load time.
2) Lazy Instantiation: creation of instance when required.
-When to use it
- Singleton pattern is mostly used in multi-threaded and database applications.
It is used in logging, caching, thread pools, configuration settings etc.
- Saves memory because object is not created at each request. Only single instance
is reused again and again.
10. How to make object immutable in java?
11. Why should you make an object immutable?
12. How to make a thread safe?
13. Volatile
14. Why string is immutable in java?
15. Read-write lock?
16. Can you modify the state of the object?
17. What is busy spinning?
18. If you use the large number of import statements does it
effect the performance of the class? If some are not used in class?
1) Early Instantiation: creation of instance at load time.
2) Lazy Instantiation: creation of instance when required.
-When to use it
- Singleton pattern is mostly used in multi-threaded and database applications.
It is used in logging, caching, thread pools, configuration settings etc.
- Saves memory because object is not created at each request. Only single instance
is reused again and again.
10. How to make object immutable in java?
- Don't use any methods, which can change fields of your class. For example, don't use Setters.
- Avoid to use public non-final fields. If your fields is public, then you must declare them as final
- and initialize them in the constructor or directly in the declaration line.
11. Why should you make an object immutable?
- Immutable objects are simpler to construct, test, and use.
- Truly immutable objects are always thread-safe
- They help to avoid temporal coupling.
12. How to make a thread safe?
- Immutable objects are by default thread-safe because their state can not be modified once created.
- Since String is immutable in Java, its inherently thread-safe.
- Read-only or final variables in Java are also thread-safe in Java.
- Locking is one way of achieving thread-safety in Java.
- Example of thread-safe class in Java: Vector, Hashtable, ConcurrentHashMap, String etc.
- Volatile keyword in Java can also be used to instruct thread not to cache variables and read from main memory and can also instruct JVM not to reorder or optimize code from threading perspective.
13. Volatile
- The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory";
- Whenever thread is updating the values, it is updated to the main memory. So, other threads can access the updated value.
14. Why string is immutable in java?
- Security: parameters are typically represented as String in network connections, database connection URLs, usernames/passwords etc. If it were mutable, these parameters could be easily changed.
- Class loading: String is used as arguments for class loading. If mutable, it could result in the wrong class being loaded (because mutable objects change their state).
15. Read-write lock?
- A ReadWriteLock maintains a pair of associated locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive.
- readLock(): Returns the lock used for reading.
- writeLock(): Returns the lock used for writing.
16. Can you modify the state of the object?
- Yes,So you can alter the state of an object by changing the value of its attributes.
17. What is busy spinning?
- Busy-waiting or spinning is a technique in which a process repeatedly checks to see if a condition is true instead of calling wait or sleep method and releasing CPU. A "busy spin" is constantly looping in one thread to see if the other thread has completed some work
18. If you use the large number of import statements does it
effect the performance of the class? If some are not used in class?
- It doesn't impact performance to have excess import statements. It may make the source code longer than it should be, but there is no effect on the compiled code. Java itself imports unnecessary class files.
19. hotspot scenario for variables?
20. What will happen if an exception is thrown from finally block?
- Basically, finally clauses are there to ensure proper
- release of a resource. However, if an exception is thrown
- inside the finally block, that guarantee goes away. Worse,
- if your main block of code throws an exception, the exception
- raised in the finally block will hide it. It will look like the
- error was caused by the call to close, not for the real reason.
No comments:
Post a Comment