aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Problem in Mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Problem in Mock exam" Watch "Problem in Mock exam" New topic

Problem in Mock exam

Gurukant Desai
Ranch Hand

Joined: Apr 07, 2008
Posts: 56
I just found this question in javabeat's mock-3

8.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?



Integer count = (Integer) request.getSession(false).getAttribute("count");

if(count != null )




else request.getSession(false).setAttribute("count", new Integer(1));




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.

they have given answer as B but I think answer is C.
Reason :- I think answer C is correct because of the fact that request made by user is "very first request" and so session might not be available.
When session is not available then request.getSession(false) will return "null" and calling any method on null will result into null pointer exception.

Please reply whether I am right/wrong?
Garlapati Ravi
Ranch Hand

Joined: Mar 05, 2008
Posts: 168
My answer is also C

what is the explanation they have given to justify answer B

Ravi Kumar
SCWCD 5 - 89%, SCJP 1.4 - 90%
Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1278


The same for me... I think you're both right and the answer should be C. The documentation for getSession() clearly says
If create is false and the request has no valid HttpSession, this method returns null.

Have you already tried this in a real application?

Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1278

Now I rememembered what was wrong

A HTTP session is created by default! You have to specify it in the page directive if you don't want a session to be created:

John Pearson
Ranch Hand

Joined: Oct 19, 2007
Posts: 51
Marco's right. If you look at the servlet source code generated for your JSP page, you'll see that the session implicit variable is, by default, initialized by:

which will create a new session if one doesn't exist already.

Tricky question, though. I like it.
Gurukant Desai
Ranch Hand

Joined: Apr 07, 2008
Posts: 56
Thanks Marco/John for the explaination.... It was really a tricky one!!!
[ May 20, 2008: Message edited by: Gurukant Desai ]
I agree. Here's the link:
subject: Problem in Mock exam
Similar Threads
question about session?
getSession(false) at first request
Confused about getSession
another mock exam question
Session creation