Java MCQ Set 1
1. JdbcTemplate that takes advantage of Java 1.5 features such as auto-boxing, generics, and variable-length arguments to simplify its usage.
a) org.springframework.jdbc.core.JdbcTemplate
b) org.springframework.jdbc.core.simple.SimpleJdbcTemplate
c) org.springframework.jdbc.*
d) none of the mentioned
Answer
Answer: b [Reason:] org.springframework.jdbc.core.simple.SimpleJdbcTemplate is an evolution of JdbcTemplate that takes advantage of Java 1.5 features such as auto-boxing, generics, and variable-length arguments to simplify its usage.
2. JdbcTemplate require statement parameters to be passed as an object array.
a) True
b) False
Answer
Answer: a [Reason:] In SimpleJdbcTemplate, they can be passed as variable-length arguments; this saves you the trouble of wrapping them in an array.
3. To use SimpleJdbcTemplate:-
a) instantiate it directly
b) retrieve its instance by extending the SimpleJdbcDaoSupport class
c) all of the mentioned
d) none of the mentioned
Answer
Answer: c [Reason:] To use SimpleJdbcTemplate, you can either instantiate it directly or retrieve its instance by extending the SimpleJdbcDaoSupport class.
4. SimpleJdbcTemplate offers a convenient batch update method in the form of:-
a) Vector
b) Set
c) Map
d) List
Answer
Answer: d [Reason:] SimpleJdbcTemplate offers a convenient batch update method for you to specify a SQL statement and a batch of parameters in the form of List
5. Method has a warning from the Java compiler because of an unchecked conversion from List to List
a) findAll()
b) query()
c) update()
d) batchUpdate()
Answer
Answer: a [Reason:] When using the classic JdbcTemplate, the findAll() method has a warning from the Java compiler because of an unchecked conversion from List to List
6. The return type of the queryForObject() method will be determined by the class argument (e.g., String.class).
a) True
b) False
Answer
Answer: a [Reason:] So, there’s no need for you to perform type casting manually.
7. Named SQL parameters are specified by name (starting with a colon) rather than by position.
a) True
b) False
Answer
Answer: a [Reason:] Another option when binding SQL parameters in the Spring JDBC framework is to use named parameters.
8. Named parameters are supported only in SimpleJdbcTemplate.
a) True
b) False
Answer
Answer: a [Reason:] Named parameters are supported only in SimpleJdbcTemplate and NamedParameterJdbcTemplate.
9. Implementations of the SqlParameterSource interface:-
a) MapSqlParameterSource
b) BeanPropertySqlParameterSource
c) none of the mentioned
d) all of the mentioned
Answer
Answer: d [Reason:] There are three implementations of the SqlParameterSource interface. The basic one is MapSqlParameterSource, which wraps a map as its parameter source.
10. The Spring framework offers a consistent data access exception-handling mechanism for its data access module.
a) True
b) False
Answer
Answer: a [Reason:] In general, all exceptions thrown by the Spring JDBC framework are subclasses of org.springframework.dao.DataAccessException, a type of RuntimeException that you are not forced to catch.
11. In your DAO methods, you neither need to surround the code with a try/catch block nor declare throwing an exception in the method signature.
a) True
b) False
Answer
Answer: a [Reason:] This is because DataAccessException (and therefore its subclasses, including DuplicateKeyException) is an unchecked exception that you are not forced to catch.
12. The direct parent class of DataAccessException is:-
a) RuntimeException
b) NestedRuntimeException
c) Exception
d) Throwable
Answer
Answer: b [Reason:] A core Spring exception class that wraps another exception in a RuntimeException.
13. Which concrete exception in the DataAccessException hierarchy should be thrown?
a) errorCode
b) SQLState properties of the caught SQLException
c) All of the mentioned
d) None of the mentioned
Answer
Answer: c [Reason:] As a DataAccessException wraps the underlying SQLException as the root cause, you can inspect the errorCode and SQLState properties with the following catch block.
14. Error code for The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint.
a) -1
b) 0
c) 23505
d) 1
Answer
Answer: a [Reason:] SQL State Message Text
23505 The statement was aborted because it would have caused a duplicate key value in a unique or primary.
15. How does the Spring JDBC framework know that state 23505 should be mapped to DuplicateKeyException?
a) error code
b) SQL state
c) all of the mentioned
d) none of the mentioned
Answer
Answer: c [Reason:] The error code and SQL state are database specific, which means different database products may return different codes for the same kind of error.
Java MCQ Set 2
1. Grails applications are equipped to support internationalization.
a) True
b) False
Answer
Answer: a [Reason:] By default, all Grails applications are equipped to support internationalization.
2. Where can you find series of *.properties files.
a) /grails-app/
b) /grails-app/i18n/
c) /grails-app/i18n/WEB-INF
d) /grails-app/i18n/WEB-APP
Answer
Answer: b [Reason:] Inside an application’s /grails-app/i18n/ folder, you can find series of *.properties files used to define messages in 12 languages.
3. *.properties files allow Grails applications to display messages.
a) True
b) False
Answer
Answer: a [Reason:] The values declared in these *.properties files allow Grails applications to display messages based on a user’s languages preferences or an application’s default language.
4. Which locale (i.e., from an internationalization properties file) to use for a user is based on?
a) /grails-app/conf/spring/resource.groovy file
b) user’s browser language preferences
c) all of the mentioned
d) none of the mentioned
Answer
Answer: c [Reason:] Grails determines which locale (i.e., from an internationalization properties file) to use for a user based
on two criteria:
• The explicit configuration inside an application’s /grails-app/conf/spring/
resource.groovy file
• A user’s browser language preferences
5. There is no default configuration present in an application’s resource.groovy file.
a) True
b) False
Answer
Answer: a [Reason:] Since the explicit configuration of an application’s locale takes precedence over a user’s browser language preferences, there is no default configuration present in an application’s resource.groovy file.
6. If you want to coerce users into using a specific language locale.
a) /grails-app/conf/spring/resource.groovy file
b) /grails-app/resource.groovy file
c) /grails-app/spring/resource.groovy file
d) none of the mentioned
Answer
Answer: a [Reason:] You should define an explicit configuration inside an application’s /grails-app/conf/spring/resource.groovy file, only if you want to coerce users into using a specific language locale.
7. JSP Tag which declares statements using the Groovy.
a) g:message
b) g:string
c) g:msg
d) none of the mentioned
Answer
Answer: a [Reason:] JSP page first imports the Groovy tag library and later declares statements using the Groovy g:message tag.
8. Tag can also be used in GSP pages views.
a) g:message
b) g:string
c) g:msg
d) none of the mentioned
Answer
Answer: a [Reason:] The g:message tag can also be used in GSP pages views, but note that it’s not necessary to import the tag library explicitly, since it’s available by default on all Grails GSP pages.
9. Grails uses RDBMS as a permanent storage system.
a) True
b) False
Answer
Answer: a [Reason:] Grails is designed to use a RDBMS as a permanent storage system.
10. Grails comes preconfigured to use:-
a) MySql
b) PostgreSQL
c) Access
d) HSQLDB
Answer
Answer: d [Reason:] HSQLDB is a database that is automatically started by Grails upon deploying an application (i.e., executing grails run-app).
11. HSQLDB is a non-volatile database.
a) True
b) False
Answer
Answer: b [Reason:] Every time an application is restarted in development and testing environments, HSQLDB loses all its data since it’s configured to operate in memory.
12. Configure Grails to use another RDBMS by modifying:-
a) Data.groovy
b) DataSource.groovy
c) All of the mentioned
d) None of the mentioned
Answer
Answer: c [Reason:] You can configure Grails to use another RDBMS by modifying an application’s DataSource.groovy file, located under the grails-app/conf directory.
13. Grails relies on the standard Java JDBC notation to specify RDBMS.
a) True
b) False
Answer
Answer: a [Reason:] Grails relies on the standard Java JDBC notation to specify RDBMS connection parameters, as well as on the corresponding JDBC drivers provided by each RDBMS vendor, to create, read, update, and delete information.
14. DataSource properties, Class name for the JDBC driver.
a) driverClassName
b) dataSource properties
c) all of the mentioned
d) none of the mentioned
Answer
Answer: a [Reason:] dataSource properties for configuring a RDBMS.:-
driverClassName Class name for the JDBC driver
15. Indicates whether to enable SQL logging.
a) driverClassName
b) dataSource properties
c) logSql
d) none of the mentioned
Answer
Answer: c [Reason:] dataSource properties for configuring a RDBMS.:-
logSql Indicates whether to enable SQL logging
Java MCQ Set 3
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
Answer: a [Reason:] 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
Answer: a [Reason:] 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
Answer: a [Reason:] 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
Answer: c [Reason:] 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
Answer: a [Reason:] 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
Answer: a [Reason:] 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
Answer: a [Reason:] 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
Answer: c [Reason:] 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
Answer: c [Reason:] 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
Answer: a [Reason:] The class must implement the java.io.Serializable interface.
Java MCQ Set 4
1. Support for deployment of traditional .war artifacts, enable use of some of the standard Java EE libraries, provide useful defaults for many de facto standard libraries, and provide fully integrated support for Spring Dynamic Modules.
a) Spring Dynamic Modules
b) Spring dm Server
c) Spring Tooling
d) All of the mentioned
Answer
Answer: b [Reason:] Use Spring dm Server, SpringSource’s tried and true OSGi–oriented server built on many technologies including Equinox and the Spring framework itself.
2. OSGi doesn’t solve framework concerns, instead focusing on infrastructure requirements for Java applications.
a) True
b) False
Answer
Answer: a [Reason:] Spring Dynamic Modules provides functionality that sits on top of those implementations, providing very powerful runtime sophistication for developers looking to produce and consume OSGi services in a Spring-friendly fashion.
3 Spring Dynamic Modules was, while powerful for those already invested in an OSGi platform, not the most natural accommodations for those trying to migrate large code into the OSGi environment, SpringSource created.
a) Spring Dynamic Modules
b) Spring dm Server
c) Spring Tooling
d) All of the mentioned
Answer
Answer: b [Reason:] SpringSource dm Server is a robust solution. There are several editions available.
4. SpringSource dm Server’s many advances focus on delivering a solution, and not just a framework, for delivering:-
a) Spring Tooling
b) Spring dm
c) OSGi
d) None of the mentioned
Answer
Answer: c [Reason:] SpringSource dm Server’s many advances focus on delivering a solution, and not just a framework, for delivering OSGi–based enterprise applications.
5. SpringSource dm Server provides value above and beyond a regular OSGi solution.
a) True
b) False
Answer
Answer: a [Reason:] It’s already well integrated.
6 OSGi–enabling all of these interwoven dependencies via the granular use of the Import-Package header would be tedious.
a) True
b) False
Answer
Answer: a [Reason:] Spring dm Server provides the ability to wholesale import an entire library and all packages therein to expedite the process.
7. SpringSource dm Server also allows you to bend the rules where necessary.
a) True
b) False
Answer
Answer: a [Reason:] For example, consider the application of an aspect using Spring’s AOP. This might require weaving of classes, which in the case of a pointcut that matches classes deployed across multiple bundles, would prove cumbersome. SpringSource dm Server can intervene on Spring’s behalf, propagating such changes across multiple bundles where necessary.
8. SpringSource dm Server works with several types of deployment formats:-
a) bundle
b) Java EE .war
c) platform archive
d) all of the mentioned
Answer
Answer: d [Reason:] SpringSource dm Server works with four types of deployment formats.
9. Application isolation is critical because it allows you to solve the issue of reconciliation of two services whose interfaces collide. You can use:-
a) .WAR
b) .XML
c) .PAR
d) All of the mentioned
Answer
Answer: c [Reason:] You can use a .PAR to isolate services within the deployment unit.
10. SpringSource dm Server provides the robustness needed to commoditize enterprise application development in an OSGi environment.
a) True
b) False
Answer
Answer: a [Reason:] For a really good reference, specifically on SpringSource dm Server, I (Josh Long) would humbly recommend you investigate my co-author’s in-depth treatment of the subject, Pro SpringSource dm Server, by Gary Mak and Daniel Rubio (Apress, 2009).
11. To begin with SpringSource dm Server but need a way to rapidly turnaround development.
a) Spring Dynamic Module
b) Spring Dynamic Server
c) SpringSource dm Server
d) None of the mentioned
Answer
Answer: c [Reason:] Use the SpringSource dm Server tooling available as part of SpringSource Tool Suite (STS).
12. SpringSource has provided solid tooling for Eclipse, called :-
a) Spring Dynamic Module
b) dm Server Tools
c) SpringSource dm Server
d) None of the mentioned
Answer
Answer: b [Reason:] SpringSource has provided solid tooling for Eclipse, called dm Server Tools, which facilitate executing applications directly in a development environment.
13. These tools—part of the broader SpringSource Tool Suite—are available as :-
a) plug-in
b) stand-alone environment
c) all of the mentioned
d) none of the mentioned
Answer
Answer: c [Reason:] These tools—part of the broader SpringSource Tool Suite—are available as either a plug-in or as a stand-alone environment.
Java MCQ Set 5
1. To create services and have those automatically installed in the registry.
a) Spring Dynamic Modules
b) Spring DM Server
c) Spring Tooling
d) None of the mentioned
Answer
Answer: a [Reason:] You can use Spring Dynamic Modules configuration schema to export a service. The service will be made available to other beans as well as other OSGi components.
2. Element to export the bean as an OSGi service, classified by the interface we specify.
a) osgix
b) osgi:service
c) all of the mentioned
d) none of the mentioned
Answer
Answer: b [Reason:] We’ll use the osgi:service element to export the bean as an OSGi service, classified by the interface we specify.
3.
<osgi:service interface="com.apress.springrecipes. osgi.helloworld.service.GreeterService"> <bean class="com.apress.springrecipes.osgi.helloworld. service.GreeterServiceImpl"/> </osgi:service>
These beans are proxies, some may load asynchronously or take a longer time to register.
a) True
b) False
Answer
Answer: a [Reason:] This implies that you may have timing issues to resolve in configuring your service. For this, Spring Dynamic Modules provides the depends-on attribute, which lets your bean wait for another bean.
4. An anonymous bean specified inside of the osgi:service element allows you to avoid cluttering the namespace.
a) True
b) False
Answer
Answer: a [Reason:] You can abbreviate the syntax by using an anonymous bean.
<osgi:service auto-export="all-classes" ref="greeterService"> <osgi:interfaces> <value>com.apress.springrecipes.osgi.helloworld.service.GreeterService</value> <value>com.apress.springrecipes.osgi.helloworld.service.GreetingRecorderService</value> </osgi:interfaces> </osgi:service>
5. The first, most direct connection to OSGi is the bean that’s created on your behalf when you export a service. This bean, an instance of:-
a) org.osgi.framework.ServiceRegistration
b) org.osgi.framework.Service
c) org.osgi.framework.ServiceRegister
d) none of the mentioned
Answer
Answer: a [Reason:] This bean, an instance of org.osgi.framework.ServiceRegistration, is in turn a delegate to the Spring bean you have defined.
6. By default, beans created in a Spring application context are global to the entire OSGi runtime.
a) True
b) False
Answer
Answer: a [Reason:] By default, beans created in a Spring application context are global to the entire OSGi runtime, including all clients that use it.
7. Sometimes, you may want to limit the visibility of a service so that multiple clients each get their own instance of the bean.
a) time
b) scope
c) attr
d) none of the mentioned
Answer
Answer: b [Reason:] Spring Dynamic Modules provides a clever use of the scope attribute here, allowing you to limit beans exported as services to the client, or service importer.
8. Spring Dynamic Modules also supports injection and manipulation of bundles themselves. An injected bundle is of type:-
a) org.osgi.framework.savedBundle
b) org.osgi.framework.Bundle
c) org.osgi.framework.BundleInstance
d) none of the mentioned
Answer
Answer: b [Reason:] The simplest use case is that you want to obtain an instance of the org.osgi.framework.Bundle class from an already loaded bundle in the system.
9. Once acquired, the Bundle can be interrogated to introspect information about the bundle itself, including any entries, its current state.
a) True
b) False
Answer
Answer: a [Reason:] Once acquired, the Bundle can be interrogated to introspect information about the bundle itself, including any entries, its current state (which can be any of: UNINSTALLED, INSTALLED, RESOLVED, STARTING, STOPPING, or ACTIVE), and any headers specified on the bundle.
10. OSGi will let you maintain multiple versions of a service in your registry at the same time.
a) True
b) False
Answer
Answer: a [Reason:] OSGi, and Spring Dynamic Modules on top of it, provides many tools for discriminating services both in publishing and consuming services.
11. Multiple services of the same interface may be registered inside of an OSGi environment, which necessitates a conflict-resolution process.
a) True
b) False
Answer
Answer: a [Reason:] To aid in that, Spring Dynamic Modules provides a few features to help.
12. When specified on a service element, allows the ascription of a rank relative to other beans with the same interface.
a) Ranking
b) Service
c) All of the mentioned
d) None of the mentioned
Answer
Answer: a [Reason:] Ranking is specified on the service element itself. When encountered, the OSGi runtime will return the service with the highest-ranking integer value.
13. A more robust solution to service discrimination is service attributes.
a) True
b) False
Answer
Answer: a [Reason:] A service attribute is an arbitrary key and value pair that the service exports, and on which lookups for a service with multiple matches can be predicated.
14. There are frequently situations where OSGi will return more than one instance of a service that satisfies the interface.
a) True
b) False
Answer
Answer: a [Reason:] Take, for example, a deployment where you have multiple GreeterServices deployed for various regions. Spring Dynamic Modules provides the set and list interfaces to retrieve multiple references and populate a java.util.Set and java.util.List, respectively.
15. Attribute, which, is the symbolic name of another bundle.
a) Fragment
b) Fragment-Host
c) All of the mentioned
d) None of the mentioned
Answer
Answer: b [Reason:] The Fragment-Host attribute, in turn, is the symbolic name of another bundle.