This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam Question

 
Nilesh Raje
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

I can across this mockexam Question on Jiris..!
Which statements are TRUE regarding the following code? Mark

Please select two correct answers.

import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends GenericServlet {

public void init() { //1
// Do something
}

public void init(ServletConfig config) //2
throws ServletException{
// Do something
super.init(config); //3
}

public void destroy() { //4
// Do something
}

public void service(ServletRequest req, //5
ServletResponse res) {
// Do something
}
}

A : Method //1 is necessary for this code to compile
B : Method //2 is necessary for this code to compile
C : Line //3 is necessary for this code to run
D : Method //4 is necessary for this code to compile
E : Method //5 is necessary for this code to compile


Explanation

You need to override the service() method when you extends GenericServlet.
You need to call super.init(config) if you override this method


I dont remember the answer given but i guess its C and E from the explanation given. Wanted to know if the explanation given is right.!Can we overide the Service menthod is we extend Generic Servlet??
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nilesh -

the class GenericServlet implements the interface Servlet.
But the class doesnt implement all the methods, it doesnt implement the
service(ServletRequest, ServletResponse) method, and thats why the GenericServlet class is declared abstract.

Since the servlet in the question extends the GenericServlet, option E is correct !

and about option C its correct because -

the code overrides the init(ServletConfig) nethod. Thus the container wont call the method (this method has a call to the init( ) method )that is inherited from the class GenericServlet. So in order that your init( ) method should be called, you must place a super.init(ServletConfig) method [ in your init(ServletConfig) ], which in turn has a call to the init( ) method that is already coded at line 1 .

hope this doesnt confuse you !
 
wise owen
Ranch Hand
Posts: 2023
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic