Java MCQ Number 01108

Java MCQ Set 1

1. Which page directive should be used in JSP to generate a PDF page?
a) contentType
b) generatePdf
c) typePDF
d) contentPDF

Answer

Answer: a [Reason:] <%page contentType=”application/pdf”> tag is used in JSP to generate PDF.

2. 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

Answer

Answer: a [Reason:] <%jsp:param> tag is used to pass information from JSP to included JSP.

3. Application is instance of which class?
a) javax.servlet.Application
b) javax.servlet.HttpContext
c) javax.servlet.Context
d) javax.servlet.ServletContext

Answer

Answer: d [Reason:] Application object is wrapper around the ServletContext object and it is an instance of a javax.servlet.ServletContext object.

4. _jspService() method of HttpJspPage class should not be overridden.
a) True
b) False

Answer

Answer: a [Reason:] _jspService() method is created by JSP container. Hence, it should not be overridden.

5. 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

Answer: b [Reason:] Object data is available till session is alive.

6. Default value of autoFlush attribute is?
a) true
b) false

Answer

Answer: a [Reason:] Default value “true” depicts automatic buffer flushing.

7. Which one is correct order of phases in JSP life cycle?
a) Initialization, Cleanup, Compilation, Execution
b) Initialization, Compilation, Cleanup, Execution
c) Compilation, Initialization, Execution, Cleanup
d) Cleanup, Compilation, Initialization, Execution

Answer

Answer: c [Reason:] The correct order is Compilation, Initialization, Execution, Cleanup.

8. “request” is instance of which one of the foloowing classes?
a) Request
b) HttpRequest
c) HttpServletRequest
d) ServletRequest

Answer

Answer: c [Reason:] request is object of HttpServletRequest.

9. Which is not a directive?
a) include
b) page
c) export
d) useBean

Answer

Answer: c [Reason:] Export is not a directive.

10. Which is mandatory in tag?
a) id, class
b) id, type
c) type, property
d) type,id

Answer

Answer: a [Reason:] The useBean searches existing object and if not found creates object using class.

Java MCQ Set 2

1. Which one of the following is correct for directive in JSP?
a) <%@directive%>
b) <%!directive%>
c) <%directive%>
d) <%=directive%>

Answer

Answer: a [Reason:] Directive is declared as <%@directive%>.

2. Which of the following action variable is used to include a file in JSP?
a) jsp:setProperty
b) jsp:getProperty
c) jsp:include
d) jsp:plugin

Answer

Answer: c [Reason:] jsp:include action variable is used to inclide a file in JSP.

3. Which attribute uniquely identifyaction element?
a) ID
b) Class
c) Name
d) Scope

Answer

Answer: a [Reason:] ID attribute is used to uniquely identify action element.

4. “out” is implicit object of which class?
a) javax.servlet.jsp.PrintWriter
b) javax.servlet.jsp.SessionWriter
c) javax.servlet.jsp.SessionPrinter
d) javax.servlet.jsp.JspWriter

Answer

Answer: d [Reason:] JspWriter object is referenced by the implicit variable out which is initialized automatically using methods in the PageContext object.

5. Which object stores references to the request and response objects?
a) sessionContext
b) pageContext
c) HttpSession
d) sessionAttribute

Answer

Answer: b [Reason:] pageContext object contains information about directives issued to JSP page.

6. What temporarily redirects response to the browser?
a) <jsp:forward>
b) <%@directive%>
c) response.sendRedirect(URL)
d) response.setRedirect(URL)

Answer

Answer: c [Reason:] response.sendRedirect(URL) directs response to the browser and creates a new request.

7. Which tag is used to set a value of a JavaBean?
a) <c:set>
b) <c:param>
c) <c:choose>
d) <c:forward>

Answer

Answer: a [Reason:] <c:set> is used to set a value of a java.util.Map object.

8. Can <!–comment–> and <%–comment–%> be used alternatively in JSP?
a) True
b) False

Answer

Answer: b [Reason:] <!–comment–> is an HTML comment. <%–comment–%> is JSP comment.

9. Java code is embedded under which tag in JSP?
a) Declaration
b) Scriptlet
c) Expression
d) Comment

Answer

Answer: b [Reason:] Scriptlet is used to embedd java code in JSP.

10. Which of the following is not a directive in JSP?
a) page directive
b) include directive
c) taglib directive
d) command directive

Answer

Answer: d [Reason:] command directive is not a directive in JSP.

Java MCQ Set 3

1. What are the components of marker interface?
a) Fields and methods
b) No fields, only methods
c) Fields, no methods
d) No fields, No methods

Answer

Answer: d [Reason:] Marker interface in Java is an empty interface in Java.

2. Which of the following is not a marker interface?
a) Serializable
b) Cloneable
c) Remote
d) Reader

Answer

Answer: d [Reason:] Reader is not a marker interface. Serializable, Cloneable and Remote interfaces are marker interface.

3. What is not the advantage of Reflection?
a) Examine a class’s field and method at runtime
b) Construct an object for a class at runtime
c) Examine a class’s field at compile time
d) Examine an object’s class at runtime

Answer

Answer: c [Reason:] Reflecation inspects classes, interfaces, fields and methods at a runtime.

4. How private method can be called using reflection?
a) getDeclaredFields
b) getDeclaredMethods
c) getMethods
d) getFields

Answer

Answer: b [Reason:] getDeclaredMethods gives instance of java.lang.reflect.Method.

5. How private field can be called using reflection?
a) getDeclaredFields
b) getDeclaredMethods
c) getMethods
d) getFields

Answer

Answer: a [Reason:] getDeclaredFields gives instance of java.lang.reflect.Fields.

6. What is used to get class name in reflection?
a) getClass().getName()
b) getClass().getFields()
c) getClass().getDeclaredFields()
d) new getClass()

Answer

Answer: a [Reason:] getClass().getName() is used to get a class name from object in reflection.

7. How method can be invoked on unknown object?
a) obj.getClass().getDeclaredMethod()
b) obj.getClass().getDeclaredField()
c) obj.getClass().getMethod()
d) obj.getClass().getObject()

Answer

Answer: c [Reason:] obj.getClass().getMethod is used to invoke a method on unknown object obj.

8. How to get the class object of associated class using Reflection?
a) Class.forName(“className”)
b) Class.name(“className”)
c) className.getClass()
d) className.getClassName()

Answer

Answer: a [Reason:] forName(String className) returns the Class object associated with the class or interface with the given string name.

9. What does Class.forName(“myreflection.Foo”).getInstance() return?
a) An array of Foo objects
b) class object of Foo
c) Calls the getInstance() method of Foo class
d) Foo object

Answer

Answer: d [Reason:] Class.forName(“myreflection.Foo”) returns the class object of Foo and getInstance() would return a new object.

10. What does foo.getClass().getMethod(“doSomething”, null) return?
a) doSomething method instance
b) Method is returned and we can call the method as method.invoke(foo,null);
c) Class object
d) Exception is thrown

Answer

Answer: b [Reason:] foo.getClass().getMethod() returns a method and we can call the method using method.invoke();

Java MCQ Set 4

1. How constructor can be used for a servlet?
a) Initialization
b) Contructor function
c) Initialization and Contructor function
d) Setup() method

Answer

Answer: c [Reason:] We cannot declare constructors for interface in Java.This means we cannot enforce this requirement to any class which implements Servlet interface.
Also, Servlet require ServletConfig object for initialization which is created by container.

2. Can servlet class declare constructor with ServletConfig object as a argument?
a) True
b) False

Answer

Answer: b [Reason:] ServletConfig object is created after the constructor is called and before init() is called. So, servlet init parameters cannot be accessed in the constructor.

3. 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

Answer

Answer: b [Reason:] Servlets execute on Server and doesn’t have GUI.Applets execute on browser and has GUI.

4. 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)

Answer

Answer: a [Reason:] session has various methods for use.

5. Which method is used to get three-letter abbreviation for locale’s country in servlets?
a) Request.getISO3Country()
b) Locale.getISO3Country()
c) Response.getISO3Country()
d) Local.retrieveISO3Country()

Answer

Answer: a [Reason:] Each country is usually denoted by a 3 digit code.ISO3 is the 3 digit country code.

6. 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()

Answer

Answer: c [Reason:] InputStream is an abstract class. getInputStream() retrieves the request in binary data.

7. When destroy() method of filter is called?
a) The destroy() method is called only once at the end of the life cycle of a filter
b) The destroy() method is called after the filter has executed doFilter method
c) Both of the mentioned
d) The destroyer() method is called after the filter has executed

Answer

Answer: a [Reason:] destroy() is an end of life cycle method so it is called at the end of life cycle.

8. 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

Answer

Answer: d [Reason:] 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.

9. How is dynamic interception of requests and responses to transform the information done?
a) servlet container
b) servlet config
c) servlet context
d) servlet filter

Answer

Answer: d [Reason:] Servlet has various components like container, config, context, filter. Servlet filter provides the dynamic interception of requests and responses to transform the information.

10. 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

Answer

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

Java MCQ Set 5

1. Which of the following is used for session migration?
a) Persisting the session in database
b) URL rewritting
c) Create new database connection
d) Kill session from multiple sessions

Answer

Answer: a [Reason:] Session migration is done by persisting session in database. It can also be done by storing session in memory on multiple servers.

2. Which of the below is not a session tracking method?
a) URL rewriting
b) History
c) Cookies
d) SSL sessions

Answer

Answer: b [Reason:] History is not a session tracking type. Cookies, URL rewriting, Hidden form fields and SSL sessions are session tracking methods.

3. Which of the following is stored at client side?
a) URL rewritting
b) Hidden form fields
c) SSL sessions
d) Cookies

Answer

Answer: d [Reason:] Cookies are stored at client side. Hence, it is advantageous in some cases where clients disable cookies.

4. Which of the following leads to high network traffic?
a) URL rewritting
b) Hidden form fields
c) SSL sessions
d) Cookies

Answer

Answer: a [Reason:] WRL rewritting requires large data transfer to and from server which leads to network traffic and access may be slow.

5. 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 time-out value for session variable is 20 minutes
d) New session cannot be created for a new user

Answer

Answer: c [Reason:] Default time-out value for session variable is 20 minutes. This can be changed as per requirement.

6. SessionIDs are stored in cookies.
a) True
b) False

Answer

Answer: a [Reason:] SessionIDs are stored in cookies, URLs and hidden form fields.

7. What is the maximum size of cookie ?
a) 4 KB
b) 4 MB
c) 4 bytes
d) 40 KB

Answer

Answer: a [Reason:] 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.

8. How can we invalidate a session?
a) session.discontinue()
b) session.invalidate()
c) session.disconnect()
d) session.falsify()

Answer

Answer: b [Reason:] We can invalidate session by calling session.invalidate() to destroy the session.

9. 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

Answer

Answer: c [Reason:] HTML Hidden field is the simplest way to pass information but it is not secure and session can be hacked easily.

10. Which object is used by spring for authentication?
a) ContextHolder
b) SecurityHolder
c) AnonymousHolder
d) SecurityContextHolder

Answer

Answer: d [Reason:] The SessionManagementFilter checks the contents of the SecurityContextRepository against the current contents of the SecurityContextHolder to determine whether user has been authenticated during the current request by a non-interactive authentication mechanism, like pre authentication or remember me.

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.