TCS Tech Web Development With Java | 2018

What is the output of the following code
The value is <%=””%>
Ans: The value is

Which of the following is not an attribute of Page Directive
Ans: name

Select valid expression tag from the following
Ans: <%=new java.util.Date().toString()%>

Comments in Jsp file can be written by
Ans: <%--  ……….. --%> and <!--  ……….. -->

Why RequestDispatcher to forward a request to another resource, instead of using a sendRedirect?
Ans:  The RequestDispatcher does not require a round trip to the client, and thus is more efficient and allows the server to maintain request state.
In JSP, how can you know that HTTP method (GET or POST) is used by client request?
Ans:  request.getMethod()

Select the correct scopes in JSp
Ans: page,request,session,application
What is a benefit of using JavaBeans to separate business logic from presentation markup within the JSP environment?      Ans:
Which of the following are correct. Select the one correct answer.
Ans:  To use the character %> inside a scriptlet, you may use %\> instead.

 What gets printed when the following JSP code is invoked in a browser. Select the one correct answer.
    <%= if(Math.random() < 0.5) %>
      hello
    <%= } else { %>
      hi
    <%= } %>
Ans: The JSP file will not compile.

What gets printed when the following is compiled. Select the one correct answer.
    <% int y = 0; %>
    <% int z = 0; %>

    <% for(int x=0;x<3;x++) { %>
    <% z++;++y;%>
    <% }%>
    <% if(z<y) {%>
    <%= z%>
    <% } else {%>
    <%= z - 1%>
    <% }%>

Ans: 3

Which of the following is not implicit object in JSP?
Ans: pageConfig

Which of the following code is used to set the session timeout in servlets?
Ans: session.setMaxInactiveInterval(interval)

What is the return type of getParameter(String name) in ServletRequest
Ans:  String

Which of the following code is used to set auto refresh of a page after 5 seconds?
Ans: response.setIntHeader(“Refresh”,5)

How to create a cookie in a servlet?
Ans: Use new Operator

Which of the following code retrieves the MIME type of the body of the request?
Ans: request.getContentType()

Which of the following code delete a cookie in servlet?
Ans: cookie.setMaxAge(0);
Select the right options which second.jsp can be included in first.jsp
Ans:  <%@include file=”second.jsp”%> , <jsp:include page=”second.jsp”/>

Which of the below methods returns a string containing information about the servlet, such as its author, version, and copyright.
Ans: getServletInfo()

A JSP page called test.jsp is passed a parameter name in the URL using “http://localhost:8080/projectName/test.jsp?
Ans:
Which of the following code used to get session in servlet?
Ans: request.getSession()
In HTTp request which asks for the loopbacks of the request message, for testing or troubleshooting?
TRACE
What is the return type of getAttribute(String name) in ServletRequest
Ans: Object
Select the right method to include welcome file in a web application
Which of the following code can be used to set the character encoding for the body of the response?
ANS: response.setCharacterEncoding(charset)
Which of the following package contains servlet classes?
ANS: javax.servlet, javax.servlet.http
Select a valid declaration from the following
Ans: <%! String orgName=”TCS”;%>
What is the return type of getAttribute(String name) in ServletRequest
ANS: Object
A JSP needs to generate an XML file. Which attribute of page directive may be used to specify that JSP is generating an XML file?
ANS: contentType
How can you make jsp page as error page
ANS: isErrorPage=”true”
String name = request.getAttribute(“name”); Select the correct option from below for this code statement.
Ans: It gives compilation error as we need to use the casting of (String)
Which of the following code can be used to clear the content of the underlying buffer in the response without clearing headers or status code.
Ans: response.resetBuffer()
Java Declaration tag is used to declare
ANS: both variables and methods
Which of the following is true about javax.servlet.error.exception_type?
Ans: This attribute gives information about exception type which can be stored and analyzed after storing in a java.lang.Class data type
Which of the following is legal JSP syntax to print the value of i.select the one correct answer
ANS: <%int i = 1;%>
<%= i %> (d)

Which method is used to retrieve a form values in a JSP or servlet?
ANS: request.getParameter(String)
Jsp is used for
ANS: Server Side Programming
Which is not a JSP directive ?
ANS: scriptlet
Which of the following code sends a cookie in servlet?
ANS: response.addCookie(cookie);
 Web Development With Java mcqs
1. Error Message displayed when the page is not found at current location?
Ans: 404

2. What is the output of the following code. The value is <%=""%>
Ans: The value is

3. What gets printed when the following JSP code is invoked in a browser. Select the one correct answer. <%=if(Math.random()<0.5%)>hello<%=}else{%>hi<%=}%>
Ans: The JSP file will not compile

