• 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

HFSJ Mock Exam Question 55

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm wondering what is the different between
setAttribute("key", new X()); //print "B"
setAttribute("key", "x") // print "UB";
Question 55.
15. req.getSession().setAttribute("key", new X());
16. req.getSession().setAttribute("key", new X());
17. req.getSession().setAttribute("key", "x");
18. req.getSession().removeAttribute("key");

public void valueBound(HttpSessionBindingEvent event){
System.out.println("B");
}

public void valueUnbound(HttpSessionBindingEvent event){
System.out.println("UB");
}

Answer: B. BBUBUB


Any help would be much appreciated.
 
Ranch Hand
Posts: 90
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm wondering what is the different between
setAttribute("key", new X()); //print "B"
setAttribute("key", "x") // print "UB";
Question 55.
15. req.getSession().setAttribute("key", new X());
16. req.getSession().setAttribute("key", new X());
17. req.getSession().setAttribute("key", "x");
18. req.getSession().removeAttribute("key");
}
Answer: B. BBUBUB


The B/UB are printed only for elements of class X. "x" is a String... So:
15. new X() is created, prints "B"
16. another X() object is created and bound to session, so "B" is printed. But previous object created in line 15 is removed, so "UB" is printed too.
17. it removes object put in line 16 (so prints "UB") and inserts String object, so nothing more is printed.

So, it prints BBUBUB.

BTW: Personally for me it would be more logical that it prints BUBBUB (eg line 16 should first unbound previous object and then bound new object). But BBUBUB was the only answer that could fit for me while answering this question. And no - I didn't check it
 
Oh the stink of it! Smell my tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic