Object Oriented
Programming using
Java
Course Code: MMC202
18
2018
WORKBOOK
Prof.Dhivya R
Assistant Professor
Dept of MCA, CMRIT
1
INDEX
Module # Module Name Page #
1. SERVLET 3
2. Introduction to JSP 9
3. Controlling the Structure of 15
generated servlets and Java Beans :
4. Annotations and JDBC : 24
5. EJB and Server Side Component 18
Models :
2
Module – 1 SERVLET
I MCQ
Q 1 - Which of the following is the correct order of servlet life cycle phase methods?
A - init(), service(), destroy()
B - initialize(), service(), destroy()
C - init(), execute(), destroy()
D - init(), service(), delete()
Q 2 - Which of the following method can be used to get the multiple values of a parameter like
checkbox data?
A - request.getParameter()
B - request.getParameterValues()
C - request.getParameterNames()
D - None of the above.
Q 3 - Which of the following code retrieves the request header?
A - Header.getHeaderName(headerName)
B - response.getHeader(headerName)()
C - request.getHeader(headerName)
D - None of the above.
Q 4 - Which of the following code can be used to send an error response to the client using
the specified status code and error message?
A - request.sendError(statusCode,message)
B - response.sendError(statusCode,message)
C - header.sendError(statusCode,message)
D - None of the above
3
Q 5 - Which of the following is true about init() method of filter?
A - The init() method simply creates or loads some data that will be used throughout the life of the
filter.
B - The init() method is not called again and again for each user request.
C - Both of the above.
D - None of the above.
Q 6 - Which of the following code is used to update an attribute in a HTTP Session object in
servlets?
A - session.setAttribute(name,value)
B - session.alterAttribute(name,value)
C - session.updateAttribute(name,value)
D - None of the above.
Q 7 - Which of the following code is used to set the session timeout in servlets?
A - session.setMaxInactiveInterval(interval)
B - response.setMaxInactiveInterval(interval)
C - request.setMaxInactiveInterval(interval)
D - None of the above.
Q 8 - Which of the following code is used to get a particular attribute in servlet?
A - request.getAttribute(name)
B - response.getAttribute(name)
C - new Attribute(name)
D - None of the above.
Q 9 - Which of the following code can be used to add a header?
A - request.addHeader(name,value)
B - response.addDateHeader(name,value)
C - Header.addDateHeader(name,value)
D - None of the above.
4
Q10 - Which of the following code is used to get a particular attribute in servlet?
A - request.getAttribute(name)
B - response.getAttribute(name)
C - new Attribute(name)
D - None of the above.
Q11. Which of the following code is used to delete an attribute from a HTTP Session object in
servlets?
A - session.removeAttribute(name)
B - session.alterAttribute(name)
C - session.updateAttribute(name)
D - None of the above.
Q12 Which of the following is true about HTTP Post method?
A - The POST method sends the encoded user information as a seperate message to page request.
B - The POST method is used to submit form data normally.
C - Both of the above.
D - None of the above.
Q13. Which of the following code retrieves the query string that is contained in the request
URL after the path?
A - Header.getQueryString()
B - response.getQueryString()
C - request.getQueryString()
D - None of the above.
Q14. Which of the following code retrieves the fully qualified name of the client making this
request?
A - request.getRemoteHost()
B - response.getRemoteHost()
C - Header.getRemoteHost()
5
D - None of the above.
II. Short Answer - One line code
1. Give the code to retrieve the MIME type of the body of the request?
__________________________________________
2. Give the code to get PrintWriter object in servlet
__________________________________________
3. Give the code which can be used to send an error response to the client using the specified
status code and clearing the buffer.
__________________________________________
4. Give the code can be used to set the length of content of body of the response?
__________________________________________
5. Give the code to create a cookie in servlet?
____________________________________
III. Short Answers
1. Define Servlet
6
2. What is cookie?
3. List the session tracking techniques in Java
4. Write four basic steps in session tracking
a.
b.
c.
d.
7
Complete the table with the appropriate
Sl.# Header/Status code Description
1. Host
2. Connection
3. Specifies the Client professed Languages
4. response.addCookie(ck)
5. Status code : 301
6. Status code : 403
7. Status code : 503
8. Set cookie age to as zero
9. a) List 2 packages represent interfaces and
classes for servlet api
b)
10. Provides a way to identify a user across more
than one page request or visit to a Web site
and to store information about that user
11. public String getServletInfo()
12. List any 3 methods of a.
GenericServlet class
b.
c.
13. getHeaderNames
8
14. getProtocol
15. addIntHeader
9
Module – 2 Introduction to JSP
Q1 Which tag should be used to pass information from JSP to included JSP?
a) Using <%jsp:page> tag
b) Using <%jsp:param> tag
c) Using <%jsp:import> tag
d) Using <%jsp:useBean> tag
Q2. Which is not a directive?
a) include
b) page
c) export
d) useBean
Q3 Which page directive should be used in JSP to generate a PDF page?
a) contentType
b) generatePdf
c) typePDF
d) contentPDF
Q4. In <jsp:useBean> which two attributes is necessary
id and scope
class or beanName
type and id
none
Q5. Which of following is true by default
session
language
isTreadSafe
info
10
Q6. Choose correct scopes into JSP
page, request, session, application
page, response, session, application
page, request, response, session
None
Q7. JSP technology is extensible
True
False
Q8. Which methods can't be overridden in the JSP page?
jspDestroy()
jspInit
_jspService()
getParameter()
Q9. The maximum age of the cookie in JSP can be set by ?
cookie.setAgeMax(int seconds)
cookie.setAgeMax(float seconds)
cookie.setMaxAge(float seconds)
cookie.setMaxAge(int seconds)
11
II. Write short notes with example for the following
1. Scriptlet Tag ( <% … %> )
2. Expression Tag: ( <%= … %> )
3. Directive Tag ( <%@ … %> )
4. JSP Action
12
5. JSP Comments
III. Complete the Table
Attribute Description
buffer
contentType
Import
iserrorPage
sThreadSafe
13
IV. Compare <jsp:include> action tag with include directive
Description <jsp:include> Include directive
V. List of Advantages of JSP
1.
2.
14
3.
4.
5.
VI Write a JSP program shows the Fibonacci series upto a particular term, while the
input is taken from an HTML form
15
Module – 3 Controlling the Structure of generated servlets and Java Beans :
Q1. Which of the following retention policy and the type that defines @override ?
A. SOURCE,METHOD
B. CLASS,METHOD
C. CLASS,TYPE
D. TYPE,CLASS
Q2. Which of the following are the valid retention policy type available in Java ?
SOURCE
CLASS
RUNTIME
CODE
Q3. In <jsp:useBean> which two attributes is necessary
id and scope
class or beanName
type and id
none
Q4.Which tag should be used to pass information from JSP to included JSP?
Using <%jsp:page> tag
Using <%jsp:param> tag
Using <%jsp:import> tag
Using <%jsp:useBean> tag
Q5. Full form for JAR
16
8.Write a Java JSP program to get employee information through a HTML and create
JavaBean class, populate Bean and display the same information through an other JSP.
17
18
Module – 4 Annotations and JDBC
I. MCQ
Q1. What is JDBC?
A - JDBC is a java based protocol.
B - JDBC is a standard Java API for database-independent connectivity between the Java
programming language and a wide range of databases.
C - JDBC is a specification to tell how to connect to a database.
D - Joint Driver for Basic Connection
Q 2 - In which of the following type of ResultSet, the cursor can only move forward in the result
set?
A - ResultSet.TYPE_FORWARD_ONLY
B - ResultSet.TYPE_SCROLL_INSENSITIVE
C - ResultSet.TYPE_SCROLL_SENSITIVE
D - None of the above.
Q 3 - Which of the following type of JDBC driver, is also called Type 3 JDBC driver?
A - JDBC-ODBC Bridge plus ODBC driver
B - Native-API, partly Java driver
C - JDBC-Net, pure Java driver
D - Native-protocol, pure Java driver
Q 4 - Which of the following is correct about Class.forName() method call?
A - This method dynamically loads the driver's class file into memory, which automatically
registers it.
B - This method is preferable because it allows you to make the driver registration configurable
and portable.
C - Both of the above.
19
D - None of the above.
Q 5 - Which of the following is correct about batch process?
A - Batch Processing allows you to group related SQL statements into a batch.
B - Batch Processing allows you to submit batch of SQL statements with one call to the
database.
C - Both of the above.
D - None of the above.
Answer : C
Q6. Functional interfaces can be annotated as
@Function
@FunctionalInterface
@Functional
@Interface
Q7. Database system compiles the query when it is
Executed
Initialized
Prepared
Invoked
Q8. To execute a statement, we invoke the method
executeUpdate method
executeRel method
executeStmt method
executeConn method
Q9. Method on the result set that tests whether there remains at least one unfetched tuple in
the result set, is said to be
Fetch method
Current method
20
Next method
Access method
Q10. Which driver is efficient and always preferable for using JDBC applications?
A. Type – 4
B. Type – 1
C. Type – 3
D. Type – 2
Q11. Which of the following methods are needed for loading a database driver in JDBC?
A. registerDriver() method
B. Class.forName()
C. Both A and B
D. getConnection()
Q12. What is, in terms of JDBC, a DataSource?
A. A DataSource is the basic service for managing a set of JDBC drivers
B. A DataSource is the Java representation of a physical data source
C. A DataSource is a registry point for JNDI-services
D. A DataSource is a factory of connections to a physical data source
Q13. What is the disadvantage of Type-4 Native-Protocol Driver?
A. At client side, a separate driver is needed for each database
B. Type-4 driver is entirely written in Java
C. The driver converts JDBC calls into vendor-specific database protocol
D. It does not support to read MySQL data
Q14. What happens if you call deleteRow() on a ResultSet object?
A. The row you are positioned on is deleted from the ResultSet, but not from the database
B. The row you are positioned on is deleted from the ResultSet and from the database
21
C. The result depends on whether the property synchronizeWithDataSource is set to true or
false
D. You will get a compile error: the method does not exist because you cannot delete rows
from a ResultSet
Q15. Which statements about JDBC are true?
A. JDBC is an API to connect to relational-, object- and XML data sources
B. JDBC stands for Java DataBase Connectivity
C. JDBC is an API to access relational databases
D. JDBC is an API to bridge the object-relational mismatch between OO programs and
relational databases
II. What will be output of following code -
22
a. compilation error
b. Runtime Exception
c. Compile and execute without any Exception or error
d. None
How to send the class file to another directory or drive?
Define JAR file
23
Options for creating JAR
Options Description
-c
-v
-m
-f
-x
-t
-u
24
Module – 5 EJB and Server Side Component Models :
Q1. What is the Life Cycle of Session bean ? Stateless session bean is instantiated by
A. newInstance()
B. create()
C. Both of these
D. None
Q2. Which of the following annotation is used to specify or inject a dependency as ejb instance
into another ejb?
A. javax.ejb.Stateless
B. javax.ejb.Stateful
C. javax.ejb.MessageDrivenBean
D. javax.ejb.EJB
Answer: Option D
Q3. To expose a service through RMI, you have to create the service interface that extends
A. java.rmi.Remote
B. java.rmi.RemoteException
C. All of the mentioned
D. none of the mentioned
Q4. The EJB specification architecture does NOT define
A. transactional components
B. client side security and encryption
C. distributed object components
D. server-side components
25
Q5. Which statement about session beans is true?
A. In both stateless and statefull session classes, the bean provider must write the method
public void remove()
B. The method << remove >> in the component interface can be accessed only by the remote
clients
C. The bean’s handle must be provided by the client, in order to ask the EJBHome for
removing a session bean
D. None of the above
Q6. Which case of a session bean obtains the UserTransaction object via the EJBContext
using the getUserTransaction() method in EJB transaction management?
A. Bean-managed transactions
B. Container-managed transactions
C. Both A & B
D. None of the above
Q7. What happens when you pass a serialized object as an argument or return value that
doesn't exist either in the Client or the Server?
A. Nothing will happpen
B. An error will occur
C. A run time exception is produced
D. None of these
Q8. Abbreviate the term JMS?
A. Java Message Service
B. Java Monitor Service
C. Java Message Session
D. Java Monitor Session
Q9. An entity bean's local interface MUST extend the ________ interface.
A. javax.ejb.EJBLocalObject
26
B. javax.ejb.EJBObject
C. javax.ejb.RemoteObject
D. None of the above
Q10. Which component does the Entity bean represent the persistent data stored in the
database?
A. Server-side component
B. Client-side component
C. Server and client side component
D. None of the above
Q11. Which server-side component is required to be deployed on the server?
A. EJB
B. RMI
C. Both A & B
D. None of the above
Q13.Which option is true about session scope?
a) Objects are accessible only from the page in which they are created
b) Objects are accessible only from the pages which are in same session
c) Objects are accessible only from the pages which are processing the same request
d) Objects are accessible only from the pages which reside in same application
Answer: b
II Difference between Stateless and stateful session bean
Stateless Session bean Stateful session bean
27
List the types of Bean
1.
2.
3.
28