Java MCQ Set 1
1. There are two main options for scheduling tasks on the Java platform:-
a) JDK Timer
b) Quartz Scheduler
c) All of the mentioned
d) None of the mentioned
Answer
Answer: c [Reason:] JDK Timer offers simple task scheduling features that you can use conveniently because the features are bundled with JDK. Compared with JDK Timer, Quartz offers more powerful job scheduling features.
2. Spring supports JMX by allowing you to export any beans in its IoC container as model MBeans.
a) True
b) False
Answer
Answer: a [Reason:] This can be done simply by declaring an MBeanExporter instance.
3. Connectors to expose your MBeans for remote access over a specific protocol by using a factory bean.
a) MBeanExporter
b) JSR-160
c) All of the mentioned
d) None of the mentioned
Answer
Answer: c [Reason:] In addition, Spring enables you to declare JSR-160 (Java Management Extensions Remote API) connectors to expose your MBeans for remote access over a specific protocol by using a factory bean. Spring provides factory beans for both servers and clients.
4. Spring can also detect and export your MBeans automatically from beans declared in the IoC container and annotated with JMX-specific annotations defined by Spring.
a) True
b) False
Answer
Answer: a [Reason:] The MBeanExporter class exports beans, delegating to an instance of MBeanInfoAssembler to do the heavy lifting.
5. Method, all files in the source directory will be replicated to the destination directory.
a) replicate
b) copy
c) rep
d) none of the mentioned
Answer
Answer: a [Reason:] Each time you call the replicate() method, all files in the source directory will be replicated to the destination directory. To avoid unexpected problems caused by concurrent replication, you declare this method as synchronized.
6.
import java.lang.management.ManagementFactory; import javax.management.Descriptor; import javax.management.JMException; import javax.management.MBeanServer; import javax.management.ObjectName; import javax.management.modelmbean.DescriptorSupport; import javax.management.modelmbean.InvalidTargetObjectTypeException; import javax.management.modelmbean.ModelMBeanAttributeInfo; import javax.management.modelmbean.ModelMBeanInfo; import javax.management.modelmbean.ModelMBeanInfoSupport; import javax.management.modelmbean.ModelMBeanOperationInfo; import javax.management.modelmbean.RequiredModelMBean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {<code></code> public static void main(String[] args) throws IOException { ApplicationContext context = new ClassPathXmlApplicationContext("beans-jmx.xml"); FileReplicator documentReplicator = (FileReplicator) context.getBean("documentReplicator"); try { MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName objectName = new ObjectName("bean:name=documentReplicator"); RequiredModelMBean mbean = new RequiredModelMBean(); mbean.setManagedResource(documentReplicator, "objectReference"); Descriptor srcDirDescriptor = new DescriptorSupport(new String[] { "name=SrcDir", "descriptorType=attribute", "getMethod=getSrcDir", "setMethod=setSrcDir" }); ModelMBeanAttributeInfo srcDirInfo = new ModelMBeanAttributeInfo( "SrcDir", "java.lang.String", "Source directory", true, true, false, srcDirDescriptor); Descriptor destDirDescriptor = new DescriptorSupport(new String[] { "name=DestDir", "descriptorType=attribute", "getMethod=getDestDir", "setMethod=setDestDir" }); public static void main(String[] args) throws IOException { ApplicationContext context = new ClassPathXmlApplicationContext("beans-jmx.xml"); FileReplicator documentReplicator = (FileReplicator) context.getBean("documentReplicator"); ModelMBeanAttributeInfo destDirInfo = new ModelMBeanAttributeInfo( "DestDir", "java.lang.String", "Destination directory", true, true, false, destDirDescriptor); ModelMBeanOperationInfo getSrcDirInfo = new ModelMBeanOperationInfo( "Get source directory", FileReplicator.class.getMethod("getSrcDir")); ModelMBeanOperationInfo setSrcDirInfo = new ModelMBeanOperationInfo( "Set source directory", FileReplicator.class.getMethod("setSrcDir", String.class)); ModelMBeanOperationInfo getDestDirInfo = new ModelMBeanOperationInfo( "Get destination directory", FileReplicator.class.getMethod("getDestDir")); ModelMBeanOperationInfo setDestDirInfo = new ModelMBeanOperationInfo( "Set destination directory", FileReplicator.class.getMethod("setDestDir", String.class)); ModelMBeanOperationInfo replicateInfo = new ModelMBeanOperationInfo( "Replicate files", FileReplicator.class.getMethod("replicate")); ModelMBeanInfo mbeanInfo = new ModelMBeanInfoSupport( "FileReplicator", "File replicator", new ModelMBeanAttributeInfo[] { srcDirInfo, destDirInfo }, null, new ModelMBeanOperationInfo[] { getSrcDirInfo, setSrcDirInfo, getDestDirInfo, setDestDirInfo, replicateInfo }, null); mbean.setModelMBeanInfo(mbeanInfo); mbeanServer.registerMBean(mbean, objectName); } catch (JMException e) { ... } catch (InvalidTargetObjectTypeException e) { ... } catch (NoSuchMethodException e) { ... } System.in.read(); } }
To register an MBean, you need an instance of the interface :-
a) javax.managment.MBeanServer
b) javax.managment
c) javax.managment.MBean
d) none of the mentioned
Answer
Answer: a [Reason:] To register an MBean, you need an instance of the interface javax.managment.MBeanServer.
7.
import java.lang.management.ManagementFactory; import javax.management.Descriptor; import javax.management.JMException; import javax.management.MBeanServer; import javax.management.ObjectName; import javax.management.modelmbean.DescriptorSupport; import javax.management.modelmbean.InvalidTargetObjectTypeException; import javax.management.modelmbean.ModelMBeanAttributeInfo; import javax.management.modelmbean.ModelMBeanInfo; import javax.management.modelmbean.ModelMBeanInfoSupport; import javax.management.modelmbean.ModelMBeanOperationInfo; import javax.management.modelmbean.RequiredModelMBean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws IOException { ApplicationContext context = new ClassPathXmlApplicationContext("beans-jmx.xml"); FileReplicator documentReplicator = (FileReplicator) context.getBean("documentReplicator"); try { MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName objectName = new ObjectName("bean:name=documentReplicator"); RequiredModelMBean mbean = new RequiredModelMBean(); mbean.setManagedResource(documentReplicator, "objectReference"); Descriptor srcDirDescriptor = new DescriptorSupport(new String[] { "name=SrcDir", "descriptorType=attribute", "getMethod=getSrcDir", "setMethod=setSrcDir" }); ModelMBeanAttributeInfo srcDirInfo = new ModelMBeanAttributeInfo( "SrcDir", "java.lang.String", "Source directory", true, true, false, srcDirDescriptor); Descriptor destDirDescriptor = new DescriptorSupport(new String[] { "name=DestDir", "descriptorType=attribute", "getMethod=getDestDir", "setMethod=setDestDir" }); public static void main(String[] args) throws IOException { ApplicationContext context = new ClassPathXmlApplicationContext("beans-jmx.xml"); FileReplicator documentReplicator = (FileReplicator) context.getBean("documentReplicator"); ModelMBeanAttributeInfo destDirInfo = new ModelMBeanAttributeInfo( "DestDir", "java.lang.String", "Destination directory", true, true, false, destDirDescriptor); ModelMBeanOperationInfo getSrcDirInfo = new ModelMBeanOperationInfo( "Get source directory", FileReplicator.class.getMethod("getSrcDir")); ModelMBeanOperationInfo setSrcDirInfo = new ModelMBeanOperationInfo( "Set source directory", FileReplicator.class.getMethod("setSrcDir", String.class)); ModelMBeanOperationInfo getDestDirInfo = new ModelMBeanOperationInfo( "Get destination directory", FileReplicator.class.getMethod("getDestDir")); ModelMBeanOperationInfo setDestDirInfo = new ModelMBeanOperationInfo( "Set destination directory", FileReplicator.class.getMethod("setDestDir", String.class)); ModelMBeanOperationInfo replicateInfo = new ModelMBeanOperationInfo( "Replicate files", FileReplicator.class.getMethod("replicate")); ModelMBeanInfo mbeanInfo = new ModelMBeanInfoSupport( "FileReplicator", "File replicator", new ModelMBeanAttributeInfo[] { srcDirInfo, destDirInfo }, null, new ModelMBeanOperationInfo[] { getSrcDirInfo, setSrcDirInfo, getDestDirInfo, setDestDirInfo, replicateInfo }, null); mbean.setModelMBeanInfo(mbeanInfo); mbeanServer.registerMBean(mbean, objectName); } catch (JMException e) { ... } catch (InvalidTargetObjectTypeException e) { ... } catch (NoSuchMethodException e) { ... } System.in.read(); } }
The static method to locate a platform MBean server.
a) ManagementFactory.getPlatform()
b) ManagementFactory.getPlatformMBeanServer()
c) ManagementFactory.getPlatformMBean()
d) All of the mentioned
Answer
Answer: b [Reason:] It will create an MBean server if none exists and then register this server instance for future use.
8.
import java.lang.management.ManagementFactory; import javax.management.Descriptor; import javax.management.JMException; import javax.management.MBeanServer; import javax.management.ObjectName; import javax.management.modelmbean.DescriptorSupport; import javax.management.modelmbean.InvalidTargetObjectTypeException; import javax.management.modelmbean.ModelMBeanAttributeInfo; import javax.management.modelmbean.ModelMBeanInfo; import javax.management.modelmbean.ModelMBeanInfoSupport; import javax.management.modelmbean.ModelMBeanOperationInfo; import javax.management.modelmbean.RequiredModelMBean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws IOException { ApplicationContext context = new ClassPathXmlApplicationContext("beans-jmx.xml"); FileReplicator documentReplicator = (FileReplicator) context.getBean("documentReplicator"); try { MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName objectName = new ObjectName("bean:name=documentReplicator"); RequiredModelMBean mbean = new RequiredModelMBean(); mbean.setManagedResource(documentReplicator, "objectReference"); Descriptor srcDirDescriptor = new DescriptorSupport(new String[] { "name=SrcDir", "descriptorType=attribute", "getMethod=getSrcDir", "setMethod=setSrcDir" }); ModelMBeanAttributeInfo srcDirInfo = new ModelMBeanAttributeInfo( "SrcDir", "java.lang.String", "Source directory", true, true, false, srcDirDescriptor); Descriptor destDirDescriptor = new DescriptorSupport(new String[] { "name=DestDir", "descriptorType=attribute", "getMethod=getDestDir", "setMethod=setDestDir" }); public static void main(String[] args) throws IOException { ApplicationContext context = new ClassPathXmlApplicationContext("beans-jmx.xml"); FileReplicator documentReplicator = (FileReplicator) context.getBean("documentReplicator"); ModelMBeanAttributeInfo destDirInfo = new ModelMBeanAttributeInfo( "DestDir", "java.lang.String", "Destination directory", true, true, false, destDirDescriptor); ModelMBeanOperationInfo getSrcDirInfo = new ModelMBeanOperationInfo( "Get source directory", FileReplicator.class.getMethod("getSrcDir")); ModelMBeanOperationInfo setSrcDirInfo = new ModelMBeanOperationInfo( "Set source directory", FileReplicator.class.getMethod("setSrcDir", String.class)); ModelMBeanOperationInfo getDestDirInfo = new ModelMBeanOperationInfo( "Get destination directory", FileReplicator.class.getMethod("getDestDir")); ModelMBeanOperationInfo setDestDirInfo = new ModelMBeanOperationInfo( "Set destination directory", FileReplicator.class.getMethod("setDestDir", String.class)); ModelMBeanOperationInfo replicateInfo = new ModelMBeanOperationInfo( "Replicate files", FileReplicator.class.getMethod("replicate")); ModelMBeanInfo mbeanInfo = new ModelMBeanInfoSupport( "FileReplicator", "File replicator", new ModelMBeanAttributeInfo[] { srcDirInfo, destDirInfo }, null, new ModelMBeanOperationInfo[] { getSrcDirInfo, setSrcDirInfo, getDestDirInfo, setDestDirInfo, replicateInfo }, null); mbean.setModelMBeanInfo(mbeanInfo); mbeanServer.registerMBean(mbean, objectName); } catch (JMException e) { ... } catch (InvalidTargetObjectTypeException e) { ... } catch (NoSuchMethodException e) { ... } System.in.read(); } }
From the preceding code, you can see that for each MBean attribute and MBean operation, you need to create a:-
a) ModelMBeanAttributeInfo
b) ModelMBeanOperationInfo
c) None of the mentioned
d) All of the mentioned
Answer
Answer: d [Reason:] From the preceding code, you can see that for each MBean attribute and MBean operation, you need to create a ModelMBeanAttributeInfo object and a ModelMBeanOperationInfo object for describing it.
9. VM argument to enable local monitoring of this application.
a) -Dcom.sun.management.jmxremote
b) -Dcom.sun.management
c) -Dcom.sun.management.jmx
d) None of the mentioned
Answer
Answer: a [Reason:] You have to add the VM argument -Dcom.sun.management.jmxremote to enable local monitoring of this application. You should also include all other options for your command, such as the classpath, as necessary.
Java –classpath … -Dcom.sun.management.jmxremote com.apress.springrecipes.replicator.Main
10. However, in an environment with no MBean server available, you have to create one explicitly using:-
a) MBeanServerFactoryBean
b) MBeanServerFactory
c) All of the mentioned
d) None of the mentioned
Answer
Answer: a [Reason:] To make your application portable to different runtime environments, you should enable the locateExistingServerIfPossible property so that this factory bean will create an MBean server only if none is available.
11. Spring allows you to create a JMX connector server through:-
a) ConnectorServerFactoryBean
b) ConnectorServer
c) ConnectorServerFactory
d) All of the mentioned
Answer
Answer: a [Reason:] By default, ConnectorServerFactoryBean creates and starts a JMX connector server bound to the service URL service:jmx:jmxmp://localhost:9875, which exposes the JMX connector through the JMX Messaging Protocol (JMXMP).
12.
<beans ...> ... <bean id="rmiRegistry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean" /> <bean id="connectorServer" class="org.springframework.jmx.support.ConnectorServerFactoryBean" depends-on="rmiRegistry"> <property name="serviceUrl" value=? /> </bean> </beans>
? should be replaced by:-
a) service:jmx:rmi://localhost/jndi/rmi://localhost:1099/replicator
b) service:jmx:rmi://localhost/jndi/rmi://localhost:1099/
c) service:jmx:rmi://localhost/jndi//localhost:1099/replicator
d) none of the mentioned
Answer
Answer: a [Reason:] To expose your JMX connector through a specific protocol, you just provide the service URL for it.
13. If no RMI registry has been created externally, you should create one by using:-
a) RmiRegistryFactory
b) RmiRegistryFactoryBean
c) RmiRegistry
d) RmiRegister
Answer
Answer: b [Reason:] If no RMI registry has been created externally, you should create one by using RmiRegistryFactoryBean. The default port for this registry is 1099, but you can specify another one in its port property.
14. The simplest MBean assembler in Spring is, which allows you to specify the names of the methods to export.
a) MethodNameBasedMBeanInfoAssembler
b) MethodNameBasedMBeanInfo
c) MethodNameBasedMBean
d) MethodNameBasedInfoAssembler
Answer
Answer: a [Reason:] The simplest MBean assembler in Spring is MethodNameBasedMBeanInfoAssembler, which allows you to specify the names of the methods to export.
15. Spring allows your beans to publish JMX notifications through:-
a) NotificationPublisher
b) Notification
c) Notify
d) All of the mentioned
Answer
Answer: a [Reason:] Spring allows your beans to publish JMX notifications through the NotificationPublisher interface. You can also register standard JMX notification listeners in the IoC container to listen to JMX notifications.
Java MCQ Set 2
1. To perform its logging operations.
a) Log4J
b) LogingJ
c) JLog
d) None of the mentioned
Answer
Answer: a [Reason:] Grails relies on Java Log4J to perform its logging operations.
2. Log4J configuration parameters are specified inside:-
a) Config.groovy
b) Conf.groovy
c) Log.groovy
d) None of the mentioned
Answer
Answer: a [Reason:] Log4J configuration parameters are specified inside the Config.groovy file.
3. Grails application logging can be configured using:-
a) creating custom appenders
b) logging levels
c) console output
d) all of the mentioned
Answer
Answer: d [Reason:] Given Log4J’s logging versatility, a Grails application logging can be configured in various ways. This includes creating custom appenders, logging levels, console output, logging by artifacts and custom logging layouts.
4. Any logging operation occurring at any of the cited packages will be logged.
a) True
b) False
Answer
Answer: a [Reason:] The notation follows the convention logging level ‘package name’. This implies that any logging operation occurring at any of the cited packages will be logged so long as it occurs within the specified logging level or a more severe level.
5. In Log4J parlance, each package is known as:-
a) parlance
b) logger
c) all of the mentioned
d) none of the mentioned
Answer
Answer: b [Reason:] In Log4J parlance, each package is known as a logger.
6. Log4J also has the following logging level:-
a) fatal
b) error
c) warning
d) all of the mentioned
Answer
Answer: d [Reason:] Log4J also has the following logging levels:
fatal, error, warn, info, debug, and trace. fatal is the most severe.
7. By default, all logging message are sent to the stacktrace.groovy file.
a) True
b) False
Answer
Answer: a [Reason:] By default, all logging message are sent to the stacktrace.log file located under an application’s root directory.
8. To offer versatile logging functionality:-
a) loggers
b) appenders
c) all of the mentioned
d) none of the mentioned
Answer
Answer: c [Reason:] Log4J relies on appenders and loggers to offer versatile logging functionality.
9. A location where logging information is sent.
a) appender
b) logger
c) all of the mentioned
d) none of the mentioned
Answer
Answer: a [Reason:] An appender is a location where logging information is sent (e.g., a file or standard output).
10. A location where logging information is generated.
a) appender
b) logger
c) all of the mentioned
d) none of the mentioned
Answer
Answer: b [Reason:] A logger is a location where logging information is generated (e.g., a class or package).
11. The default Log4J logger can be customized in a Grails application.
a) True
b) False
Answer
Answer: a [Reason:] The default Log4J logger can be customized in a Grails application using the following statement within the log4j { } section of an application’s Config.groovy file:
root {
error()
additivity = true
}
12. An appender that logs to a JDBC connection.
a) jdbc
b) console
c) file
d) rollingFile
Answer
Answer: a [Reason:] There are four types of appenders available by default:
• jdbc : An appender that logs to a JDBC connection
• console: An appender that logs to standard output
• file: An appender that logs to a file.
• rollingFile: An appender that logs to a rolling set of files
13. Command for running tests in Grails.
a) grails test-app
b) grails test
c) grails test-app domain class
d) none of the mentioned
Answer
Answer: a [Reason:] Running tests in Grails is as simple as executing the grails test-app command from an application’s root directory.
14. Unit tests are designed to validate the logic contained in a single domain class.
a) True
b) False
Answer
Answer: a [Reason:] Because of this fact, besides automating the execution of such tests, Grails provides no type of bootstrapping properties for performing these type of tests.
15. Method creates a mock object from a domain class.
a) mockForConstraintsTests
b) mockForConstraints
c) all of the mentioned
d) none of the mentioned
Answer
Answer: a [Reason:] This method creates a mock object from a domain class that is used to access a class’s dynamic methods (e.g., validate) needed to perform unit tests.
Java MCQ Set 3
1. Spring supports web flows by:-
a) Java
b) XML
c) All of the mentioned
d) None of the mentioned
Answer
Answer: c [Reason:] Spring supports defining a flow either by java or XML.
2. Spring manages UI as flows.
a) True
b) False
Answer
Answer: a [Reason:] Spring Web Flow allows you to model UI activities as flows.
3. Stated related to Spring Web flows.
a) view
b) action
c) decision
d) all of the mentioned
Answer
Answer: d [Reason:] Spring Web Flow builds in several state types, including view state, action state, decision state, subflow state, and end state.
4. Once a state has completed its tasks, it fires an event.
a) True
b) False
Answer
Answer: a [Reason:] An event contains a source and an event ID, and perhaps some attributes.
5. Required dependencies to manage web UI flows.
a) spring web flow distribution.
b) org.springframework.webflow
c) all of the mentioned
d) none of the mentioned
Answer
Answer: b [Reason:] To manage your web UI flows with Spring Web Flow, you neeed the Spring Web Flow distribution (e.g.,v2.0.8) on your CLASSPATH.
6. Spring Web flow supports.
a) Unified EL
b) Object-Graph Navigation Language (OGNL)
c) All of the mentioned
d) None of the mentioned
Answer
Answer: c [Reason:] Spring Web Flow supports using Unified EL (used in JSF 1.2 and JSP 2.1) and Object-Graph Navigation Language (OGNL), which is used in Tapestry, WebWork, and other frameworks, as its expression language.
7. Spring Web flow can enable both Unified EL and Object-Graph Navigation Language (OGNL).
a) True
b) False
Answer
Answer: b [Reason:] Spring Web Flow is able to detect the libraries of JBoss EL (as the default Unified EL implementation) and OGNL from the classpath. You can enable either of them (but not both) by including the corresponding JAR file in your classpath.
8. To create URL path mappings from the ids of registered flow definitions.
a) FlowHandlerMapping
b) FlowHandler
c) FlowHandlerMappingRequest
d) None of the mentioned
Answer
Answer: a [Reason:] The FlowHandlerMapping follows a convention to create URL path mappings from the ids of registered flow definitions.
9. XML Based flow definitions can be validated by:-
a) Spring Web Flow’s XSD
b) Spring IDE
c) SpringSource Tool Suite
d) All of the mentioned
Answer
Answer: d [Reason:] Spring Web Flow offers an XML–based flow definition language that can be validated by Spring Web Flow’s XSD and supported by Spring IDE or SpringSource Tool Suite (STS).
10. Element to trigger an action for view module.
a) on-render
b) on-action
c) on-trigger
d) none of the mentioned
Answer
Answer: a [Reason:] You can use the on-render element to trigger an action for a view state before its view renders.
11. Variable to trigger the flow execution.
a) ${flowExecutionUrl}
b) ${exec.flow}
c) ${flowExecutionUrl.match}
d) none of the mentioned
Answer
Answer: a [Reason:] You have to start the URLs with the variable ${flowExecutionUrl} to trigger a flow execution. This variable will be evaluated by Spring Web Flow at runtime.
12.Servers which listen onport 8080.
a) Apache Tomcat 6.0
b) Jetty
c) None of the mentioned
d) All of the mentioned
Answer
Answer: d [Reason:] By default, Tomcat and Jetty (and the Maven Jetty plug-in configured for this code) listen on port 8080.
13. Interface which is used to persist instances sessions.
a) serializable
b) file.io
c) io.file.*
d) none of the mentioned
Answer
Answer: a [Reason:] Implement the Serializable interface, as its instances may need to be persisted in sessions.
14. Class which assist Spring Web flow in handling form.
a) Form
b) FormAction
c) FormHandler
d) Form-Handler
Answer
Answer: b [Reason:] Spring Web Flow provides a FormAction class to assist you in handling forms.
15. Property for action to instantiate form objects.
a) formObjectClass
b) formClass
c) objectClass
d) none of the mentioned
Answer
Answer: a [Reason:] A form action can bind form fields to a form object’s properties of the same name. But you first have to specify the form object class in the formObjectClass property for this action to instantiate form objects.
Java MCQ Set 4
1. Class which represents a real-world entity and its instances will be persisted to a database.
a) entity
b) persistent
c) all of the mentioned
d) none of the mentioned
Answer
Answer: c [Reason:] Class is called an entity class or a persistent class because it represents a real-world entity and its instances will be persisted to a database.
2. Each entity class to be persisted by an ORM framework, a default constructor with no argument is required.
a) True
b) False
Answer
Answer: a [Reason:] For each entity class, you must define an identifier property to uniquely identify an entity.
3. If the identifier value is null, this entity will be treated as a new and unsaved entity.
a) True
b) False
Answer
Answer: a [Reason:] When this entity is persisted, an insert SQL statement will be issued; otherwise, an update statement will.
4. To allow the identifier to be null, you should choose a primitive wrapper type:-
a) java.lang.Integer
b) java.lang.Long
c) all of the mentioned
d) none of the mentioned
Answer
Answer: c [Reason:] To allow the identifier to be null, you should choose a primitive wrapper type like java.lang.Integer and java.lang.Long for the identifier.
5. A set of persistent annotations for you to define mapping metadata.
a) JPA
b) JSR
c) XML
d) None of the mentioned
Answer
Answer: a [Reason:] JPA defines a set of persistent annotations for you to define mapping metadata in a standard format that is more likely to be reusable in other ORM frameworks.
6. For mapping and persisting your objects with Hibernate and JPA.
a) Using the Hibernate API to persist objects with Hibernate XML mappings
b) Using the Hibernate API to persist objects with JPA annotations
c) Using JPA to persist objects with JPA annotations
d) All of the mentioned
Answer
Answer: d [Reason:] Hibernate also supports the use of JPA annotations to define mapping metadata, so there are
essentially three different strategies for mapping and persisting your objects with Hibernate and JPA:
• Using the Hibernate API to persist objects with Hibernate XML mappings
• Using the Hibernate API to persist objects with JPA annotations
• Using JPA to persist objects with JPA annotations
7. Core Programming Elements for Different Data Access Strategies.
a) Resource
b) Resource Factory
c) Session
d) All of the mentioned
Answer
Answer: d [Reason:] Core Programming Elements for Different Data Access Strategies
Concept JDBC Hibernate JPA
Resource Connection Session EntityManager
Resource factory DataSource SessionFactory EntityManagerFactory
Exception SQLException HibernateException PersistenceException
8. Interface whose instances can be obtained from a SessionFactory instance.
a) Session
b) Session Factory
c) All of the mentioned
d) None of the mentioned
Answer
Answer: a [Reason:] In Hibernate, the core interface for object persistence is Session, whose instances can be obtained from a SessionFactory instance.
9. Interface whose instances can be obtained from an EntityManagerFactory instance.
a) Entity
b) EntityManager
c) All of the mentioned
d) None of the mentioned
Answer
Answer: b [Reason:] In JPA, the corresponding interface is EntityManager, whose instances can be obtained from an EntityManagerFactory instance.
10. The exceptions thrown by Hibernate are of type HibernateException, while those thrown by JPA may be of type PersistenceException.
a) True
b) False
Answer
Answer: a [Reason:] Other Java SE exceptions like IllegalArgumentException and IllegalStateException.
11. To accept a session factory via dependency injection.
a) HibernateCourseDao
b) HibernateCourse
c) HibernateDao
d) All of the mentioned
Answer
Answer: a [Reason:] Modify HibernateCourseDao to accept a session factory via dependency injection, instead of creating it directly with the native Hibernate API in the constructor.
12. Property for factory bean to load the Hibernate configuration file.
a) config.xml
b) config
c) configLocation
d) none of the mentioned
Answer
Answer: c [Reason:] The configLocation property is of type Resource, but you can assign a string value to it.
13. If you want to use this data source for your session factory, you can inject it into the dataSource property of LocalSessionFactoryBean.
a) True
b) False
Answer
Answer: a [Reason:] The data source specified in this property will override the database settings in the Hibernate configuration file.
14. FactoryBean to create an entity manager factory in the IoC container.
a) LocalEntityManagerFactoryBean
b) LocalEntityManagerFactory
c) LocalEntityManager
d) All of the mentioned
Answer
Answer: a [Reason:] Spring provides a factory bean, LocalEntityManagerFactoryBean, for you to create an entity manager factory in the IoC container.
15. It allows you to override some of the configurations in the JPA configuration file.
a) LocalEntityManagerFactoryBean
b) LocalContainerEntityManagerFactoryBean
c) All of the mentioned
d) None of the mentioned
Answer
Answer: b [Reason:] Spring supports a more flexible way to create an entity manager factory by another factory bean, LocalContainerEntityManagerFactoryBean. It allows you to override some of the configurations in the JPA configuration file, such as the data source and database dialect.
Java MCQ Set 5
1. To quickly grid-enable a method on a bean using GridGain.
a) @Gridify
b) @Grid
c) @GridGain
d) None of the mentioned
Answer
Answer: a [Reason:] You can use GridGain’s @Gridify annotation along with some Spring AOP configuration to let GridGain know that it can parallelize the execution of the method across the grid.
2. GridGain provides:-
a) load balancing
b) fault tolerance
c) routing
d) all of the mentioned
Answer
Answer: d [Reason:] GridGain provides load balancing as well as fault tolerance and routing out of the box, which you get for free by adding this annotation.
3. To build a parallelized solution for a problem that’s intrinsically better-suited to parallelization or that, for want of resources, needs to be chunked.
a) map
b) reduce
c) all of the mentioned
d) none of the mentioned
Answer
Answer: c [Reason:] Use map/reduce to approach the problem concurrently.
4. GridGain works with a GridTask
a) Grid
b) GridGain
c) GridJob
d) All of the mentioned
Answer
Answer: c [Reason:] Underneath the hood, GridGain works with a GridTask
5. When you call the method on the service with the @Gridify annotation pointing to this GridTask implementation, it stops execution of method and loads an instance of this implementation.
a) True
b) False
Answer
Answer: a [Reason:] The parameters, as passed to the method with the annotation, are passed to: split(int i, final GridifyArgument gridifyArgument), which is used to dole out GridJob instances.
6. When using GridGain stem mostly from the fact that what you develop on one node can’t always automatically work on another node with no additional configuration.
a) True
b) False
Answer
Answer: a [Reason:] The parameters, as passed to the method with the annotation, are passed to: split(int i, final GridifyArgument gridifyArgument), which is used to dole out GridJob instances.
7. GridGain lets you start up nodes using the startup script in the:-
a) etc
b) opt
c) bin
d) all of the mentioned
Answer
Answer: c [Reason:] GridGain lets you start up nodes using the startup script in the bin directory of the distribution.
8. To hoist a grid node into existence.
a) GridLoader
b) GridLoad
c) Grid
d) GridGain
Answer
Answer: a [Reason:] The GridLoader’s job is to hoist a grid node into existence.
9. When you use the script that comes with the distribution is the class:-
a) GridCommandLine
b) GridCommandLineLoader
c) GridCommand
d) All of the mentioned
Answer
Answer: b [Reason:] The one that gets started when you use the script that comes with the distribution is the class GridCommandLineLoader.
10. A GridLoader instance is responsible for many things such as:-
a) GridFactory.start
b) GridFactory.stop
c) All of the mentioned
d) None of the mentioned
Answer
Answer: c [Reason:] A GridLoader instance is responsible for many things, not the least of which is correctly calling GridFactory.start and GridFactory.stop.
11. GridFactory.start can take as its first parameter a:-
a) GridConfiguration object
b) Spring application context
c) All of the mentioned
d) None of the mentioned
Answer
Answer: c [Reason:] This GridConfiguration object is what tells GridGain what is unique about a given node and the grid’s topology. By default, it uses $GRIDGAIN HOME/config/default-spring.xml, which, in turn, does things such as load a Grid object and configure user parameters about a specific node.
12. File which enables you to tell GridGain about which GridTask
a) gridgain.xml
b) gridgain.html
c) grid.xml
d) gridify.xml
Answer
Answer: a [Reason:] gridgain.xml file, which is optional, enables you to tell GridGain about which GridTask
13. Instances of the ApplicationContext can be injected into the various GridGain class instances (GridTask
a) @Grid
b) @GridGain
c) @Gridify
d) None of the mentioned
Answer
Answer: d [Reason:] Instances of the ApplicationContext can be injected into the various GridGain class instances (GridTask
14. This is the default implementation. It is used when you run gridgain.sh or gridgain.bat.
a) org.gridgain.grid.loaders.cmdline.GridCommandLineLoader
b) org.gridgain.grid.loaders.servlet.GridServletLoader
c) org.gridgain.grid.loaders.jboss.GridJbossLoader
d) org.gridgain.grid.loaders.weblogic.GridWeblogicStartup
Answer
Answer: a [Reason:] org.gridgain.grid.loaders.cmdline.GridCommandLineLoader This is the default implementation. It is used when you run gridgain.sh or gridgain.bat.
15. This is likely the second most useful implementation. It provides a servlet that bootstraps the GridGain instance inside any web container as a servlet.
a) org.gridgain.grid.loaders.cmdline.GridCommandLineLoader
b) org.gridgain.grid.loaders.servlet.GridServletLoader
c) org.gridgain.grid.loaders.jboss.GridJbossLoader
d) org.gridgain.grid.loaders.weblogic.GridWeblogicStartup
Answer
Answer: b [Reason:] org.gridgain.grid.loaders.servlet.GridServletLoader This is likely the second most useful implementation. It provides a servlet that bootstraps the GridGain instance inside any web container as a servlet.