wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes primitive as attribute 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 "primitive as attribute" Watch "primitive as attribute" New topic
Author

primitive as attribute

Parth Twari
Ranch Hand

Joined: Jan 20, 2010
Posts: 163
Which two code snippets allow you to insert an integer value into the session? (Choose two.)

A.int pageVisits = 47;
session.setAttribute("visits", pageVisits);

B.int pageVisits = 47;
session.setIntAttribute("visits", pageVisits);

C.int pageVisits = 47;
session.setAttribute("visits", new Integer(pageVisits));

D.int pageVisits = 47;
session.setIntAttribute("visits", new Integer(pageVisits));

Source: SAI Mock

Answer: A,C

Option A is correct; while the setAttribute method requires an Object in the second parameter, Java SE v5 uses autoboxing to convert the int primitive to an Integer object at runtime.

Option B is incorrect because there is no setIntAttribute method.

Option C is correct.

Option D is incorrect because there is no setIntAttribute method.

this is contradicting to this question of SCWCD EXAM STUDY KIT

2. Which of the following code snippets, when inserted in the doGet() method,
will correctly count the number of GET requests made by a user? (Select one)

a HttpSession session = request.getSession();
int count = session.getAttribute("count");
session.setAttribute("count", count++);

b HttpSession session = request.getSession();
int count = (int) session.getAttribute("count");
session.setAttribute("count", count++);

c HttpSession session = request.getSession();
int count = ((Integer) session.getAttribute("count")).intValue();
session.setAttribute("count", count++);

d HttpSession session = request.getSession();
int count = ((Integer) session.getAttribute("count")).intValue();
session.setAttribute("count", new Integer(count++));

Answer: d

Explanation
Remember that the setAttribute() and getAttribute() methods only work
with objects and not with primitive data types. The getAttribute() method
returns an object and so you need to cast the returned value to the actual type
(Integer, in this case). Similarly, you need to wrap the count variable into an
Integer object and pass it to the setAttribute() method.

I tested and the first one came as correct.


Parth Tiwari
| Pursuing Bachelor of Engineering | OSUM Club Leader | SCJP 6 | SCWCD 5 |...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

The SCWCD Exam Study Kit book is for SCWCD 1.4 when there was no autoboxing...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Parth Twari
Ranch Hand

Joined: Jan 20, 2010
Posts: 163
I was feeling the same these kind of problems are always there when we are attemting a newer version exam but studying an older version of the book.

I had referred older mocks as well and in them as well I was misdirected.

I think there are many other points which I am misdirected at ex. new use of xml schema.

anyways thanks.
Parth Twari
Ranch Hand

Joined: Jan 20, 2010
Posts: 163
Hi Ankit ,

I think we were wrong in assuming that autoboxing is available , have a look at this errata.

HFSJ

Page 847
Question 39 - Answer D

The question reads:
Given req is a reference to a valid HttpServletRequest, and:
13. String[] s = req.getCookies();
14. Cookie[] c = req.getCookies();
15. req.setAttribute("myAttr1", "42");
16. req.setAttribute("myAttr2", 42);
17. String[] s2 = req.getAttributeNames();
18. String[] s3 = req.getParameterValues("attr");
Which lines of code will not compile? (Choose all that apply.)
A. line 13
B. line 14
C. line 15
D. line 16
E. line 17
F. line 18
The soultion says that Answer D is not correct. "-Option D: setAttribute() takes a String and an Object, and as of Java 5, 42 can be boxed to an Object."
While this may be true for Java 5, this exam does not test on Java 5 as noted on page xxviii of the book: "About the SCWCD (for Java EE 1.5) exam The updated SCWCD exam is called "Sun Certified Web Component Developer for the Java Platform, Enterprise Edition 5" (CX-310-083), but don't get confused by the title.
The updated exam is still designed for Java EE v1.4 and for the servlet v2.4 and JSP v2.0 specifications." Which means autoboxing does not exist and Answer D IS correct. Line 16 will not compile.

Note from the Author or Editor:
Agreed. Drop option D and line 16 from the question here and on pg 811


errata of hfsj
Porto Alegre
Greenhorn

Joined: Mar 14, 2012
Posts: 2
I know it is an old topic, but question 2 does not have any right answer to me. Letter d would be correct if we change count++ for ++count.
Am I wrong?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1521
    
  22

I know it is an old topic, but question 2 does not have any right answer to me. Letter d would be correct if we change count++ for ++count.
Am I wrong?

Yes, you are right none of them will work.
First of all the first time there is no attribute in the Session which will cause a NullPointerException and secondly you will have to use the ++count in order for the counter to increase.

My question to you: where would put the "count" attribute in the session so that option "d" will work?

Regards,
Frits
Porto Alegre
Greenhorn

Joined: Mar 14, 2012
Posts: 2
Hi. Tnx for replying.

Assuming the "count" attribute already exists in the session (no NullPointer to worry about), as simple as: will do the trick;

Regards,
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1521
    
  22

Assuming the "count" attribute already exists in the session

What I meant is: how and where would you do the initialization of the "count" attribute?

Regards,
Frits
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: primitive as attribute
 
Similar Threads
question about session?
another mock exam question
Mock Exam Question doubt
Problem in Mock exam
how can I send the output of a servlet to a jsp page?