Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

��Question, Class��

 
kevin goon
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public class SuperClass
class SubClassA extends SuperClass()
class SubClassB extends SuperClass()
public void test(SubClassA foo)
SuperClass bar = foo;
which statement is true about the assingnment in line 5?
a) the assingment in line5 is illegal and will not compile
b) the assingment legal, but might throw a ClassCastException at runtime
c) The assingment is legal and will always exeute without throwing an exception
d) the assingment is legal and the code will compile, but will always throw an
exception at runtime
Answer says d. But shouldn��t the answer be c??

Q2)
������4-26. Which two can diretly cause a thread to stop executing(choose 2)
a. exiting from a synchronized block
b. calling the wait method on an object
c. calling the notify method on an object
d. calling the notifyAll method on an object
e. calling the setPriority method on a thread object

ans : b,e ? why e?

Thank you!! apologize for putting many Q's but my exam is coming soon! =P
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin
For #1 I agree it looks like it should be c not d. Do they give an explaination why they said d?
For #2 I guess if you set a threads priority low enough then all other running threads (except daemons) will pre-empt it, and it might not get to run. On the other hand there is no guarantee this would happen so I would have to be careful ansering similar questions. Maybe they get away with it here by saying 'can' instead of 'will'.
Dave
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For ur Q#1,
I tend to think the answer should be b.
I feel the assignment is legal at compile time but likely to cause problem(But not always) depending on the class reffered during run time.
Any comments, corrections welcome please
 
Jyotsna Umesh
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Vick:
Kevin
For #1 I agree it looks like it should be c not d. Do they give an explaination why they said d?
For #2 I guess if you set a threads priority low enough then all other running threads (except daemons) will pre-empt it, and it might not get to run. On the other hand there is no guarantee this would happen so I would have to be careful ansering similar questions. Maybe they get away with it here by saying 'can' instead of 'will'.
Dave

Dave, you are absolutely right.
For Ques. 1 I also see option C as right ans. and Ques.2 actually these thread related questions ar very vague in the sense he language is very important, so you are right by saying, if the question had asked "which 2 options WILL stop ..... " then I would say even setting low priority to a thread does not necessarily means stop execution of the thread.
Hope we get clear and crisp languagequestions as far as threads are concerned.
Jyotsna
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually,
now that I read it in a little more detail (thanks to the post by tvs sundaram) I have to agree with him that b is the correct answer. C wouldn't be right because it might not always run without an exception, but the assignmnet itself is ok at compile time. Maybe it was a typo in the book that said d when it should have said b.

Dave

[This message has been edited by Dave Vick (edited June 20, 2001).]
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
For #Q1 the answer should be c. It won't throw any exception because we are assigning a subclass to super class. Possibility for a runtime exception is only when we assign superclass to subclass.
superclass = subclass ---> always valid.
subclass = (subclass)supeclass ---> it is ok at compile time,
runtime exception may be
thrown.

Vanitha.
 
Ricardo Polero
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

for the first the
b) the assingment legal, but might throw a ClassCastException at runtime
is the right question !
 
Jyotsna Umesh
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ricardo Polero:

for the first the
b) the assingment legal, but might throw a ClassCastException at runtime
is the right question !

Hi,
this is legal at compile time and will/ might throw ClassCastException at runtime.
yes, it is right question. though this is not the ans.
Jyotsna
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct answer is C. Java never throws an exception when a child class is placed into an object of type parent class. Otherwise we could have potential problems with huge portions of Java such as JDBC and EJB and JMS which are built around these types of frameworks. (Although they actually use interfaces but it's the same idea.)
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still arguing for B. Given only the code that is shown you have no idea what is being done before hand and in other parts of the code, look at this code:

This compiles fine but throws a classCastException at run time. So, given only the pieces we are given in Kevin's original question yes it will compile, but it won't always run without exception.
If anyone disagrees let me know

Dave
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave, the line that is throwing the ClassCastException is the line you added: "c1.tester((SubClassA)c2);", not any of the original code. You are trying to cast a SuperClass object into a SubClass type, which can and often does throw a ClassCastException.
The original code did not do this. I don't think you can properly answer the question by imagining unwritten code that might throw an exception. If that were the case, I could think of code for every question on the exam that would cause a compiler error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic