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
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
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
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
Joined: May 10, 2001
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.
[This message has been edited by Dave Vick (edited June 20, 2001).]
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.
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.)
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, 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.