• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

primitive as attribute

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The SCWCD Exam Study Kit book is for SCWCD 1.4 when there was no autoboxing...
 
Parth Twari
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic