Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

November 2, 2009

SCWCD Questions

31 True or False ? A new servlet is created each time a client request a servlet ?

A True
B false

Answer
B
There will be only one servlet in a JVM (if your servlet is not SingleThreadModel). Each time a client makes the request, the container will create a new thread for handling that request.
Answer
32 public class MyServlet extends HttpServlet {
private int x = 0;
public MyServlet (int x) {
this.x = x;
}
public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
x++;
PrintWriter out = response.getWriter();
out.println(x);
}
}
What will be the output (assume all imports are done correctly), when invoking MyServlet ?

A Compiler error
B Runtime Exception
C Prints 1 in browser window.
D Prints the value of x in browser window , depending up on the value used to create the servlet.

Answer
B
It will give a ServletException telling that the MyServlet cannot be instantiated. This is because the container uses the default no argument constructor for instantiating the servlet. So if we are overloading the constructor (it is useless anyway) , we need to provide the non argument constructor also.
Answer
33 All Servlets implement which interface ?

A HttpServlet
B Servlet
C ServletRequest
D GenericServlet

Answer
B
All Servlets must implement javax.servlet.Servlet interface either directly or indirectly.
Answer
34 What is the argument for init() method ? Select all that apply.

A ServletContext
B ServletConfig
C HttpServletConfig
D HttpServlet
E No arguments

Answer
B and E
The init() method is having two overloaded versions in GenericServlet class. The init() that takes a ServletConfig object and init without any parameter. The no-argument init method is simply calling the init with ServletConfig object.
Answer
35 If a single client makes two requests, how many threads will be created by the container ?

A 0
B 1
C 2
D 3

Answer
C
For each and every request , the container will create a separate thread. It is one thread per request, not one thread per client. HTTP protocol don't have any mechanism to identify whether the request is coming from the same client or not.
Answer
36 If the web application is distributed across multiple JVMs, how many instances of the servlet will be created ?

A 1
B we cannot distribute a web-application in multiple JVMs.
C One instance per JVM
D None

Answer
C
There will be one instance per JVM in distributed web-apps. But there will be only one instance in a single JVM
Answer
37 HTTP HEAD method is used for ?

A To get the header part of the URL
B To delete a resource from server
C To place a resource on the server
D To connect to the server

Answer
A
HEAD method is used to get only the header part of the requested URL. It will return only header, no body.
Answer
38 Which of the following HTTP methods are not idempotent?

A POST
B GET
C HEAD
D PUT

Answer
A
HTTP 1.1 declares POST as non idempotent.
Answer
39 Which is the default HTTP form method?

A POST
B HEAD
C PUT
D GET

Answer
D
The default HTTP form method is GET.
Answer
40 Which method is used to retrieve a form value in a JSP or Servlet?

A request.getAttribute(String)
B response.getAttribute(String)
C request.getParameter(String)
D response.getParameter(String)

Answer
C
Option B and D are incorrect because , no such method exist. Option A is incorrect because we cannot use that method for receiving request parameters.

No comments: