Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFSJ Mock Exam Question 55

 
julius hock
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.
 
Adam Czysciak
Ranch Hand
Posts: 90
5
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic