Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware mock doubt!!

 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following are valid JSP code fragments?
Each option shows code for an independent JSP file.

1. <%@ page import="java.util.*" autoFlush="true"%>
<%@ page import="java.io.*" autoFlush="false"%>

2. <%Date d = new Date();
out.println(d);
%>


3. <%= String val = request.getParameter("hello");
out.println(val);
%>

4. <%!
Hashtable ht = new Hashtable();
{
ht.put("max", "10");
}
%>


5. <%!
Hashtable ht = new Hashtable();
ht.put("max", "10");
%>

the given answer are in bold. I wonder how option 4 is the right answer?? Is this block indicate a intance intializer??
moreover why option 5 is not a correct answer??


Thanks
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poonam Agarwal wrote:
4. <%!
Hashtable ht = new Hashtable();
{
ht.put("max", "10");
}
%>


I wonder how option 4 is the right answer?? Is this block indicate a intance intializer??


Yes. Declarations will ended up outside the service method of the generated servlet.

moreover why option 5 is not a correct answer??




You cannot put the second statement there unless within a block. You can try in a simple class try to put that as instance members. Second line will fail to compile


 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%!
Hashtable ht = new Hashtable();
{
ht.put("max", "10");
}
%>


In this the code between {} is static Initialize thats why it is correct and in last option it is not ..
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kumar ankit wrote:
<%!
Hashtable ht = new Hashtable();
{
ht.put("max", "10");
}
%>


In this the code between {} is static Initialize thats why it is correct and in last option it is not ..


Well it is not a static initializer, it is an instance initializer block.

And about option 5, you can understand the cause of the error by looking at the type of code that will be generated by that declaration



The code won't look exactly like this but this is just to give you an idea...
 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry ankit
yes it is
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kumar ankit wrote:sorry ankit


Relax buddy . No need to say sorry to me. No one is perfect dude. Mistakes happen, its not a big deal
 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you to clear my stupid doubt..

I have noy noticed that line in the 5th option
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic