250+ TOP MCQs on Bean Post Processors and Answers

Java Spring Multiple Choice Questions & Answers (MCQs) on “Bean Post Processors”.

1. Method used to process bean before initialization callback
a) scope
b) postProcessAfterInitialization()
c) postProcessBeforeInitialization()
d) it’s own constructor
Answer: c
Clarification: You can process every bean before initialization callback method by implementing the postProcessBeforeInitialization() and methods.

2. Method used to process bean after initialization callback
a) scope
b) getBean
c) postProcessAfterInitialization()
d) it’s own constructor
Answer: c
Clarification: You can process every bean after initialization callback method by implementing the postProcessAfterInitialization() and methods.

3. Which method is used to gracefully shutdown all the bean processes after closing the spring container?
a) shutdownHook
b) destroy method
c) none of the mentioned
d) all of the mentioned
Answer: a
Clarification: ShutdownHook method gracefully shuts down each bean process before closing the container.

4. Which method is used to register BeanPostProcessor?
a) addBeanPostProcessors
b) registerBeanPostProcessors
c) addBeanProcessors
d) none of the mentioned
Answer: a
Clarification: When using a bean factory as your IoC container, bean post processors can only be registered programmatically, or more accurately, via the addBeanPostProcessor() method.

5. In application context, BeanPost Processors are registered using addBeanPostProcessors method
a) True
b) False
Answer: b
Clarification: Using an application context, the registration will be as simple as declaring an instance of the processor in the bean configuration file.

6. Which Interface for bean Post Processor is used to distinguish between checked beans
a) StorageConfig
b) Marker
c) None of the mentioned
d) All of the mentioned
Answer: a
Clarification: First of all, for the bean post processor to distinguish which beans should be checked, you create a marker interface, StorageConfig, for your target beans to implement.

7. Which method of bean post processors is used to check path existence
a) getPath
b) setPath
c) value
d) auto-wiring
Answer: a
Clarification: Moreover, for your bean post processor to check for path existence, it must be able to access the path property. This can be done by adding the getPath() method to this interface.

8. It’s possible to replace the original bean instance with a brand-new instance in your bean post processor
a) True
b) False
Answer: a
Clarification: Both the postProcessBeforeInitialization() and postProcessAfterInitialization() methods must return an instance for the bean being processed.

9. PathCheckingBeanPostProcessor will not work properly in case of:-
a) XML Configuration
b) Java based Configuration
c) JSR Annotation
d) None of the mentioned
Answer: c
Clarification: This is because your bean post processor has a lower priority than CommonAnnotationBeanPostProcessor by default. As a result, the initialization method will be called before your path checking.

10. Which bean factory post processor externalizes part of the bean configurations into a properties file
a) PropertyPlaceholderConfigurer
b) PropertyPlaceholderRegister
c) PropertyGetPath
d) None of the mentioned
Answer: a
Clarification: Spring comes with a bean factory post processor called PropertyPlaceholderConfigurer for you to externalize part of the bean configurations into a properties file.

11. Which interface defines methods for resolving text messages
a) MessageSource
b) ApplicationListener
c) ApplicationContextListener
d) TextEvent
Answer: a
Clarification: MessageSource is an interface that defines several methods for resolving messages.

12. Which interface is used to listen to certain events
a) ApplicationListener
b) ContextListener
c) EventListener
d) None of the mentioned
Answer: a
Clarification: For a bean to listen to certain events, it must implement the ApplicationListener interface and handle the events in the onApplicationEvent() method.

13. Which method is used to publish your own custom event
a) contextPublish
b) publishEvent
c) applicationEventPublish
d) addEvent
Answer: b
Clarification: Any bean can publish an event by calling an application event publisher’s publishEvent() method.

14. Which of the following is a well known recognized event
a) ContextCloasedEvemt
b) ContextRefreshedEvent
c) RequestHandledEvent
d) All of the mentioned
Answer: d
Clarification: The application context itself will also publish container events such as ContextClosedEvent, ContextRefreshedEvent, and RequestHandledEvent.

15. Which event is provoked when web request comes into action
a) ContextClosedEvent
b) ContextStoppedEvent
c) SessionEvent
d) RequestHandledEvent
Answer: d
Clarification: Web request events are handled by RequestHandledEvent.

250+ TOP MCQs on User Locales and Locale-Sensitive Text Messages and Answers

“Java Spring Interview Questions and Answers for freshers on “User Locales and Locale-Sensitive Text Messages”.

1. Interface implemented by locale resolver.
a) localeResolver
b) tiles
c) front controller
d) none of the mentioned
Answer: a
Clarification: In a Spring MVC application, a user’s locale is identified by a locale resolver, which has to implement the LocaleResolver interface.

2. You can define a locale resolver by registering a bean of type LocaleResolver in the web application context.
a) True
b) False
Answer: a
Clarification: You must set the bean name of the locale resolver to localeResolver for DispatcherServlet to auto-detect.

3. Default localeResolver used by Spring.
a) AcceptHeaderLocale
b) AcceptHeader
c) AcceptHeaderLocaleResolver
d) AcceptLocaleResolver
Answer: c
Clarification: The default locale resolver used by Spring is AcceptHeaderLocaleResolver.

4. AcceptHeaderLocaleResolver esolves locales by inspecting the accept-language header of an HTTP request.
a) True
b) False
Answer: a
Clarification: This header is set by a user’s web browser according to the locale setting of the underlying operating system.

5. Alternative way to resolve locales.
a) AcceptHeaderLocale
b) AcceptHeader
c) AcceptHeaderLocaleResolver
d) SessionLocaleResolver
Answer: d
Clarification: Another option of resolving locales is by SessionLocaleResolver.

6. Property which indicates how many seconds this cookie should be persisted.
a) cookieMaxAge
b) cookieName
c) cookieAge
d) none of the mentioned
Answer: a
Clarification: The cookieMaxAge property indicates how many seconds this cookie should be persisted.

7. Method used to change user’s locale.
a) set
b) setLocale
c) locale
d) set_locale
Answer: b
Clarification: Changing a user’s locale by calling LocaleResolver.setLocale() explicitly.

8. Property of LocaleChangeInterceptor used to customize parameter name.
a) localeResolver.setLocale
b) paramName
c) locale.parm
d) none of the mentioned
Answer: b
Clarification: Changing a user’s locale by calling LocaleResolver.setLocale() explicitly.

9. LocaleChangeInterceptor can only detect the parameter for the handler mappings that enable it.
a) True
b) False
Answer: a
Clarification: So, if you have more than one handler mapping configured in your web application context, you have to register this interceptor to allow users to change their locales in any of the URLs.

10. Parameter which can change user’s locale by any URL.
a) locale
b) languageURL
c) language
d) none of the mentioned
Answer: c
Clarification: Now a user’s locale can be changed by any URLs with the language parameter.

11. Interface used to resolve text messages.
a) Message
b) Message_Source
c) Language
d) MessageSource
Answer: d
Clarification: Spring is able to resolve text messages for you by using a message source, which has to implement the MessageSource interface.

12. Tag used by view to resolve text messages.
a) spring:message
b) spring:language
c) spring:title
d) none of the mentioned
Answer: a
Clarification: Then your JSP files can use the spring:message tag, defined in Spring tag library, to resolve a message given the code.

13. The ResourceBundleMessageSource implementation resolves messages for different locales.
a) True
b) False
Answer: a
Clarification: The ResourceBundleMessageSource implementation resolves messages from different resource bundles for different locales.

14. cookieName is used by localeResolver to customize the cookie used.
a) True
b) False
Answer: a
Clarification: The cookie used by this locale resolver can be customized by setting the cookieName and cookieMaxAge properties.

15. The cookieMaxAge value when browser is closed.
a) 0
b) 1
c) -1
d) true
Answer: c
Clarification: The value -1 indicates that this cookie will be invalid after the browser is closed.


Java Spring for interviews,

250+ TOP MCQs on Spring Portlet MVC and Answers

Java Spring Multiple Choice Questions & Answers (MCQs) on “Spring Portlet MVC”.

1. DispatcherServtlet dispatches portlet requests to appropriate handlers that handle the requests.
a) True
b) False
Answer: b
Clarification: DispatcherPortlet. It dispatches portlet requests to appropriate handlers that handle the requests. It acts as the front controller of Spring Portlet MVC, and every portlet request must go through it so that it can manage the entire request-handling process.

2. Maps each request to a handler through one or more handler mapping beans.
a) DispatcherServlet
b) DispatcherPortlet
c) All of the mentioned
d) None of the mentioned
Answer: b
Clarification: Once it has picked an appropriate handler, it will invoke this handler to handle the request.

3. In portlets, there are URLs:-
a) render
b) action
c) all of the mentioned
d) none of the mentioned
Answer: c
Clarification: In portlets, there are two types of URLs: render URLs and action URLs.

4. When a user triggers a render URL, the portlet container will ask all the portlets in the same page to handle a render request to render its view.
a) True
b) False
Answer: a
Clarification: When a user triggers a render URL, the portlet container will ask all the portlets in the same page to handle a render request to render its view, unless the view’s content has been cached.

5. When a user triggers an action URL in a portlet, the portlet container will first ask the target portlet to handle an action request.
a) True
b) False
Answer: a
Clarification: A controller should return an object in response to a render request. However, when a user triggers an action URL in a portlet, the portlet container will first ask the target portlet to handle an action request. A controller needn’t return anything for an action request.

6. After Controller has finished handling a render request, it returns:-
a) model name
b) view name
c) view obect
d) all of the mentioned
Answer: d
Clarification: After a controller has finished handling a render request, it returns a model and a view name, or sometimes a view object, to DispatcherPortlet.

7. DispatcherPortlet resolves a view name from one or more view resolver beans.
a) True
b) False
Answer: a
Clarification: DispatcherPortlet renders the view and passes in the model returned by the controller. Note that an action request doesn’t need to render a view.

8. portlet deployment descriptor file is:-
a) portlet.xml
b) portlet.config
c) portlet.xhtml
d) none of the mentioned
Answer: a
Clarification: The Java Portlet specification defines the valid structure of a portlet application, which is very similar to that of a web application, but with the addition of a portlet deployment descriptor (i.e., portlet.xml).

9. Servlet Listener to load the root application context at startup.
a) ContextLoader
b) ContextLoaderListener
c) All of the mentioned
d) None of the mentioned
Answer: b
Clarification: In the web deployment descriptor (i.e., web.xml), you have to register the servlet listener ContextLoaderListener to load the root application context at startup.

10. Unlike in a web application, you can’t control URLs directly in a portlet.
a) True
b) False
Answer: a
Clarification: In the web deployment descriptor (i.e., web.xml), you have to register the servlet listener ContextLoaderListener to load the root application context at startup.

11. You can chain multiple handler mapping annotations as required to portlet.
a) True
b) False
Answer: a
Clarification: Spring Portlet MVC comes with several strategies for you to map portlet requests.

12. The preceding controller handles portlet requests:-
a) render requests
b) action requests
c) all of the mentioned
d) none of the mentioned
Answer: c
Clarification: The preceding controller handles two types of portlet requests: render requests and action requests.

13. When handling a render request, it gets the time zone attribute from the portlet preference.
a) True
b) False
Answer: a
Clarification: When handling a render request, it gets the time zone attribute from the portlet preferences and passes it to the view for editing.

14. When handling an action request, it gets the time zone parameter from the portlet request.
a) True
b) False
Answer: a
Clarification: When handling an action request, it gets the time zone parameter from the portlet request and stores it in the portlet preferences as an attribute, which will override the existing one.

15. The submission URL of this form should be a portlet action URL that will trigger an action request to the current portlet.
a) True
b) False
Answer: a
Clarification: In portlets, you have to construct an action URL with the portlet:actionURL tag defined in the portlet tag library.

250+ TOP MCQs on Message-Driven POJOs in Spring and making connection and Answers

Java Spring question bank on “Message-Driven POJOs in Spring and making connection”

1. When you call the receive() method on a JMS message consumer to receive a message.
a) send
b) receive
c) All of the mentioned
d) None of the mentioned

Answer: b
Clarification: When you call the receive() method on a JMS message consumer to receive a message, the calling thread is blocked until a message is available.

2. EJB component which was introduced for asynchronous reception of JMS messages.
a) message-driven bean
b) message-driven
c) message bean
d) none of the mentioned

Answer: a
Clarification: Starting with EJB 2.0, a new kind of EJB component called a message-driven bean (MDB) was introduced for asynchronous reception of JMS messages.

3. MDB must implements interfaces:-
a) javax.ejb.MessageDrivenBean
b) javax.jms.MessageListener
c) all of the mentioned
d) none of the mentioned

Answer: c
Clarification: An MDB must implement both the javax.ejb.MessageDrivenBean and javax.jms.MessageListener interfaces and override all EJB life cycle methods (ejbCreate and ejbRemove).

4. In EJB 3.0, an MDB can be a POJO that implements the MessageListener interface and is annotated with the:-
a) @MessageDrive
b) @Message
c) all of the mentioned
d) @MessageDriven

Answer: d
Clarification: In EJB 3.0, an MDB can be a POJO that implements the MessageListener interface and is annotated with the @MessageDriven annotation.

5. You create a message listener to listen for JMS messages. This negates the need for the approach taken in BackOfficeImpl in previous recipes.
a) True
b) False

Answer: a
Clarification: For example, the following MailListener listens for JMS messages that contain mail information:-

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
 
import org.springframework.jms.support.JmsUtils;
public class MailListener implements MessageListener {
	public void onMessage(Message message) {
	MapMessage mapMessage = (MapMessage) message;
	try {
		Mail mail = new Mail();
		mail.setMailId(mapMessage.getString("mailId"));
		mail.setCountry(mapMessage.getString("country"));
		mail.setWeight(mapMessage.getDouble("weight"));
		displayMail(mail);
	} catch (JMSException e) {
		throw JmsUtils.convertJmsAccessException(e);
	}
	}
	private void displayMail(Mail mail) {
	System.out.println("Mail #" + mail.getMailId() + " received");
	}
}

6. A message listener must implement the:-
a) javax.jms.MessageListener
b) javax.jms.Message
c) javax.jms
d) none of the mentioned

Answer: a
Clarification: A message listener must implement the javax.jms.MessageListener interface.

7. When a JMS message arrives, the onMessage() method will be called with the message as the method argument.
a) True
b) False

Answer: a
Clarification: In this sample, you simply display the mail information to the console.

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
 
import org.springframework.jms.support.JmsUtils;
public class MailListener implements MessageListener {
	public void onMessage(Message message) {
	MapMessage mapMessage = (MapMessage) message;
	try {
		Mail mail = new Mail();
		mail.setMailId(mapMessage.getString("mailId"));
		mail.setCountry(mapMessage.getString("country"));
		mail.setWeight(mapMessage.getDouble("weight"));
		displayMail(mail);
	} catch (JMSException e) {
		throw JmsUtils.convertJmsAccessException(e);
	}
	}
	private void displayMail(Mail mail) {
	System.out.println("Mail #" + mail.getMailId() + " received");
	}
}

8. Method to convert MapMessage Object into Spring runtime exception JmsException.
a) JmsUtils.convertJmsAccessException()
b) JmsUtils.convertJmsAccess()
c) JmsUtils.convertJms()
d) none of the mentioned

Answer: a
Clarification: When extracting message information from a MapMessage object, you need to handle the JMS API’s JMSException. You can make a call to JmsUtils.convertJmsAccessException() to convert it into Spring runtime exception JmsException.

9. Spring provides several types of message listener containers:-
a) SimpleMessageListenerContainer
b) DefaultMessageListenerContainer
c) All of the mentioned
d) None of the mentioned

Answer: c
Clarification: Spring provides several types of message listener containers for you to choose from in the org.springframework.jms.listener package, of which SimpleMessageListenerContainer and DefaultMessageListenerContainer are the most commonly used.

10. If you have a transaction requirement in receiving messages, you have to use:-
a) SimpleMessageListenerContainer
b) DefaultMessageListenerContainer
c) All of the mentioned
d) None of the mentioned

Answer: b
Clarification: If you have a transaction requirement in receiving messages, you have to use DefaultMessageListenerContainer.

11. You have to set the delegate property of MessageListenerAdapter to your target bean.
a) True
b) False

Answer: a
Clarification: By default, this adapter will call the method whose name is handleMessage on that bean.

12. Using Spring JMS support with a very simple instance of:-
a) org.apache.activemq.ActiveMQConnectionFactory
b) org.apache.activemq.ActiveMQConnection
c) org.apache.activemq.ActiveMQ
d) none of the mentioned

Answer: a
Clarification: We’ve explored using Spring JMS support with a very simple instance of org.apache.activemq.ActiveMQConnectionFactory as our connection factory.

13. ActiveMQ, provides only one pooled connection factory class alternative.
a) True
b) False

Answer: b
Clarification: ActiveMQ provides two, actually: one for use consuming messages with a JCA connector and another one for use outside of a JCA container.

14. It caches consumers correctly, or use Spring ConnectionFactory implementations.
a) MessageListenerContainer
b) MessageListener
c) MessageContainer
d) None of the mentioned

Answer: a
Clarification: MessageListenerContainer implementations mechanism (MDPs), because it caches consumers correctly, or use Spring ConnectionFactory implementations.

15. Implementation which returns the same underlying JMS connection each time (which is thread-safe according to the JMS API) and ignores calls to the close() method.
a) org.springframework.jms.connection.SingleConnectionFactory
b) org.springframework.jms.connection.Single
c) org.springframework.jms.connection.SingleConnection
d) none of the mentioned

Answer: a
Clarification: org.springframework.jms.connection.SingleConnectionFactory, returns the same underlying JMS connection each time (which is thread-safe according to the JMS API) and ignores calls to the close() method.

To practice Java Spring question bank,

250+ TOP MCQs on Serialization in Java and Answers

tough Java Spring Questions on “Serialization in Java”.

1. Java provides a mechanism, where an object can be represented as a sequence of bytes:-
a) Serialization
b) Compilation
c) All of the mentioned
d) None of the mentioned
Answer: a
Clarification: Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object.

2. After a serialized object has been written into a file, it can be read from the file and deserialized.
a) True
b) False
Answer: a
Clarification: The type information and bytes that represent the object and its data can be used to recreate the object in memory.

3. Serialization is JVM independent.
a) True
b) False
Answer: a
Clarification: An object can be serialized on one platform and deserialized on an entirely different platform.

4. Classes which are high-level streams that contain the methods for serializing and deserializing an object.
a) ObjectInputStream
b) ObjectOutputStream
c) All of the mentioned
d) None of the mentioned
Answer: c
Clarification: Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object.

5. The ObjectOutputStream class contains methods for writing various data types.
a) public final void writeObject(Object x)
b) public final void write(Object x)
c) public final void writeToObject(Object x)
d) all of the mentioned
Answer: a
Clarification: The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out:
public final void writeObject(Object x) throws IOException

6. The ObjectInputStream class contains methods for writing various data types.
a) public final Object readObject()
b) public final Object read(Object x)
c) public final Object readFromObject(Object x)
d) all of the mentioned
Answer: a
Clarification: This method retrieves the next Object out of the stream and deserializes it. The return value is Object, so you will need to cast it to its appropriate data type.

7. For a class to be serialized successfully:
a) The class must implement the java.io.Serializable interface.
b) Every field in the class must be volatile.
c) All of the mentioned
d) None of the mentioned
Answer: a
Clarification: The class must implement the java.io.Serializable interface.
All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.

8. When serializing an object to a file, the standard convention in Java is to give the file a:
a) .war
b) .java
c) .ser
d) none of the mentioned
Answer: c
Clarification: When serializing an object to a file, the standard convention in Java is to give the file a .ser extension.

9. What will be the value of transient field after deserialization:-
a) 0
b) NULL
c) All of the mentioned
d) None of the mentioned
Answer: c
Clarification: It depends on the data type of field, 0 if int and NULL if String or Object.

10. If the class implements java.io.Serializable, then it is serializable; otherwise, it’s not.
a) True
b) False
Answer: a
Clarification: The class must implement the java.io.Serializable interface.


