aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ��Question, Class�� Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "��Question, Class��" Watch "��Question, Class��" New topic
Author

��Question, Class��

kevin goon
Ranch Hand

Joined: Jun 12, 2001
Posts: 62

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

Joined: May 10, 2001
Posts: 3244
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
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
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

Joined: May 09, 2001
Posts: 94
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

Joined: May 10, 2001
Posts: 3244
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

Joined: Apr 11, 2001
Posts: 356
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

Joined: May 18, 2001
Posts: 128

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

Joined: May 09, 2001
Posts: 94
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

Joined: May 05, 2000
Posts: 13974
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.)


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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

Joined: May 07, 2001
Posts: 195
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ��Question, Class��