Java MCQ Number 01080

Java MCQ Set 1

1. Which of these class contains the methods print() & println()?
a) System
b) System.out
c) BUfferedOutputStream
d) PrintStream

Answer

Answer: d [Reason:] print() and println() are defined under the class PrintStream, System.out is the byte stream used by these methods .

2. Which of these methods can be used to writing console output?
a) print()
b) println()
c) write()
d) all of the mentioned

Answer

Answer: d [Reason:] None.

3. Which of these class is used to create an object whose character sequence is mutable?
a) String()
b) StringBuffer()
c) Both of the mentioned
d) None of the mentioned

Answer

Answer: b [Reason:] StringBuffer represents growable and writeable character sequence.

4. Which of these method of class StringBuffer is used to reverse sequence of characters?
a) reverse()
b) reverseall()
c) Reverse()
d) reverseAll()

Answer

Answer: a [Reason:] reverse() method reverses all characters. It returns the reversed object on which it was called.

5. Which of these classes are used by character streams output operations?
a) InputStream
b) Writer
c) ReadStream
d) InputOutputStream

Answer

Answer: b [Reason:] Character streams uses Writer and Reader classes for input & output operations.

6. Which of the following statement is correct?
a) reverse() method reverses all characters
b) reverseall() method reverses all characters
c) replace() method replaces first occurrence of a character in invoking string with another character
d) replace() method replaces last occurrence of a character in invoking string with another character

Answer

Answer: a [Reason:] reverse() method reverses all characters. It returns the reversed object on which it was called.

7. What is the output of this program?

  1.     class output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String a="hello i love java";
  6.             System.out.println(indexof('i')+" "+indexof('o')+" "+lastIndexof('i')+" "+lastIndexof('o') ));
  7.         }
  8.     }

a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 4 3 6 9

Answer

Answer: a [Reason:] indexof(‘c’) and lastIndexof(‘c’) are pre defined function which are used to get the index of first and last occurrence of
the character pointed by c in the given array.
Output:
$ javac output.java
$ java output
6 4 6 9

8. What is the output of this program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            StringBuffer s1 = new StringBuffer("Hello");
  6.            StringBuffer s2 = s1.reverse();
  7.            System.out.println(s2);
  8.         }
  9.     }

a) Hello
b) olleH
c) HelloolleH
d) olleHHello

Answer

Answer: b [Reason:] reverse() method reverses all characters. It returns the reversed object on which it was called.
Output:
$ javac output.java
$ java output
olleH

9. What is the output of this program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.            StringBuffer s1 = new StringBuffer("Hello World");
  6.            s1.insert(6 , "Good ");
  7.            System.out.println(s1);
  8.         }
  9.    }

a) HelloGoodWorld
b) HellGoodoWorld
c) HellGood oWorld
d) Hello Good World

Answer

Answer: d [Reason:] The insert() method inserts one string into another. It is overloaded to accept values of all simple types, plus String and Objects. Sting is inserted into invoking object at specified position. “Good ” is inserted in “Hello World” T index 6 giving “Hello Good World”.
output:
$ javac output.java
$ java output
Hello Good World

10. What is the output of this program?

  1.     class output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             char c[]={'a','1','b',' ','A','0'];
  6.             for (int i = 0; i < 5; ++i)
  7.             {
  8. 	        if(Character.isDigit(c[i]))
  9.                     System.out.println(c[i]" is a digit");
  10.                 if(Character.isWhitespace(c[i]))
  11.                    System.out.println(c[i]" is a Whitespace character");
  12.                 if(Character.isUpperCase(c[i]))
  13.                    System.out.println(c[i]" is an Upper case Letter");
  14.                 if(Character.isUpperCase(c[i]))
  15.                    System.out.println(c[i]" is a lower case Letter");
  16.                 i = i + 3;
  17.             }
  18.         }
  19.     }

a) a is a lower case Letter
is White space character
b) b is a lower case Letter
is White space characte
c) a is a lower case Letter
A is a upper case Letter
d) a is a lower case Letter
0 is a digit

Answer

Answer: a [Reason:] Character.isDigit(c[i]),Character.isUpperCase(c[i]),Character.isWhitespace(c[i]) are the function of library java.lang
they are used to find weather the given character is of specified type or not. They return true or false i:e Boolean variable.
Output:
$ javac output.java
$ java output
a is a lower case Letter
is White space character

Java MCQ Set 2

