File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Mock Exam Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Mock Exam Question " Watch "Mock Exam Question " New topic
Author

Mock Exam Question

Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
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??


Thanks and Regards,<br />Nilesh<br />SCJP 1.4, SCWCD 1.4
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
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 !


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
What if we override the init(ServletConfig) and don't call super(ServletConfig)?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock Exam Question