Java 1 Topic Servlet

Java Questions & Answers

Topic Servlet


1. Servlet are used to program which component in a web application?
a) client
b) server
c) tomcat
d) applet
View Answer
Answer: b

Explanation: A servlet class extends the capabilities of servers that host applications which are
accessed by way of a request-response programming model.

2. Which component can be used for sending messages from one application to another?
a) server
b) client
c) mq
d) webapp
View Answer
Answer: c

Explanation: Messaging is a method of communication between software components or
applications. MQ can be used for passing message from sender to receiver.

3. How are java web applications packaged?
a) jar
b) war
c) zip
d) both jar and war
View Answer
Answer: d

Explanation: war are deployed on apache servers or tomcat servers. With Spring boot and few
other technologies tomcat is brought on the machine by deploying jar.

4. How can we connect to database in a web application?
a) oracle sql developer
b) toad
c) JDBC template
d) mysql
View Answer
Answer: c

Explanation: JDBC template can be used to connect to database and fire queries against it.
5. How can we take input text from user in HTML page?
a) input tag
b) inoutBufferedReader tag
c) meta tag
d) scanner tag
View Answer
Answer: a

Explanation: HTML provides various user input options like input, radio, text, etc.

6. What type of protocol is HTTP?
a) stateless
b) stateful
c) transfer protocol
d) information protocol

View Answer
Answer: a

Explanation: HTTP is a stateless protocol. It works on request and response mechanism and each
request is an independent transaction.

7. What does MIME stand for?
a) Multipurpose Internet Messaging Extension
b) Multipurpose Internet Mail Extension
c) Multipurpose Internet Media Extension
d) Multipurpose Internet Mass Extension
View Answer
Answer: b

Explanation: MIME is an acronym for Multi-purpose Internet Mail Extensions. It is used for
classifying file types over the Internet. It contains type/subtype e.g. application/msword.

8. What is the storage capacity of single cookie?
a) 2048 MB
b) 2048 bytes
c) 4095 bytes
d) 4095 MB
View Answer
Answer: c

Explanation: Storage capacity of cookies is 4095 bytes/cookie.

9. How constructor can be used for a servlet?
a) Initialization
b) Constructor function
c) Initialization and Constructor function
d) Setup() method
View Answer
Answer: c

Explanation: We cannot declare constructors for interface in Java. This means we cannot enforce
this requirement to any class which implements Servlet interface.
Also, Servlet requires ServletConfig object for initialization which is created by container.

10. Can servlet class declare constructor with ServletConfig object as an argument?
a) True
b) False
View Answer
Answer: b

Explanation: ServletConfig object is created after the constructor is called and before init() is
called. So, servlet init parameters cannot be accessed in the constructor.

11. What is the difference between servlets and applets?
i. Servlets execute on Server; Applets execute on browser
ii. Servlets have no GUI; Applet has GUI
iii. Servlets creates static web pages; Applets creates dynamic web pages
iv. Servlets can handle only a single request; Applet can handle multiple requests

a) i, ii, iii are correct
b) i, ii are correct
c) i, iii are correct
d) i, ii, iii, iv are correct
View Answer
Answer: b

Explanation: Servlets execute on Server and doesn’t have GUI. Applets execute on browser and
has GUI.

12. Which of the following code is used to get an attribute in a HTTP Session object in servlets?
a) session.getAttribute(String name)
b) session.alterAttribute(String name)
c) session.updateAttribute(String name)
d) session.setAttribute(String name)
View Answer
Answer: a

Explanation: session has various method for use

13. Which of the following code retrieves the body of the request as binary data?
a) DataInputStream data = new InputStream()
b) DataInputStream data = response.getInputStream()
c) DataInputStream data = request.getInputStream()
d) DataInputStream data = request.fetchInputStream()
View Answer
Answer: c

Explanation: InputStream is an abstract class. getInputStream() retrieves the request in binary
data.

14. Which of the following is true about servlets?
a) Servlets execute within the address space of web server
b) Servlets are platform-independent because they are written in java
c) Servlets can use the full functionality of the Java class libraries
d) Servlets execute within the address space of web server, platform independent and uses the
functionality of java class libraries
View Answer
Answer: d

Explanation: Servlets execute within the address space of a web server. Since it is written in java
it is platform independent. The full functionality is available through libraries.

15. How is the dynamic interception of requests and responses to transform the information
done?
a) servlet container
b) servlet config
c) servlet context
d) servlet filter
View Answer
Answer: d
Explanation: Servlet has various components like container, config, context, filter. Servlet filter
provides the dynamic interception of requests and responses to transform the information.

16. Which are the session tracking techniques?
i. URL rewriting
ii. Using session object
iii.Using response object
iv. Using hidden fields
v. Using cookies
vi. Using servlet object
a) i, ii, iii, vi
b) i, ii, iv, v
c) i, vi, iii, v
d) i, ii, iii, v
View Answer

Answer: b
Explanation: URL rewriting, using session object, using cookies, using hidden fields are session
tracking techniques.

17. How does applet and servlet communicate?
a) HTTP
b) HTTPS
c) FTP
d) HTTP Tunneling
View Answer
Answer: d

Explanation: Applet and Servlet communicate through HTTP Tunneling.

18. Which of the following is used for session migration?
a) Persisting the session in database
b) URL rewriting
c) Create new database connection
d) Kill session from multiple sessions
View Answer
Answer: a
Explanation: Session migration is done by persisting session in database. It can also be done by
storing session in memory on multiple servers.

19. Which of the below is not a session tracking method?
a) URL rewriting
b) History
c) Cookies
d) SSL sessions
View Answer
Answer: b
Explanation: History is not a session tracking type. Cookies, URL rewriting, Hidden form fields
and SSL sessions are session tracking methods.

20. Which of the following is stored at client side?
a) URL rewriting
b) Hidden form fields
c) SSL sessions
d) Cookies
View Answer
Answer: d

Explanation: Cookies are stored at client side. Hence, it is advantageous in some cases where
clients disable cookies.

21. Which of the following leads to high network traffic?
a) URL rewriting
b) Hidden form fields
c) SSL sessions
d) Cookies
View Answer
Answer: a

Explanation: WRL rewriting requires large data transfer to and from the server which leads to
network traffic and access may be slow.

22. Which of the following is not true about session?
a) All users connect to the same session
b) All users have same session variable
c) Default timeout value for session variable is 20 minutes
d) New session cannot be created for a new user
View Answer
Answer: c

Explanation: Default timeout value for session variable is 20 minutes. This can be changed as per
requirement.

23. SessionIDs are stored in cookies.
a) True
b) False
View Answer
Answer: a
Explanation: SessionIDs are stored in cookies, URLs and hidden form fields.

24. What is the maximum size of cookie?
a) 4 KB
b) 4 MB
c) 4 bytes
d) 40 KB
View Answer
Answer: a

Explanation: The 4K is the maximum size for the entire cookie, including name, value, expiry
date etc. To support most browsers, it is suggested to keep the name under 4000 bytes, and the
overall cookie size under 4093 bytes.

25. How can we invalidate a session?
a) session.discontinue()
b) session.invalidate()
c) session.disconnect()
d) session.falsify()
View Answer
Answer: b

Explanation: We can invalidate session by calling session.invalidate() to destroy the session.

26. Which method creates unique fields in the HTML which are not shown to the user?
a) User authentication
b) URL writing
c) HTML Hidden field
d) HTML invisible field
View Answer
Answer: c

Explanation: HTML Hidden field is the simplest way to pass information but it is not secure and
a session can be hacked easily.

27. Which method is used to specify before any lines that uses the PrintWriter?
A. setPageType()
B. setContextType()
C. setContentType()
D. setResponseType()

28. What are the functions of Servlet container?
A. Lifecycle management
B. Communication support
C. Multithreading support
D. All of the above

29. Which object of HttpSession can be used to view and manipulate information about
a session?
A. session identifier
B. creation time
C. last accessed time
D. All mentioned above

30. Which of the following is true about servlets?
A. Servlets execute within the address space of web server
B. Servlets are platform-independent because they are written in java
C. Servlets can use the full functionality of the Java class libraries
D. Servlets execute within the address space of web server, platform independent
and uses the functionality of java class libraries

31. Which of the following statements are correct about the status of the Http response?
A. A status of 200 to 299 signifies that the request was successful
B. A status of 300 to 399 are informational messages
C. A status of 400 to 499 indicates an error in the server
D. A status of 500 to 599 indicates an error in the client

32. Which JDBC driver Type(s) can be used in either applet or servlet code?
A. Both Type 1 and Type 2
B. Both Type 1 and Type 3
C. Both Type 3 and Type 4
D. Type 4 only

33. Which of the following code retrieves the MIME type of the body of the request?
A. new MimeType()
B. request.getContentType()
C. response.getContentType()
D. None of the above

34. Which methods are used to bind the objects on HttpSession instance and get the
objects?
A. setAttribute
B. getAttribute
C. Both A & B
D. None of the above

35. Choose the statement that best describes the relationship between JSP and
servlets:
A. Servlets are built on JSP semantics and all servlets are compiled to JSP pages for
runtime usage.
B. JSP and servlets are unrelated technologies.
C. Servlets and JSP are competing technologies for handling web requests. Servlets
are being superseded by JSP, which is preferred. The two
technologies are not useful in combination.
D. JSPs are built on servlet semantics and all JSPs are compiled to servlets for runtime
usage.
Ans: D

36. Why use RequestDispatcher to forward a request to another resource, instead of
using a sendRedirect?
A. Redirects are no longer supported in the current servlet API.
B. Redirects are not a cross-platform portable mechanism.
C. The RequestDispatcher does not use the reflection API.
D. The RequestDispatcher does not require a round trip to the client, and thus is more
efficient and allows the server to maintain request state.
Ans: D

37. Application is instance of which class?
a) javax.servlet.Application
b) javax.servlet.HttpContext
c) javax.servlet.Context
d) javax.servlet.ServletContext
View Answer
Answer: d
Explanation: Application object is wrapper around the ServletContext object and it is an instance of a javax.servlet.ServletContext object.

No comments:

Post a Comment