Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on mock exam question?

 
raymond yadao
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following java code:
//in file Book.java
package com.bookstore;
public class Book
{
private String isbn;
public Book(){ isbn = "UNDEFINED_BOOK"; }
public String getIsbn(){ return isbn; }
public void setIsbn(String value){ this.isbn = value; }
}

//in file MusicCD.java
package com.bookstore;
public class MusicCD
{
private String isbn;
public MusicCD(){ isbn = "UNDEFINED_CD"; }
public String getIsbn(){ return isbn; }
public void setIsbn(String value){ this.isbn = value; }
}
Code for browse.jsp:
<jsp:useBean class="com.bookstore.Book" type="com.acme.MusicCD" id="bookorcd" />
<jsp:getProperty name="bookorcd" property="isbn" />
What is printed in the generated response of browse.jsp?



Options

Select 1 correct option.

1. UNDEFINED_BOOK


2. UNDEFINED_CD


3. It will not compile


4. It will compile but will give an exception at request time.



correct answer is 3
my answer is 4.
My explanation.Since MusicCD doesn't extends Book, it will throw ClassCastException.ClassCastException is a runtime exception not a compile time exception.but why the answer is 3?
 
peter greaves
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would guess a compile time error is because the package name of the type can't be resolved? it is bookstore, not acme?
 
Neeraj Singhal
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to JSP Specification:
At least one of type and class must be present, and it is not valid to provide both class and beanName. If type and class are present, class must be assignable to type (in the Java platform sense). For it not to be assignable is a translation-time error.
Means...answer 3 is correct, since it is translation time error.
 
Sathya Sankar
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raymond,
Got to see this thread pretty late. Peter said it right:
i would guess a compile time error is because the package name of the type can't be resolved? it is bookstore, not acme?

The first step in "executing" a jsp is to compile it into it's equivalent servlet. The container does it by first converting the jsp into an equivalent .java file and then attempting to compile it into it's .class representation. This compilation fails because com.acme.MusicCD is not in it's classpath.
So the right answer is option 3.
Ciao,
GSS
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic