Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

doubt on mock exam question?

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic