File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes scjp5.0 question from 2nd chapter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "scjp5.0 question from 2nd chapter" Watch "scjp5.0 question from 2nd chapter" New topic

scjp5.0 question from 2nd chapter

nikunj thakur
Ranch Hand

Joined: Sep 23, 2007
Posts: 47
class Dog { }
2. class Beagle extends Dog { }
4. class Kennel {
5. public static void main(String [] arfs) {
6. Beagle bl = new Beagle();
7. Dog dogl = new Dog();
8. Dog dog2 = bl;
9. // insert code here
10. } }

Which, inserted at line 9, will compile? (Choose all that apply.)

Beagle b2 = (Beagle) dog1;

Beagle b3 = (Beagle) dog2;

Beagle b4 = dog2;

None of the above statements will compile.

The answer is 1st & 2nd line,but when i run the program it showing error
for the 1st answer
Praveen Seluka
Ranch Hand

Joined: Jul 17, 2007
Posts: 95

KB says the first two line would compile but the firstline would give a runtime exception.Check it out.
P Ventura
Ranch Hand

Joined: Jan 24, 2007
Posts: 42
First line compile because all assignment and casts are correct, but throw a runtime exception: the actual object is not a Beagle.

Second line compile and run correctly.

Objective: SCJP 1.5<br /><a href="" target="_blank" rel="nofollow">API</a><br /> <blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre><font size="2"><br />Double n1 = Double.NaN; Double n2 = Double.NaN;<br /> n1.equals(n2) // true even though Double.NaN != Double.NaN<br />-0.0 == +0.0; // true<br />Double n1 = -0.0;Double n2 = +0.0;<br />n1.equals(n2) // false even though -0.0 == +0.0<br /></font></pre><hr></blockquote>
I agree. Here's the link:
subject: scjp5.0 question from 2nd chapter
It's not a secret anymore!