This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes one question about JavaBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "one question about JavaBean" Watch "one question about JavaBean" New topic

one question about JavaBean

julie li

Joined: Feb 10, 2003
Posts: 3
Question 22
Question for Java Beans.
The contents of beans class are as follows:-
package projsp;
public class trying
private String name;
private int age;
public void setName(String n)
public void setAge(int a)

public int getAge()
return age;
public String getName()
return name;
The contents of test.jsp file are as follows:
<%@ page language="java" import="projsp.*" %>
<jsp:useBean id="test" class="projsp.trying" />
The Name is <getProperty name="test" property="name" />
The Age is < jsp:getProperty name="test" property="age" />
If a user types in http://servername:8080/test.jsp?name=Anand&age=22 the out put is
A The Name is Anand
The Age is 22
B The Name is Null
The Age is 0

C It gives a compilation error as the
properties are not set prior
to retrieving them
D The Name is null
The Age is null

I think the correct answer is B.
But the Question Auther choose D.
Could anybody help to explain it to me?
Thanks a lot !!!
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
hi julie,
it is because you haven't set the properties first before getting the property values. You should first set it to something like <jsp:setProperty name="test" property="*" />. This would get the parameters attached to your address and assign the corresponding values to the properties of the bean i.e. test, being accessed.
Since the properties haven't been set, and since these are instance variables, they would be assigned default values, e.g. null for String.
It would be better if you whip up some code, run it, and see the results. Change the code for other possibilities e.g. initialize the instance variables of your bean class and see what happens.
Enrico Mannarino
Ranch Hand

Joined: Dec 14, 2001
Posts: 133
age is declared as int! And name as String!
The default values for these instance variables should be:
name = null
age = 0
So the right answer should be b)!
I think we have another thread on this somewhere!
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
hi Enrico,
Thanks, you're right. i might have jumbled the code or mixed up the results while testing different variations.
julie li

Joined: Feb 10, 2003
Posts: 3
Hi E,rico and Boyet,
Thanks for your help. I have passed the SCWCD exam with score 86%
Best Regards
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
Congratulations on your efforts, Julie.
It is sorta covered in the JavaRanch Style Guide.
subject: one question about JavaBean
Similar Threads
mock question
jsp:useBean Example
question about jsp:useBean from mock test by anand chawala
a question need help
Mock Exam Question