Java MCQ Set 1
1. Classes used to describe an application’s primary elements.
a) Domain Classes
b) Application
c) Context
d) View
Answer
Answer: a [Reason:] Domain classes are used to describe an application’s primary elements and characteristics.
2. In web applications, domain classes are generally the first things to be defined.
a) True
b) False
Answer
Answer: a [Reason:] Domain classes represent data that is saved for posterity—in a permanent storage system—so it interacts with controllers, as well as representing data displayed in views.
3. In Grails, domain classes are placed under:-
a) /grails-app/WEB-INF/
b) /grails-app/domain/
c) /grails-app/domain/WEB-APP
d) /grails-app/
Answer
Answer: b [Reason:] In Grails, domain classes are placed under the /grails-app/domain/ directory.
4. The creation of domain classes by executing a simple command which is:-
a) grails create-domain-class
b) grails create-domain-class domain class name
c) domain class name
d) none of the mentioned
Answer
Answer: b [Reason:] The creation of domain classes, like most other things in Grails, can be carried out by executing a simple command in the following form:
grails create-domain-class domain class name
5. The command domain class name in creation of domain classes generates a file named domain class name.groovy.
a) True
b) False
Answer
Answer: a [Reason:] It generates a skeleton domain class file named domain class name.groovy inside the /grails-app/domain/ directory.
6. Corresponding unit tests files are also generated for each domain class while creating domain classes.
a) True
b) False
Answer
Answer: a [Reason:] In addition, corresponding unit tests files are also generated for each domain class under an application’s test/unit directory.
7. Static field which defines constraints on the domain class.
a) static{}
b) static field{}
c) static constraint{}
d) static constraints{}
Answer
Answer: d [Reason:] static constraints = { }, defines constraints on the domain class.
8. Declaration name which indicates that object’s name field can’t be blank.
a) blank:false
b) blank:true
c) all of the mentioned
d) none of the mentioned
Answer
Answer: a [Reason:] The declaration name(blank:false) indicates that a Player object’s name field cannot be left blank.
9. A variety of constraints can be used to enforce a domain class’s structure.
a) True
b) False
Answer
Answer: a [Reason:] Under certain circumstances, if a constraint is too elaborate, it’s often incorporated within an application’s controller prior to creating an object of a certain domain class.
10. Command to generate the corresponding CRUD controller and views for an application’s domain class.
a) grails create-domain-class domain class name
b) grails create-domain-class
c) grails generate-all domain class name
d) none of the mentioned
Answer
Answer: c [Reason:] You can execute the following command to generate the corresponding CRUD controller and views for an application’s domain class:
grails generate-all domain class name.
11. Grails is capable of inspecting an application’s domain classes and generating the corresponding controllers and views.
a) True
b) False
Answer
Answer: a [Reason:] Grails is capable of inspecting an application’s domain classes and generating the corresponding controllers and views necessary to create, read, update, and delete instances belonging to an application’s domain classes.
12. Views corresponding to a controller class’s CRUD operations.
a) create.gsp
b) edit.gsp
c) list.gsp
d) all of the mentioned
Answer
Answer: d [Reason:] Four views are created corresponding to a controller class’s CRUD operations named create.gsp, edit.gsp, list.gsp, and show.gsp.
13. .gsp extension stands for:-
a) Groovy Server Pages
b) Groovy Service Pages
c) All of the mentioned
d) None of the mentioned
Answer
Answer: a [Reason:] .gsp extension stands for “Groovy Server Pages,” which is equivalent to JavaServer Pages except it uses Groovy to declare programmatic statements instead of Java.
14. Views are placed under which directory:-
a) app/views/
b) app/
c) grails-app/views/WEB-INF
d) grails-app/views/domain class
Answer
Answer: d [Reason:] These views are placed under an application’s grails-app/views/domain class directory.
15. Command to start the Grails application:-
a) grails run
b) grails start-app
c) grails run-app
d) none of the mentioned
Answer
Answer: c [Reason:] You can start the Grails application using grails run-app and work as an end user with the application.
Java MCQ Set 2
1. Which tag informs the spring container about the use of AspectJ annotation?
a) aop:aspectj-autowire
b) aop:aspectj-name
c) aop:aspectj-autoproxy
d) none of the mentioned
Answer
Answer: c [Reason:] To enable AspectJ annotation support in the Spring IoC container, you only have to define an empty XML element aop:aspectj-autoproxy in your bean configuration file.
2. Which of the following is advice supported by Aspect Annotation?
a) @Before
b) @After
c) @AfterReturning
d) All of the mentioned
Answer
Answer: d [Reason:] AspectJ supports five types of advice annotations: @Before, @After, @AfterReturning, @AfterThrowing, and @Around.
3. An advice is an action which comes into play at pointcuts.
a) True
b) False
Answer
Answer: b [Reason:] A pointcut is an expression to match a set of join points, while an advice is the action to take at a particular join point.
4. Which advice is executed once a joint point finishes?
a) @Before
b) @After
c) @AfterReturning
d) @AfterThrowing
Answer
Answer: b [Reason:] An after advice is executed after a join point finishes, whenever it returns a result or throws an exception abnormally.
5. Which advice is executed only when joint point returns or throws an exception?
a) @Before
b) @After
c) @AfterReturning
d) @AfterThrowing
Answer
Answer: c [Reason:] If you would like to perform logging only when a join point returns, you should replace the after advice with an after returning advice.
6. Which advice combines all advices into one?
a) @Before
b) @After
c) @AfterThrowing
d) None of the mentioned
Answer
Answer: d [Reason:] It gains full control of a join point, so you can combine all the actions of the preceding advices into one single advice. You can even control when, and whether, to proceed with the original join point execution.
7. An advice can access the current join point information by declaring an argument of type org.aspectj.lang.AdvicePoint in the advice method signature.
a) True
b) False
Answer
Answer: b [Reason:] An advice can access the current join point information by declaring an argument of type org.aspectj.lang.JoinPoint in the advice method signature.
8. Which interface is implemented to specify precedence of aspects?
a) Ordered
b) ApplicationAspect
c) AspectPointcut
d) None of the mentioned
Answer
Answer: a [Reason:] The precedence of aspects can be specified either by implementing the Ordered interface.
9. Alternative annotative way to specify precedence of aspects?
a) @Order
b) @Aspect
c) @PointCut
d) None of the mentioned
Answer
Answer: a [Reason:] The precedence of aspects can be specified either by implementing the Ordered interface or @Order Annotation.
10. Method which returns the highest priority of aspect’s join point?
a) getHighestPriority
b) getOrder
c) getHighOrder
d) getPriority
Answer
Answer: b [Reason:] The lower value returned by the getOrder() method represents higher priority.
11.
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterThrowing; @Aspect public class AfterThrowingExample { @AfterThrowing( pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()", throwing="ex") public void doRecoveryActions(DataAccessException e) { throw new IllegalArgumentException(); // ...[/expand] } }
a) Runtime Error
b) IllegalArgumentException
c) BeanCreation Exception
d) None of the mentioned
Answer
Answer: c [Reason:] The throwing keyword in pointcut annotation doesn’t matches with the method’s parameter’s exception.
12. Which instantiation model is supported by AspectJ?
a) perthis
b) pertarget
c) none of the mentioned
d) all of the mentioned
Answer
Answer: d [Reason:] Spring supports AspectJ’s perthis and pertarget instantiation models.
13. Which instantiation model is supported by AspectJ?
a) perthis
b) pertarget
c) none of the mentioned
d) all of the mentioned
Answer
Answer: c [Reason:] percflow, percflowbelow, and pertypewithin are not currently supported.
14. Which tag in XML is used to declare @Before advice’s method?
a) aop:before
b) aop:after
c) aop:afterthrow
d) None of the mentioned
Answer
Answer: a [Reason:] Before advice runs before a matched method execution. It is declared inside an aop:aspect using the aop:before element.
15. Which tag in XML is used to declare @Before advice’s method?
a) aop:before
b) aop:after-returning
c) aop:afterthrow
d) None of the mentioned
Answer
Answer: b [Reason:]- After returning advice runs when a matched method execution completes normally. It is declared inside an aop:aspect in the same way as before advice.
Java MCQ Set 3
1. Method used to process bean before initialization callback
a) scope
b) postProcessAfterInitialization()
c) postProcessBeforeInitialization()
d) it’s own constructor
Answer
Answer: c [Reason:] 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
Answer: c [Reason:] 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) destory method
c) none of the mentioned
d) all of the mentioned
Answer
Answer: a [Reason:] 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
Answer: a [Reason:] 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 aplication context, BeanPost Processors are registered using addBeanPostProcessors method
a) True
b) False
Answer
Answer: b [Reason:] 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
Answer: a [Reason:] 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
Answer: a [Reason:] 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
Answer: a [Reason:] 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
Answer: c [Reason:] 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
Answer: a [Reason:] 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
Answer: a [Reason:] 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
Answer: a [Reason:] 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 ued to publish your own custom event
a) contextPublish
b) publishEvent
c) applicationEventPublish
d) addEvent
Answer
Answer: b [Reason:] 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
Answer: d [Reason:] 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
Answer: d [Reason:] Web request events are handled by RequestHandledEvent.
Java MCQ Set 4
1. A bean can be requested by:-
a) getBean method
b) reference from another bean using autowiring, property etc
c) all of the mentioned
d) none of the mentioned
Answer
Answer: c [Reason:] When a bean is requested by the getBean() method or a reference from other beans, Spring will decide which bean instance should be returned according to the bean scope.
2. Which attribute is used to set the scope of the bean?
a) setScope
b) scope
c) getScope
d) none of the mentioned
Answer
Answer: b [Reason:] Scope attribute defines the scope of a bean.
3. Which one is the default scope of the beans?
a) Prototype
b) Session
c) Request
d) Singleton
Answer
Answer: d [Reason:] This unique bean instance will be returned for all subsequent getBean() calls and bean references.
4. Which scope creates a new bean instance each time when requested?
a) Singleton
b) Prototype
c) Session
d) Request
Answer
Answer: b [Reason:] Creates a new bean instance each time when requested.
5. Session Creates a single bean instance per HTTP request, only valid in the context of a web application?
a) True
b) False
Answer
Answer: b [Reason:] Session Creates a single bean instance per HTTP session, only valid in the context of a web application.
6. Which of the following are considered valid beans?
a) Singleton
b) Prototype
c) All of the mentioned
d) None of the mentioned
Answer
Answer: c [Reason:] In Spring 1.x, singleton and prototype are the only two valid bean scopes, and they are specified by the singleton attribute (i.e., singleton=”true” or singleton=”false”), not the scope attribute.
7.
public class ShoppingCart { private List<Product> items = new ArrayList<Product>(); public void addItem(Product item) { items.add(item); } public List<Product> getItems() { return items; } } <beans ...> <bean id="aaa" class="com.shop.Battery"> <property name="name" value="AAA" /> <property name="price" value="2.5" /> </bean> <bean id="cdrw" class="com.shop.Disc"> <property name="name" value="CD-RW" /> <property name="price" value="1.5" /> </bean> <bean id="dvdrw" class="com.shop.Disc"> <property name="name" value="DVD-RW" /> <property name="price" value="3.0" /> </bean> <bean id="shoppingCart" class="com.shop.ShoppingCart" /> </beans> import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Product aaa = (Product) context.getBean("aaa"); Product cdrw = (Product) context.getBean("cdrw"); Product dvdrw = (Product) context.getBean("dvdrw"); ShoppingCart cart1 = (ShoppingCart) context.getBean("shoppingCart"); cart1.addItem(aaa); cart1.addItem(cdrw); System.out.println("Shopping cart 1 contains " + cart1.getItems()); ShoppingCart cart2 = (ShoppingCart) context.getBean("shoppingCart"); cart2.addItem(dvdrw); System.out.println("Shopping cart 2 contains " + cart2.getItems()); } }
What will be the output?
a) Shopping cart 1 contains (AAA 2.5, CD-RW 1.5)
Shopping cart 2 contains (AAA 2.5, CD-RW 1.5, DVD-RW 3.0)
b) Shopping cart 1 contains (AAA 2.5, CD-RW 1.5)
Shopping cart 2 contains (DVD-RW 3.0)
c) BeanCreationException
d) None of the mentioned
Answer
Answer: a [Reason:] Since the defalut scope is singleton, so items added by first bean instantiated will be used again by same bean if instantiated again.
8. In above question if scope of shoppingCart neamd bean is prototype, then what will be the output?
What will be the output?
a) Shopping cart 1 contains (AAA 2.5, CD-RW 1.5)
Shopping cart 2 contains (AAA 2.5, CD-RW 1.5, DVD-RW 3.0)
b) Shopping cart 1 contains (AAA 2.5, CD-RW 1.5)
Shopping cart 2 contains (DVD-RW 3.0)
c) BeanCreationException
d) None of the mentioned
Answer
Answer: b [Reason:] Since the defalut scope is prototype, so items added by first bean instantiated will be removed by same bean if instantiated again, and will add new items listed.
9. Which inteface is used to perform initialization of beans?
a) InitializingBean
b) Disposbalebean
c) None of the mentioned
d) All of the mentioned
Answer
Answer: a [Reason:] Spring allows your bean to perform initialization callback methods afterPropertiesSet() by implementing the InitializingBean and interfaces.
10. Which inteface is used to perform destruction of beans?
a) InitializingBean
b) Disposbalebean
c) None of the mentioned
d) All of the mentioned
Answer
Answer: b [Reason:] Spring allows your bean to perform destroy callback methods destroy() by implementing the DisposableBean and interfaces.
11. Alternate way of initialization method is:-
a) init-method attribute
b) afterPropertiesSet
c) destroy-method attribute
d) none of the mentioned
Answer
Answer: a [Reason:] A better approach of specifying the initialization callback methods is by setting the init-method attributes in your bean declaration.
12. Alternate way of destruction method is:-
a) init-method attribute
b) afterPropertiesSet
c) destroy-method attribute
d) none of the mentioned
Answer
Answer: c [Reason:] A better approach of specifying the destroy callback methods is by setting the destroy-method attributes in your bean declaration.
13. Which annotation is used as a substitute of initialization method?
a) @PostConstruct
b) @PreDestroy
c) None of the mentioned
d) All of the mentioned
Answer
Answer: a [Reason:] Using JSR annotation.
14.Which annotation is used as a substitute of destroy method?
a) @PostConstruct
b) @PreDestroy
c) None of the mentioned
d) All of the mentioned
Answer
Answer: b [Reason:] Using JSR annotation.
15. Which configuration can be used for Dependency Injection?
a) XML Configuration
b) Annotation Configuration
c) Java Based Configuration
d) All of the mentioned
Answer
Answer: d [Reason:] All of the above mentioned can be used for Dependency Injection.
Java MCQ Set 5
1. To validate Java beans in a web application using annotations.
a) XML
b) Java Based
c) JAR-303 standard
d) All of the mentioned
Answer
Answer: d [Reason:] You can validate Java beans in a web application using annotations based on the JSR-303 standard.
2. JSR-303 or bean vaildation can access beans through annotations.
a) True
b) False
Answer
Answer: a [Reason:] JSR-303 or bean validation is a specification whose objective is to standardize the validation of Java beans through annotations.
3. For validating beans Spring supports.
a) ad-hoc technique
b) jsr-303
c) all of the mentioned
d) none of the mentioned
Answer
Answer: c [Reason:] This requires you to extend one of the Spring framework’s classes to create a validator class for a particular type of Java bean.
4. JSR-303 can’t access java beans directly.
a) True
b) False
Answer
Answer: b [Reason:] The objective of the JSR-303 standard is to use annotations directly in a Java bean class.
5. Annotation which indicates a field cannot be a null.
a) @NotNULL
b) @NotNull
c) All of the mentioned
d) None of the mentioned
Answer
Answer: b [Reason:] The @NotNull annotation, which indicates a field cannot be null .
6. Annotation used to indicate a field has to have a minimum of 2 characters.
a) @NotNull
b) @Size
c) @MaxSize
d) @size
Answer
Answer: b [Reason:] @Size annotation used to indicate a field has to have a minimum of 2 characters.
7. Annotation which receives a value in the form regexp=”+.[a-z]+”.
a) @Pattern
b) @EmailRecognizer
c) @Email
d) @Null
Answer
Answer: a [Reason:] @Pattern annotation receives a value in the form regexp=”+.[a-z]+”.
8. A field named validator is of type.
a) javax.validation.Validator
b) javax.validation.ValidatorFactory
c) javax.validation.ValidatorFactor
d) none of the mentioned
Answer
Answer: a [Reason:] The first addition to the controller is a field named validator of the type javax.validation.Validator.
9. Validator field is not assigned to any bean, but rather a factory class of the type.
a) javax.validation.Validator
b) javax.validation.ValidatorFactory
c) javax.validation.ValidatorFactor
d) none of the mentioned
Answer
Answer: b [Reason:] This is how JSR-303 validation works. The assignment process is done inside the controller’s constructor.
10. To hold any errors detected from validating the instance of beans.
a) javax.validation.Validator
b) javax.validation.ValidatorFactory
c) javax.validation.ValidatorFactor
b) javax.validation.ConstraintViolation
Answer
Answer: d [Reason:] Set of the type javax.validation.ConstraintViolation to hold any errors detected from validating the instance of the Person object.
11. To use JSR-303 bean validation in a web application, dependencies to be added in maven.
a) javax.validation
b) org.hibernate
c) all of the mentioned
d) none of the mentioned
Answer
Answer: c [Reason:] you must add above mentioned dependencies to an implementation to your CLASSPATH.
12. Spring doesn’t cope up with pdf and excel views.
a) True
b) False
Answer
Answer: b [Reason:] Spring integrates the generation of Excel and PDF files into its MVC framework.
13. Spring MVC supports generating Excel files using which of the following libraries.
a) Apache POI library
b) JExcelAPI library
c) All of the mentioned
d) None of the mentioned
Answer
Answer: c [Reason:] Spring MVC supports generating Excel files using either the Apache POI library (http://poi.apache.org/) or the JExcelAPI library (http://jexcelapi.sourceforge.net/).
14. The view classes for Excel view is/are:-
a) AbstractExcelView
b) AbstractJExcelView
c) None of the mentioned
d) All of the mentioned
Answer
Answer: d [Reason:] The corresponding view classes are AbstractExcelView and AbstractJExcelView.
15. Spring MVC supports generating PDF files using which of the following libraries.
a) iText library
b) iJText library
c) all of the mentioned
d) none of the mentioned
Answer
Answer: a [Reason:] PDF files are generated by the iText library (http://www.lowagie.com/iText/), and the corresponding view class is AbstractPdfView.