1. OSGi—which was formerly known as the:-
a) Open Services Gateway initiative
b) Open Source Gateway initiative
c) Open Services Gateway initialisation
d) None of the mentioned

Answer

Answer: a [Reason:] OSGi—which was formerly known as the Open Services Gateway initiative, though the name’s obsolete now—has its roots in the embedded space, where dynamic service provisioning is far more important than it is in the gridiron world of enterprise applications.

2. OSGi provides a layer on top of the JVM’s default class loader.
a) True
b) False

Answer

Answer: a [Reason:] The deployment unit for OSGi is a bundle, which is essentially a JAR with an augmented MANIFEST.MF.

3. On top of Spring Dynamic Modules, SpringSource built SpringSource dm Server, which is a server wired from top to bottom with OSGi and Spring.
a) True
b) False

Answer

Answer: a [Reason:] SpringSource dm Server supports dynamic deployment, enhanced tooling, HTTP, and native .war deployment.

4. OSGi is a framework.
a) True
b) False

Answer

Answer: b [Reason:] OSGi is a specification, not a framework. There are many implementations of the specification, just as there are many implementations of the Java EE specification.

5. User component models are:-
a) OSGi
b) Spring
c) All of the mentioned
d) None of the mentioned

Answer

Answer: b [Reason:] OSGi is not a user component model, like Spring or EJB 3. Instead, it sits below your components, providing life-cycle management for Java classes.

6. In OSGi, anything used by something else is a state.
a) True
b) False

Answer

Answer: b [Reason:] In OSGi, anything used by something else is a service. “Service” doesn’t imply any concrete inheritance.

7. “Service” doesn’t imply any:-
a) RPC
b) inheritance
c) transactional qualities
d) all of the mentioned

Answer

Answer: d [Reason:] “Service” doesn’t imply any concrete inheritance; it doesn’t imply transactional qualities, and it doesn’t imply RPC. It’s merely a class on whose concrete, black-box functionality and interface your class relies.

8. OSGi bundles are simply standard .jar files with customized MANIFESTs that OSGi consumes at runtime.
a) True
b) False

Answer

Answer: a [Reason:] The plug-in wraps the bnd command line tool.

9. Tool which dynamically interrogates classes for their imports and generates OSGi–compliant entries.
a) pnd
b) jndi
c) bnd
d) none of the mentioned

Answer

Answer: c [Reason:] We repeat it here mainly for illustrative purposes.

10. The plug-in produces OSGi–compliant bundles that work in any container.
a) True
b) False

Answer

Answer: c [Reason:] To read more on the plug-in itself, see http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html.

11. The Bundle-Activator directive describes to the OSGi environment, which class implements :-
a) BundleActivator
b) BundleActivate
c) All of the mentioned
d) None of the mentioned

Answer

Answer: a [Reason:] The Bundle-Activator directive describes to the OSGi environment, which class implements BundleActivator, and should be consulted when life cycle events occur.

12. When the bundle begins to load and start, it calls the start method of the:-
a) BundleActivator
b) Activator
c) All of the mentioned
d) None of the mentioned

Answer

Answer: b [Reason:] When the bundle begins to load and start, it calls the start method of the Activator.

13. To start using Spring to smooth over some of the minutiae of resource acquisition and to help build more reliable systems in an OSGi environment.
a) Spring DM server
b) Spring Dynamic Modules
c) All of the mentioned
d) None of the mentioned

Answer

Answer: b [Reason:] Use Spring Dynamic Modules to provide the integration. Spring Dynamic Modules is a framework on top of OSGi that works with any OSGi environment.

14. Spring Dynamic Modules scans our deployed bundles and loads an ApplicationContext (actually, the specific type of the ApplicationContext is OsgiBundleXmlApplicationContext) into memory based on an event, or a trigger.
a) True
b) False

Answer

Answer: a [Reason:] There are two ways to trigger this behavior. The first is to explicitly specify in the META-INF/MANIFEST.MF file the attribute Spring-Context, which allows you to override the default location it consults. Otherwise, by default, Spring Dynamic Modules will look for the XML file in the META-INF/spring directory of a bundle.

15. You’ll split your OSGi–specific Spring configuration and your plain-vanilla Spring configuration into:-
a) modulename-context.xml
b) modulename-osgi-context.xml
c) all of the mentioned
d) none of the mentioned

Answer

Answer: a [Reason:] Typically, you’ll split your OSGi–specific Spring configuration and your plain-vanilla Spring configuration into two different files, of the form: modulename-context.xml and modulename-osgi-context.xml.

Java MCQ Set 3

1. jBPM will expose beans using the:-
a) jBPM expression language
b) jBoss
c) Spring expression language
d) None of the mentioned

Answer

Answer: a [Reason:] jBPM will expose beans using the jBPM expression language, and you can then just reference them by name.

2. jBPM, and indeed most workflow engines, passivate state for you, allowing a process to wait on external events :-
a) True
b) False

Answer

Answer: a [Reason:] Indeed, because you’ve decomposed your process into a series of isolated steps, each of which contributes to the larger goal while remaining independently useful, you get the best of both worlds:
stateful processes and stateless scalability.

3.

<?xml version="1.0" encoding="UTF-8"?>
<beans >="http://www.springframework.org/schema/beans"
	>:tx="http://www.springframework.org/schema/tx" 
	>:p="http://www.springframework.org/schema/p"
	>:util="http://www.springframework.org/schema/util" 
	>:xsi="http://www.w3.org/2001/XMLSchema-instance"
	>:aop="http://www.springframework.org/schema/aop"
	>:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/util
	http://www.springframework.org/schema/util/spring-util-3.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	">
<import resource="jbpm4-context.xml"/>
<context:annotation-config/>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method propagation="REQUIRED" name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* 
	com.apress.springrecipes..jbpm4.*.*(..))"/>
</aop:config>
<util:list id="annotatedHibernateClasses">
<value>com.apress.springrecipes.jbpm.jbpm4.customers.Customer</value>
</util:list>
<bean id="customerService" class="com.apress.springrecipes.jbpm.jbpm4.customers.
	CustomerServiceImpl">
<property name="processDefinitions">
<list>
<value>/process-definitions/RegisterCustomer.jpdl.xml</value>
</list>
</property>
        </bean>
</beans>

We override the List bean (with id annotatedHibernateClasses) that we created for the last recipe (jbpm4 context.xml) to provide the session factory with a collection of annotated entities which is here as:-
a) HibernateCustom
b) Customer
c) All of the mentioned
d) None of the mentioned

Answer

Answer: b [Reason:] This bean leverages Hibernate (through the HibernateTemplate instance) to handle persistence, and it leverages jBPM (through the SpringConfiguration instance) to handle BPM.

4. The method annotated with @PostConstruct will be run after the bean’s been configured to let the user inject custom initialization logic.
a) True
b) False

Answer

Answer: a [Reason:] The bean handles as part of its duties in its post-initialization phase.

5. The business process file’s name needs to end in :-
a) jpdl
b) xml
c) jpdl.xml
d) none of the mentioned

Answer

Answer: c [Reason:] The business process file’s name needs to end in jpdl.xml; otherwise, jBPM won’t deploy it.

6. At the top, we’ve injected some dependencies:
a) springConfiguration
b) repositoryService
c) executionService
d) all of the mentioned

Answer

Answer: d [Reason:] At the top, we’ve injected three dependencies:
springConfiguration (which doesn’t get used, though its configuration is worth noting because you may use it to access other services), repositoryService, and executionService.

7.

package com.apress.springrecipes.jbpm.jbpm4.customers;
public interface CustomerService {
void sendWelcomeEmail(Long customerId);
void deauthorizeCustomer(Long customerId);
void authorizeCustomer(Long customerId);
Customer getCustomerById(Long customerId);
Customer createCustomer(String email, String password, String firstName, 
String lastName);
void sendCustomerVerificationEmail(Long customerId);
}

The class(CustomerServiceImpl) provides a few salient methods:-
a) void setupProcessDefinitions()
b) Customer createCustomer(String email, String passphrase, String firstName, String lastName)
c) void sendCustomerVerificationEmail(Long customerId)
d) all of the mentioned

Answer

Answer: d [Reason:] The class provides a few
salient methods (some of which are required by its interface, CustomerService):
• void setupProcessDefinitions()
• Customer createCustomer(String email, String passphrase, String firstName,
String lastName)
• void sendCustomerVerificationEmail(Long customerId)
• void authorizeCustomer(Long customerId)

8. In the bean, setupProcessDefinitions is run when the bean is created.
a) True
b) False

Answer

Answer: a [Reason:] It iterates through the processDefinitions collection and deploys the resource whose path it is given.

9. Process definition will reference Spring beans using the JBoss expression language.
a) True
b) False

Answer

Answer: a [Reason:] We’ll walk through how the business process uses our customerService bean and how the customerService bean uses the business process to handle the customer’s sign-up.

10. In jBPM, a business process is built using jPDL.
a) True
b) False

Answer

Answer: a [Reason:] You can use the Eclipse plug-in to model jBPM processes, but the jPDL schema is so simple that you don’t really need it.

11.

<?xml version="1.0" encoding="UTF-8"?>
<process name="RegisterCustomer" >="http://jbpm.org/4.0/jpdl">
          <start>
          <transition to="send-verification-email" />
          </start>
          <java name="send-verification-email" expr="#{customerService}"
          method="sendCustomerVerificationEmail">
          <arg> <object expr="#{customerId}" /> </arg>
          <transition to="confirm-receipt-of-verification-email" />
          </java>
          <state name="confirm-receipt-of-verification-email">
          <transition to="send-welcome-email" />
          </state>
          <java name="send-welcome-email"
          expr="#{customerService}" method="sendWelcomeEmail">
          <arg> <object expr="#{customerId}" /> </arg>
          </java>
</process>

In the customerService bean, a client will use createCustomer to create a customer record.
a) True
b) False

Answer

Answer: a [Reason:] In a real-world example, you might imagine exposing these services as a SOAP endpoint to be consumed by various clients, such as a web application or other business applications.

12.

<?xml version="1.0" encoding="UTF-8"?>
<process name="RegisterCustomer" >="http://jbpm.org/4.0/jpdl">
          <start>
          <transition to="send-verification-email" />
          </start>
          <java name="send-verification-email" expr="#{customerService}"
          method="sendCustomerVerificationEmail">
          <arg> <object expr="#{customerId}" /> </arg>
          <transition to="confirm-receipt-of-verification-email" />
          </java>
          <state name="confirm-receipt-of-verification-email">
          <transition to="send-welcome-email" />
          </state>
          <java name="send-welcome-email"
          expr="#{customerService}" method="sendWelcomeEmail">
          <arg> <object expr="#{customerId}" /> </arg>
          </java>
</process>

Inside the createCustomer method, we use jBPM to start the business process to track the Customer. This is done with the :-
a) startProcessInstanceByKey
b) startProcessInstance
c) all of the mentioned
d) none of the mentioned

Answer

Answer: a [Reason:] In the invocation, we give jBPM variables through a Map instance (acting as something of a context for the process variables).

13. Once in the java element named send-verification-email, jBPM will invoke the method:-
a) sendCustomerVerificationEmail
b) sendCustomerVerification
c) veifyCustomerVerificationEmail
d) all of the mentioned

Answer

Answer: a [Reason:] Once in the java element named send-verification-email, jBPM will invoke the method sendCustomerVerificationEmail on the customerService bean in Spring.

14. Inside authorizeCustomer, the service queries the server for the any processes waiting at the:-
a) confirm-receipt-of-verification
b) confirm-receipt
c) confirm-receipt-of-verification-email
d) none of the mentioned

Answer

Answer: c [Reason:] Inside authorizeCustomer, the service queries the server for the any processes waiting at the confirm-receipt-of-verification-email state and having this customer’s ID.

15. The authorizeCustomer method also updates the Customer entity, marking it as authorized. From there, execution proceeds to the send-welcome-email java element.
a) True
b) False

Answer

Answer: a [Reason:] As before, the java element will be used to invoke a method on the customerService bean. This time, it will invoke sendWelcomeEmail to send the newly registered Customer a welcome e-mail.

Java MCQ Set 4

1. The ____ interface allows setting expectations for an associated expected invocation.
a) IMocksControl
b) IExpectationSetters<T>
c) IExpectation<T>
d) Isetters<T>

Answer

Answer: b [Reason:] The IexpectationSetters<T> interface is extended and allows setting expectation for the expected invocation.

2. _______ function expect the last invocation any times.
a) andStubReturn(T value)
b) andThrow(Throwable throwable)
c) anyTimes()
d) asStub()

Answer

Answer: c [Reason:] anyTimes() returns IExpectationSetters<T> object to allow method call chaining.

3. ________ sets stub behavior for the expected invocation.
a) andStubReturn(T value)
b) andThrow(Throwable throwable)
c) anyTimes()
d) asStub()

Answer

Answer: d [Reason:] Setting the stub behavior is needed for void methods.