To practice tough questions on all areas of Java Spring,

250+ TOP MCQs on Property Editors and TaskExecutors in Spring and Answers

Java Spring Multiple Choice Questions & Answers (MCQs) on “Property Editors and TaskExecutors in Spring”.

1. Which of the following is used to convert property values to text Values?
a) property Editor
b) property setter
c) property getter
d) none of the mentioned
Answer: a
Clarification: A property editor is a feature of the JavaBeans API for converting property values to and from text values.

2. Which property editor is used to specify a URL String for a property of the URL type?
a) java.net.*
b) java.io.*
c) java.net.URL
d) none of the mentioned
Answer: c
Clarification: Spring will automatically convert the URL string into a URL object and inject it into your property.

3. Which interface is used to create your own property editors?
a) CustomEditorConfigurer
b) RegisterCustomEditor
c) PropertyEditorConfigurer
d) None of the mentioned
Answer: a
Clarification: The CustomEditorConfigurer is implemented as a bean factory post processor for you to register your custom property editors before any of the beans get instantiated.

4. Which of the following property editors are registered by Spring?
a) CustomNumberEditor
b) FileEditor
c) CustomDateEditor
d) CustomNetEditor
Answer: b
Clarification: ClassEditor, FileEditor, LocaleEditor, and URLEditor are pre registered by Spring, so you don’t need to register them again.

5. What are the ways to create custom Property Editors?
a) implement PropertyEditor interface
b) extend PropertyEditorSupport Class
c) none of the mentioned
d) all of the mentioned
Answer: d
Clarification: You can write custom property editors by implementing the java.beans.PropertyEditor interface or extending the convenient support class java.beans.PropertyEditorSupport.

6. Method which converts property into a string value
a) getAsText
b) setAsText
c) regText
d) None of the mentioned
Answer: a
Clarification: The getAsText() method converts a property into a string value.

7. Method that converts string into a property value
a) getAsText
b) setAsText
c) regText
d) None of the mentioned
Answer: b
Clarification: The setAsText() method converts a string back into a property.

8. The property value is retrieved by:-
a) getValue method
b) setValue method
c) none of the mentioned
d) all of the mentioned
Answer: d
Clarification: The property value is retrieved and set by calling the getValue() and setValue() methods.

9. For a property editor to be searched correctly, it must be located in the same package as the target class, and the name must be Editor’s name
a) True
b) False
Answer: b
Clarification: The name must be target class name with Editor as its suffix.

10. Which package is used for periodic work
a) java.lang.Thread
b) java.util.TimerTask
c) java.util.Timer
d) java.util.concurrent
Answer: b
Clarification: Java 1.3 saw the introduction of java.util.TimerTask to support doing some sort of work periodically.

11. Which subinterface provides functionality for managing Threads and their events
a) ExecutorService
b) ThreadService
c) All of the mentioned
d) None of the mentioned
Answer: a
Clarification: ExecutorService, a subinterface, provides more functionality for managing threads and providing support for raising events to the threads, such as shutdown().

12. Method provided by ExectuorService which returns a Future < T >
a) submit
b) publish
c) addService
d) registerService
Answer: a
Clarification: ExecutorService, a subinterface, provides more functionality for managing threads and providing support for raising events to the threads, such as shutdown().

13. Which method provided by ExecutorService is used to check whether job is finished or cancelled
a) Future.isDone()
b) Future.isCancelled()
c) None of the mentioned
d) All of the mentioned
Answer: d
Clarification: You can call Future.isDone() or Future.isCancelled() to determine whether the job is finished or cancelled, respectively.

14. The Quartz integration and message driven POJO container doesn’t needs TaskExecutor Services
a) True
b) False
Answer: b
Clarification: The TaskExecutor interface is used quite a bit internally in the Spring framework.

15. Which of the following class’s instance is used by TimerTaskExecutor for managing jobs
a) java.util.Timer
b) java.util.Date
c) java.util.HashMap
d) none of the mentioned
Answer: a
Clarification: TimerTaskExecutor uses a java.util.Timer instance and manages jobs (java.util.concurrent.Callable or java.lang.Runnable instances) for you by running them on the Timer.