wood burning stoves 2.0
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes another mock exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "another mock exam question" Watch "another mock exam question" New topic

another mock exam question

madhav changala
Ranch Hand

Joined: Dec 20, 2005
Posts: 57
Consider the following JSP code (See exhibit).What will it print for the very first request to this page as well as the web application that contains this page?

<html><body><% Integer count = (Integer) request.getSession(false).getAttribute("count"); if(count != null ) { out.println(count); } else request.getSession(false).setAttribute("count", new Integer(1));%>Hello!</body></html>

Select 1 correct option.
A.It will print Hello!
B.It will print Hello and will set the count attribute in the session.
C.It will throw a NullPointerException at request time.
D.It will not compile.

My answer was C.. as request.getSession(false) returns null at the very first request.. as null.set/getAttribute() results.. NullPointerexception.. right? But the answer given is B..

Could any help on this?


Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10032

Even I guess that the answer is C!

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
I will go for B. Why because by default session(Page Directive)=true is created for JSP.

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10032

Madhav, did you actually try this out??? I don't have a system to try it or otherwise I would have done it practically and checked!
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3635
Hi Madhav,
Did you see the detailed explanation that is provided with it?

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Manju Devarla
Ranch Hand

Joined: Dec 14, 2006
Posts: 85
it prints Hello! no Nullpointer exception
achayya matta
Ranch Hand

Joined: Oct 27, 2005
Posts: 111
Yes, it prints hello for the first request and "1 hello" for the next request.
madhav changala
Ranch Hand

Joined: Dec 20, 2005
Posts: 57
I did n't try out this one..

But there was no detailed answer...

Could any one from above tell how hello will be the correct answer?

Rishi Chopra
Ranch Hand

Joined: Mar 07, 2004
Posts: 108

Since the question has not specified the page directive in the code snippet .. so the jsp container assumes that the page directive has session set to true.So a jsp page automatically participates in session tracking.
<% Integer count = (Integer) request.getSession(false).getAttribute("count");
is encounteres then the session is already there so "request.getSession(false)" wud return the session created implicitly by the page but there is no attribute named count so above statement is equal to Integer count=null;

then the code enters the else clause where the count is set as an attribute with value as new Integer(1) and then Hello is printed..

I hope i am able to clear your confusion

Rishi Chopra

Life is Beautiful Learn to See it.........
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
yes rishi you are...

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.
I agree. Here's the link: http://aspose.com/file-tools
subject: another mock exam question
It's not a secret anymore!