4. ___ expect the last invocation between min and max times.
a) times(int count)
b) times(int min, int max)
c) once()
d) asStub()

Answer

Answer: b [Reason:] The max and min values are passed as arguments in the function definition times(int min, int max).

5. ______ expect the last invocation a specified number of times.
a) times(int count)
b) times(int min, int max)
c) once()
d) asStub()

Answer

Answer: a [Reason:] times (int count) specifies the number of times to expect the last invocation via the count argument.

6. _______ sets a stub object that will be used for the calculation of the answer for the expected invocation.
a) andAnswer(IAnswer<? extends T>answer)
b) andDelegateTo(Object delegateTo)
c) andStubAnswer(IAnswer<? extends T> answer)
d) andThrow(Throwable throwable)

Answer

Answer: c [Reason:] andStubAnswer(IAnswer<? extends T>answer) either returns a value, or throws an exception.

7. ___ sets a stub object implementing the same interface as the mock.
a) andAnswer(IAnswer<? extends T> answer)
b) andDelegateTo(Object delegateTo)
c) andStubAnswer(IAnswer<? extends T> answer)
d) andStubDelegateTo(Object delegateTo)

Answer

Answer: d [Reason:] The expected method call will be delegated to it with the actual arguments.

8. _______ records a call but returns nothing.
a) andVoid()
b) times(int min, int max)
c) once()
d) asStub()

Answer

Answer: a [Reason:] andVoid() is used to chain calls on void methods expectLastCall(), andThrow(e), andVoid().

9. _______ expects the last invocation once.
a) andVoid()
b) times(int min, int max)
c) once()
d) asStub()

Answer

Answer: c [Reason:] This is the default in EasyMock.

10. ___ sets a return value that will be returned for the expected invocation.
a) andReturn(T value)
b) times(int min, int max)
c) once()
d) asStub()

Answer

Answer: a [Reason:] The andReturn( T value) take as an argument value, which is the value to return.

Java MCQ Set 5

1. Improving the design of existing code is known as ____
a) Refactoring
b) Improving
c) Betterment
d) Upgrading

Answer

Answer: a [Reason:] Refactoring is the process of restructuring existing code, changing the design.

2. In what order is multiple @Before annotated methods run?
a) Oldest First
b) Newest First
c) Randomly
d) According to argument list

Answer

Answer: c [Reason:] It is not certain which @Before annotated method will run first as all run randomly.

3. The _____ method confirms that both references are to the same object.
a) assertEquals
b) assertEqual
c) assertEqualObject
d) assertSame

Answer

Answer: d [Reason:] assertSame uses the == operator to compare objects.

4. The assertEquals() method uses which underlying method to compare objects?
a) ==
b) equals()
c) isequal()
d) =

Answer

Answer: b [Reason:] The assertEquals() method makes use of equals() to compare objects, comparing identity.

5. The equals() method is inherited from which base class?
a) Object
b) String
c) Integer
d) org.junit.Equals

Answer

Answer: a [Reason:] The equals() method is inherited from the Objects class defined in java.lang.Object.

6. To use Parameterized.class with the @RunWith annotation, we need to import ____
a) org.junit.runners.Parameterized
b) org.junit.Parameterized
c) org.junit.runners.Parameterized
d) org.runners.Parameterized

Answer

Answer: a [Reason:] The Parameterized.class is a part of the runner class of JUnit.

7. To avoid overdesigning an interface, usually ______ is used.
a) Agile Development
b) Test Driven Development
c) Base Design Development
d) Waterfall Development

Answer

Answer: b [Reason:] Since test driven development is reliant on a very short development cycle being repeated, it is used to avoid overdesigning an interface.

8. The ___ is a sequential or non-iterative design process, which is used in software development processes, in which progress is seen as flowing steadily downwards.
a) Waterfall Model
b) Iterative Model
c) Spiral Model
d) Big Bang Model

Answer

Answer: a [Reason:] In a waterfall model, each phase or part must be completed before the next phase can begin and there is no overlapping between the phases.

9. Controller logic component accesses the ________ logic component.
a) Class
b) Object
c) Business
d) Criteria

Answer

Answer: c [Reason:] Business logic component is responsible for the business logic and delegates data access.

10. Overuse of argument ___ can lead to fragile tests.
a) Parameters
b) Objects
c) Classes
d) Captors

Answer

Answer: d [Reason:] The system under test is no longer a black box.

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.