4. Select the right option to set session expiry after 1 minute of inactiveness
Ans: <session-config><session-timeout>1</session-timeout></session-config>

5. JSP is used for
Ans: Server Side Programming

6. Which of the following is true about javax.servlet.error.exception_type?
Ans: This attribute gives information about exception type which can be stored and analyzed after storing in a java.lang.Class data type

7. Which is not a JSP directive
Ans: scriptlet

8. Which of the following package contains servlet classes?
Ans: (a) javax.servlet
         (b) javax.servlet.http

9. JSP is
Ans: Java Server Pages

10. Select the correct scopes in JSP
Ans: page, request, session, application

11. Select a valid declaration from the following
Ans: <%! String orgName="TCS";%>

12. How to create a cookie in servlet?
Ans: Use new operator

13. How many implicit objects are there in JSP
Ans: 9

14. In JSP, how can you know what HTTP method (GET or POST) is used by client request?
Ans: request.getMethod()

16. Comments in JSP can be written by
Ans: Both a and b (a. <%--comments--%>, b. <!--comments-->)



19. Which of the following tags can be used to print the value of an expression to the output stream?
Ans: <%=%>

20. Why use RequestDispatcher to forward a request to another resource, instead of using a sendRedirect?
Ans: The RequestDispatcher does not require a round trip to the client, and thus is more efficient and allows the server to maintain request state

21. A JSP needs to generate an XML file. Which attribute of page directive may be used to specify that JSP is generating an XML file?
Ans: contentType

22. Select the right method to include welcome file in a web application
Ans: <welcome-file-list><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file-list>

23. Select a valid expression tag from the following
Ans: <%=new java.util.Dat().toString()%>


25. What is the difference between doing an include or a forward with a RequestDispatcher?
Ans: The forward method transfers control to the designated resource, while the include method invokes the designated resource, substitutes its output dynamically in the display, and returns control to the calling page

26. Which of the following is true about javax.servlet.error.exception_type?
Ans: This attribute gives information about exception type which can be stored and analyzed after storing in a java.lang.Class data type

27. What are Servlets?
Ans: Both of the above (Java Servlets are programs that run on a Web or Application server. Java Servlets act as a middle layer between a request coming from a web browser or other HTTP client and databases or applications on the HTTP server)

28. Which of the following is true about servlets?
Ans: All of the above. ((a) Servlets execute within the address space of a Web server. (b) Servlets are platform-independent because they are written in Java. (c)The full functionality of the Java class libraries is available to a servlet.)

29.  Which of the following is the correct order of servlet life cycle phase methods?
Ans: init, service, destroy

30. When init method of servlet gets called?
Ans: The init method is called when the servlet is first created.

31. Which of the following is true about init method of servlet?
Ans: Both of the above. ((a) The init method simply creates or loads some data that will be used throughout the life of the servlet. (b) The init method is not called again and again for each user request.)

32. When service method of servlet gets called?
Ans:  The service method is called whenever the servlet is invoked.

33. Which of the following is true about service method of servlet?
Ans: All of the above ((a) The servlet container i.e.webserver calls the service method to handle requests coming from the client. (b) Each time the server receives a request for a servlet, the server spawns a new thread and calls service. (c) The service method checks the HTTP request type GET,POST,PUT,DELETE,etc. and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate. )

34. When doGet method of servlet gets called?
Ans: Both of the above. ((a) A GET request results from a normal request for a URL. (b) The service method checks the HTTP request type as GET and calls doGet method.)

35. When doPost method of servlet gets called?
Ans: Both of the above. ((a) A POST request results from an HTML form that specifically lists POST as the METHOD. (b) The service method checks the HTTP request type as POST and calls doPost method.)

37. How can you make jsp page as error page
Ans: isErrorPage="true"

38. In JSP, how can you know what HTTP method (GET or POST) is used by client request?
Ans: request.getMethod()

39. Which of the following code can be used to set the character encoding for the body of the response?
Ans: response.setCharacterEncoding(charset)

40. Which of the following is not an implicit object in JSP?
Ans: pageConfig

41. Java declaration tag is used to declare
Ans: Both variables and methods

42. What is the return type of getParameter(String name) in ServletRequest?
Ans: String

What will be the output of the following code?
<% int x=5;%>; <%! Int x =10; %><%! Int y=50;%><%y*x%>
ANS: 250.0

how to get multiple values of selected checkboxes in a servlet?
ANS: String values[]=request.getParameterValues(“hobbies”);

which of the following code is used to get names of the parameters in servlet
ANS: request.getParameterNames()
JSP is
ANS: Java Server Pages
Which of the following code is used to set content type of a page to be serviced using servlet?

ANS: response.setContentType()

1 comment: